Math.sin()在服务器/客户端上给出不同的结果?

时间:2022-01-11 21:47:40

I use Meteor. It uses Javascript on both the server and client.

我用Meteor。它在服务器和客户端上都使用Javascript。

When I run Math.sin(356644061314425) * 10000 i get:

当我运行Math.sin(356644061314425)* 10000时,我得到:

-9986.46139381927 on the server and

-9986.46139381927在服务器上

-9986.46115497749 on the client / browser / app

-9986.46115497749在客户端/浏览器/应用程序上

Why is this? How can I prevent this?

为什么是这样?我怎么能阻止这个?

EDIT: The proposed duplicate questions refer to degrees / radians. I think mine is more a runtime problem.

编辑:建议的重复问题是指度/弧度。我认为我的更多是运行时问题。

1 个解决方案

#1


6  

I think the answer to "How do I prevent this?" is "you can't".

我认为答案是“如何防止这种情况?”是“你不能”。

The answer to "Why is this?" is that the javascript implementation of Math.sin is not determined.

答案为“这是为什么?”是没有确定Math.sin的JavaScript实现。

See http://www.ecma-international.org/ecma-262/5.1/#sec-15.8.2.16

Specifically "sin (x) Returns an implementation-dependent approximation to the sine of x. The argument is expressed in radians." (my italics).

具体来说,“sin(x)返回x的正弦的依赖于实现的近似值。参数以弧度表示。” (我的斜体)。

But my experimentation suggests that modern browsers currently only use one of two implementations, with Chrome being different to (and seemingly more accurate than) other browsers.

但我的实验表明,现代浏览器目前只使用两种实现中的一种,Chrome与其他浏览器不同(并且看起来比其他浏览器更准确)。

#1


6  

I think the answer to "How do I prevent this?" is "you can't".

我认为答案是“如何防止这种情况?”是“你不能”。

The answer to "Why is this?" is that the javascript implementation of Math.sin is not determined.

答案为“这是为什么?”是没有确定Math.sin的JavaScript实现。

See http://www.ecma-international.org/ecma-262/5.1/#sec-15.8.2.16

Specifically "sin (x) Returns an implementation-dependent approximation to the sine of x. The argument is expressed in radians." (my italics).

具体来说,“sin(x)返回x的正弦的依赖于实现的近似值。参数以弧度表示。” (我的斜体)。

But my experimentation suggests that modern browsers currently only use one of two implementations, with Chrome being different to (and seemingly more accurate than) other browsers.

但我的实验表明,现代浏览器目前只使用两种实现中的一种,Chrome与其他浏览器不同(并且看起来比其他浏览器更准确)。