What does 1e9 means in NodeJS hrtime context? [duplicate] - javascript

This question already has answers here:
What number does 8e3 evaluate to?
(4 answers)
Closed 6 years ago.
var time_arr = process.hrtime(start);
var time = (time_arr[0] * 1e9 + time_arr[1]) / 1e6;
What does it mean when the calculation has to multiply by 1e9 and divide by 1e6 ?

What does it mean when the calculation has to multiply by 1e9 and divide by 1e6 ?
It means that it's multiplied by 1 billion and divided by 1 million.
1e9 means 1 * 10 to the 9th power, which is 1 billion (1000000000).
1e6 means 1 * 10 to the 6th power, which is 1 million (1000000).
You can think of it as 1 and 9 zeroes, 1 and 6 zeroes etc.
It doesn't have to be 1, it can be:
3e4 meaning 30000
or:
1.2e6 meaning 1200000
You can also use negative powers of ten for decimal fractions:
1e-1 is 0.1
1e-2 is 0.01
etc.
It's useful for very large or very small numbers where you don't want to count zeroes.
See the scientific notation on Wikipedia:
https://en.wikipedia.org/wiki/Scientific_notation

Related

Generate a random number with fixed digit without leading zero [duplicate]

This question already has answers here:
Generate big numbers with Math random in Javascript
(4 answers)
Closed 1 year ago.
I'm using this one liner to generate a random 18 digit number :
Math.floor(100000000000000000 + Math.random() * 900000000000000000)
The problem is that it always contains one or more zeros at the end, and I don't understand why.
How can I generate a really random number that always contains 18 digits, and without a leading zero?
You have too many digits, you're running against the limit to precision in a javascript number.
It looks like a Problem with the max number Range. You can hack it like this. And it is a better Random number.
Math.floor(10000000000000000 + Math.random() * 90000000000000000)+ "" + Math.floor(Math.random()* 100)
But if you parse to int you have the same problem.
The solution for this so far is to generate two numbers (10 digits + 8 digits) & concatenate them as follow:
Math.floor(1000000000 + Math.random() * 9000000000) + "" + Math.floor(10000000 + Math.random() * 90000000)

How can one avoid too many numbers after the decimalpoint? [duplicate]

This question already has answers here:
How to deal with floating point number precision in JavaScript?
(47 answers)
Closed 2 years ago.
The following code generates numbers and than divides it with 1000:
But some times numbers like 1.2295999999999998 are generated, while 1229.6 is only being divided by 1000?
What do I need to change in the code, so that this does not occur?
Z1 = document.getElementById("Z1").innerHTML = Math.floor((Math.random() * 99899 + 100)) / Math.pow(10, Math.floor((Math.random() * 3 + 1)));
document.getElementById("L").innerHTML = Z1 / 1000;
<label id="Z1"></label><br>
<label id="L"></label>
document.getElementById("L").innerHTML = (Z1 / 1000).toFixed(2);
// if you want 2 digits after decimalpoint
Further Explaination:
Floating point numbers cannot represent all decimals precisely in binary. It has to do with how decimal values are converted to binary floating point numbers. For example, 1/10 turns into a repeating decimal in binary, so the number is not perfectly represented, and repeated operations can expose the error.

Please help make Modulo Simple To Understand

