Obiwahn
Obiwahn

Reputation: 3087

Math.sin() gives different results on server/client?

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

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

-9986.46139381927 on the server and

-9986.46115497749 on the client / browser / app

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.

Upvotes: 7

Views: 264

Answers (1)

Chris Lear
Chris Lear

Reputation: 6742

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.

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).

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.

Upvotes: 8

Related Questions