Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are browser differences in the last digit of a JavaScript Math function (atan2) within spec?

I'm seeing differences between Firefox and Safari in the last digit of the output from Math#atan2.

My code:

Math.atan2(-0.49999999999999994, 0.8660254037844387) 

Safari (12.1.1) gives -0.5235987755982988 but Firefox (Mac/67.0) gives -0.5235987755982987.

This is of course a tiny difference. However, it seems that all implementations should yield the same output across all inputs. A difference like this could, for example, cause an if statement to follow different paths depending on browser.

Does what I'm seeing violate any version of the ECMAScript spec?

like image 692
Rich Apodaca Avatar asked Jun 07 '19 01:06

Rich Apodaca


People also ask

What is atan2 in JavaScript?

atan2() method returns a numeric value between -π and π representing the angle theta of an (x, y) point. This is the counterclockwise angle, measured in radians, between the positive X axis, and the point (x, y) . Note that the arguments to this function pass the y-coordinate first and the x-coordinate second. Math.

What is Math function JavaScript?

The JavaScript Math is a built-in object that provides properties and methods for mathematical constants and functions to execute mathematical operations. It is not a function object, not a constructor. You can call the Math as an object without creating it because the properties and methods of Math are static.

Which of the following is a mathematical constant in JavaScript?

Math Constants in JavaScript: Math. E, Math. PI, and more.

What do the properties and methods of the Math object enable you to do?

The math object provides you properties and methods for mathematical constants and functions. Unlike other global objects, Math is not a constructor. All the properties and methods of Math are static and can be called by using Math as an object without creating it. Thus, you refer to the constant pi as Math.


1 Answers

The ECMAScript 2015 spec has this to say:

The behaviour of the functions acos, acosh, asin, asinh, atan, atanh, atan2, cbrt, cos, cosh, exp, expm1, hypot, log,log1p, log2, log10, pow, random, sin, sinh, sqrt, tan, and tanh is not precisely specified here except to require specific results for certain argument values that represent boundary cases of interest. For other argument values, these functions are intended to compute approximations to the results of familiar mathematical functions, but some latitude is allowed in the choice of approximation algorithms.The general intent is that an implementer should be able to use the same mathematical library for ECMAScript on a given hardware platform that is available to C programmers on that platform.

Although the choice of algorithms is left to the implementation, it is recommended (but not specified by this standard) that implementations use the approximation algorithms for IEEE 754-2008 arithmetic contained in fdlibm, the freely distributable mathematical library from Sun Microsystems (http://www.netlib.org/fdlibm).

The 5.1 spec has similar language.

So I think it's safe to say this behavior doesn't violate the spec.

like image 114
Mark Avatar answered Sep 19 '22 13:09

Mark