nifty
nifty

Reputation: 45

Why does (-2.4492935982947064e-16).toFixed(5) equal "-0.00000"?

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

Answers (3)

Tony Abrams
Tony Abrams

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

barbsan
barbsan

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

Krzysztof Krzeszewski
Krzysztof Krzeszewski

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

Related Questions