(Sorry in advance if this has been asked before, I searched and couldn't find a similar question)
So I believe modulo (%) gives me the remainder of a long division equation. so 2%4 =0r.
So in simple terms a modulo equation that equals zero should be an even number. and a modulo equation that equals 1, should be an odd number? Is that correct?
Here's where I begin to confuse myself.
What about equations that equal an even or odd remainder, would that still output an equal or odd number.
For instance. 5%149 equals 4r.. the remainder is an even number, so is the output all even numbers.. or does the very fact that there is any remainder at all mean that the output will be odd numbers??
TLDR, is modulo as simple as 0r outputs even numbers. And anything with 1 or more remainder outputs odd numbers.
Modulo (or modulus) is used to see the remainder of a division.
You can flip it on it's head and use multiplication to help you out as well if necessary. I've provided some examples.
Try doing something like this:
From the equation you posted in your example: 149 % 5 would give you the remainder of 4. Reason for this: The last multiple of 5 you can get before 149 is 145, and your modulus equation is telling you that you have 4 left over.
Now, if you were to do something like 150 % 5, your remainder would be 0 because 150 is a safe multiple of 5.
Some documentation should hopefully help you understand this a bit better as well: https://docs.onux.com/en-US/Developers/JavaScript-PP/Language/Reference/Expressions/arithmetic-operators/modulus
Some examples to help you understand the remainder:
10 % 5 = 0 Since 5 x 2 = 10
9 % 3 = 0 Since 9 x 3 = 9
6 % 2 = 0 Since 2 x 3 = 6
7 % 2 = 1 Since you can only multiply 2 three times to get 6, you are left with a remainder of 1.
a modulo equation that equals zero should be an even number. and a
modulo equation that equals 1, should be an odd number?
You've probably seen modulo as a way of testing for evenness but this is not right. It should read
a modulo by 2 operation that equals zero is an even number
ie. x % 2 == 0 implies x is even. Because x is divisible by 2. x % 3 == 0 means x is divisible by 3.
Here's one way I learned to look at it. Consider an analogue clock with n hours on in (perhaps n=12 or n=24 or some other funny clock). The first number in the modulo operation tells you how many hours forward to traverse, going round and round the circle. The second number (n) tells you how many hours is built into the clock.
Here are some examples:
You go forward 5 on a 12 hour clock and land on 5 o'clock.
5 % 12 == 5
You go forward 13, completing one full loop plus one more hour, landing on 1 o'clock.
13 % 12 == 1
You go forward 24, completing 2 full loops but landing at the starting point, 0. (ok most clocks have 12 at the top but its the same as 0.)
24 % 12 == 0
Consider a clock or spinning wheel with 4 categories.
Start at the base and take 7 steps forward. That give you one full traversal (4 steps) then 3 more steps lands you on the 3rd item.
7 % 4 == 3
You've just stepped forward 2. Because the wheel has 4 slots, the counting hasn't reset yet.
2 % 4 == 2
So to recap, the first number is the number of steps to take, the second number is the size of the clock.

Weird Javascript Calculation

Why when using javascript does this formula return -Infinity
793 * ( 1 - ( 1 + Math.pow(.032 / 12 , (-1 * (30 * 12))))) / (.032 / 12);
Because it's the correct answer!
The Math.pow part is equivalent to the following:
Math.pow(0.0026666666666666666, -360)
Which has the result Infinity
Then, you multiply it by -1, and multiply / divide it by positive numbers, which doesn't affect the result considering it's -Infinity.
The gist of it comes from Math.pow(.032 / 12 , (-1 * (30 * 12))). You're taking a very small number (0.032/12) and taking it to a large, negative power (-1 * 30 * 12).
Mathematically, that's the same as taking a normal-sized number and taking it to a large number -- you're basically calculating 375**360, which clearly is infinity. The rest of the numbers just end up making it -Infinity instead of Infinity.
According to MDN,
The MAX_VALUE property has a value of approximately 1.79E+308, or 21024. Values larger than MAX_VALUE are represented as "Infinity".
This part of your calculation Math.pow(.032 / 12 , (-1 * (30 * 12))) equals
4.4797768587048112310581443943723309108149091701091649 × 10^926
Which is larger than Number.MAX_VALUE, so it is represented as Infinity.
After that, you're basically just adding and flipping the sign.

understanding the modulus operator in javascript

Twenty modulus six is equal to two, which is the remainder but how to know the the modulus is using the 3 to perform the operation?
20 % 6 = 2
You can use
Math.floor(20 / 6);
for this. It rounds down so you have the largest number possible without decimals.
If you want the quotient, you just have to truncate the division
Math.trunc(20/6) //Return 3
Reference
Modulus is not using the 3, it simply knows that:
20 / 6 = 3, with a remainder of 2
=> 20 % 6 = 2
If you want the 3, you just need the expression:
Math.floor(20 / 6)
(or possibly Math.trunc depending on how you want negative numbers handled - floor rounds towards negative infinity, trunc rounds toward zero).

Categories