为什么node不将Math.tan(Math.PI / 2)评估为Infinity但Chrome V8会这样做? [英] Why does node not evaluate Math.tan(Math.PI/2) to Infinity but Chrome V8 does?

查看:165
本文介绍了为什么node不将Math.tan(Math.PI / 2)评估为Infinity但Chrome V8会这样做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在节点命令行界面中运行时:

When running this in a node command-line interface:

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

但在Chrome中:

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

两者都没有使用相同的V8引擎?

Aren't both using the same V8 engine?

节点的结果甚至不等于最大整数值。

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

推荐答案

如果你看一下 v8实现 Math 对象,你看:

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

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

确实, Math.cos(Math.PI / 2)在Node中返回一个异常值(实际上,你的异常 Math.tan 结果的倒数):

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

所以,你的问题简化为:为什么 Math.cos(Math.PI / 2)在Node< = 0.10中非零。 24?

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

这很难回答。正弦和余弦的实现由称为 TrigonometricInterpolation ,它依赖于由C ++代码生成的1800个样本值的反向查找表,代码本身生成首次安装v8时的Python脚本

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.

然而,值得注意的是,当前trig查找表代码最近更换了旧的查找表,因此当前版本的Node可能没有使用最新的trig查找表(因为新代码于2013年11月22日到达v8,但在2013年12月0.10.24发布之前,从v8到节点的唯一拉动是2013年11月11日,即更改前11天)。 Chrome可能使用的是最新代码,而当前稳定的Node使用的是不同的三角代码。

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.

这篇关于为什么node不将Math.tan(Math.PI / 2)评估为Infinity但Chrome V8会这样做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