Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 458
Obiwahn Avatar asked Oct 27 '15 16:10

Obiwahn


People also ask

What does Math sin do?

The Math. sin() function returns the sine of a number in radians.

Is Math sin in radians or?

The sine is equal to the ratio of the side opposite the angle and the hypotenuse of the right-angled triangle they define. The angle must be measured in radians.


1 Answers

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.

like image 104
Chris Lear Avatar answered Oct 15 '22 09:10

Chris Lear