Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does node not evaluate Math.tan(Math.PI/2) to Infinity but Chrome V8 does?

Tags:

When running this in a node command-line interface:

> Math.tan(Math.PI/2)
16331778728383844 

But in Chrome:

> Math.tan(Math.PI/2)
Infinity

Aren't both using the same V8 engine?

Node's result is not even equal to the maximum "integer" value in JavaScript.

like image 549
Ludovic C Avatar asked Jun 27 '14 15:06

Ludovic C


People also ask

How to find tangent in JavaScript?

tan() method in Javascript is used to return the tangent of a number. The Math. tan() method returns a numeric value that represents the tangent of the angle. The tan() is a static method of Math, therefore, it is always used as Math.


4 Answers

If you look at the v8 implementation of the Math object, you see:

function MathTan(x) {
  return MathSin(x) / MathCos(x);
}

Indeed, Math.cos(Math.PI/2) returns an unusual value in Node as well (in fact, the reciprocal of your unusual Math.tan result):

> Math.cos(Math.PI/2)
6.123031769111886e-17  // in Chrome, this is 0

So, your question reduces to: Why is Math.cos(Math.PI/2) non-zero in Node <=0.10.24?

This is difficult to answer. The implementation of sine and cosine are supplied by a math-heavy function called TrigonometricInterpolation, which relies on a reverse lookup table of 1800 sample values generated by C++ code, code which is itself generated a Python script when v8 is first installed.

It is also worth noting, however, that the current trig lookup table code very recently replaced an older lookup table, so the current release of Node may not be using the most recent trig lookup table (since new code arrived in v8 on Nov. 22, 2013, but the only pull from v8 into Node prior to the 0.10.24 release in December 2013 was on Nov 11, 2013, eleven days prior to the change). Chrome probably is using up-to-date code, while current stable Node is using different trigonometric code.

like image 155
apsillers Avatar answered Sep 19 '22 04:09

apsillers


If you type:

Math.PI/2

Do you get exactly π/2? Nope ;)

Therefore, it can't "accurately" calculate Math.tan(Math.PI/2) as being Infinity because it doesn't have the precision for Math.PI/2.

But in some cases (such as Chrome), the loss of precision is so small that it gets Infinity anyway.

To illustrate this, take a look at this console output:

Math.PI/2  
> 1.5707963267948966  

Math.tan(1.5707963267948964)  
> 5039790063769915  

Math.tan(1.5707963267948965)  
> Infinity  

Math.tan(1.5707963267948966)  
> Infinity  

Math.tan(1.5707963267948967)  
> -5039790063769915

Notice how there are actually two values that result in Infinity? That's the inaccuracy.

like image 44
Niet the Dark Absol Avatar answered Sep 20 '22 04:09

Niet the Dark Absol


The thing is that, server has different settings from a browser. Infinity is the variable "Number.POSITIVE_INFINITY" but if you check out another variable, the "Number.MAX_INTEGER" my chrome gives:

console.log( Number.MAX_INTEGER ) // prints 9007199254740991

and 9007199254740991 is smaller than 16331778728383844, so probably chrome decide every number bigger than Number.MAX_INTEGER to be Infinity.

On node js console.log( Number.MAX_INTEGER ) // prints 1.7976931348623157e+308

Chrome and node js has different up and down limits for Numbers.

To sum up, on nodejs Number.MAX_INTEGER is bigger than Math.tan(Math.PI/2) while chrome's Number.MAX_INTEGER is smaller than Math.tan(Math.PI/2) .

So nodejs see a number while chrome see Infinity.

like image 31
GramThanos Avatar answered Sep 20 '22 04:09

GramThanos


With HEAD version

node -v
v0.11.14-pre

node
> Math.tan(Math.PI/2)
Infinity

https://github.com/joyent/node/issues/7852

like image 44
Ludovic C Avatar answered Sep 22 '22 04:09

Ludovic C