Reputation: 45
Given this code:
const value = 1;
Math.sin(2 * Math.PI * value).toFixed(5);
Why does this return "-0.00000"
, when the value before .toFixed(5)
is -2.4492935982947064e-16
?
Upvotes: 1
Views: 1199
Reputation: 4673
The number is in scientific notation.
The e-16 means that there are 16 0's to the left of the number.
-2.4492935982947064e-16
is really
-0.00000000000000024492935982947064
When you run toFixed(5), you end up with 5 decimal places, which are all 0's.
Upvotes: 4
Reputation: 3458
-2.4492935982947064e-16
is -2.4492935982947064 * Math.pow(10,-16)
, so 5 places after decimal point is not enough to see something different than 0
const value = 1;
let result = Math.sin(2 * Math.PI * value);
console.log(result)
console.log(result.toFixed(20))
console.log(result.toFixed(5))
Upvotes: 0
Reputation: 6749
The number you presented -2.4492935982947064e-16
is in scientific notation. That number would be equivalent to -2.4492935982947064 × 10^-16
which would be exactly -0.00000000000000024492935982947064
after expanding it.
Upvotes: 0