为什么 (-2.4492935982947064e-16).toFixed(5) 等于“-0.00000”?

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

鉴于此代码:

const value = 1;
Math.sin(2 * Math.PI * value).toFixed(5);

为什么 return "-0.00000",而 .toFixed(5) 之前的值是 -2.4492935982947064e-16

您提供的数字-2.4492935982947064e-16是科学记数法。该数字将等于 -2.4492935982947064 × 10^-16,展开后正好是 -0.00000000000000024492935982947064

这个数字是科学计数法。

e-16 表示数字左边有 16 个 0。

-2.4492935982947064e-16

真的

-0.00000000000000024492935982947064

当你 运行 到 Fixed(5) 时,你最终得到 5 个小数位,它们都是 0。

-2.4492935982947064e-16-2.4492935982947064 * Math.pow(10,-16),所以小数点后 5 位不足以看出与 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))