Carl Shapiro (2014-07-28T17:49:15.000Z)
domenic at domenicdenicola.com (2014-08-07T15:57:12.248Z)
In [ECMA-262](http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf), section 15.8.2, the note allows implementations to choose appropriate algorithms for the evaluation of the special functions and it is recommended but not required to use the algorithms from fdlibm <http://netlib.org/fdlibm>. Since this is a recommendation and not a requirement implementations compute incorrect results for some values. This produces things where `Math.cos(Math.pow(2,120))` doesn’t even have the correct sign or basic identities like sin(-x) = -sin(x) don’t hold for all finite values of x. [This spreadsheet](https://docs.google.com/spreadsheets/d/1t2jrptAvaQetDIYPD8GKc90Dni2dT3FuHgKKFF-eJHw/edit?usp=sharing) gives some results from various browsers on some selected functions. This lack of precision makes it very difficult to port numerical applications from C or Java to Javascript. It also forces every serious numerical Javascript application to test against every browser and platform for correct behaviour. This seems a major disservice to the web platform and Javascript in particular. Since the specification recommends using the algorithms from fdlibm, which, I believe produces results that are accurate to < 1 ulp, why not make this a requirement? As the spreadsheet shows, many browsers already achieve correct results. Porting fdlibm to Javascript is not particularly difficult provided a couple of key routines are available. (My colleague has done this for the trig functions, except for the hairy case of the Payne-Hanek pi reduction routine.) Note also that [Java requires that many special function be accurate to < 1 ulp](http://docs.oracle.com/javase/8/docs/api/java/lang/Math.html). Specifying a similar requirement for Javascript should not be too onerous on existing implementations. Java is an existence proof that these requirements can work. While having an accuracy requirement is good in itself, it’s also important that the functions are semi-monotonic to match the mathematical functions. This is also a requirement in Java. It is known that applications using divided differences behave incorrectly when functions are not monotonic when they should be.