跨不同浏览器的JavaScript中Math.pow()的奇怪结果 [英] Strange Result for Math.pow() in JavaScript across different Browsers

查看:116
本文介绍了跨不同浏览器的JavaScript中Math.pow()的奇怪结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许这只发生在Chrome最新版本中。

Maybe this only happens in chrome latest version.

Chrome浏览器中负指数值的一些奇怪行为

Some weird behavior for the negative exponent value only in the Chrome browser.

我已经检查过不同的浏览器&觉得很奇怪。正如 FireFox & Chromium 会显示完全相同的结果,而 Chrome最新版本会显示某些示例的不同结果。我不知道发生了什么事?

I already checked with different browsers & find it really weird. As FireFox & Chromium will show the exact same result while Chrome latest version will show the different result for some of the examples. And I don't know what's going on?

以下是我对不同浏览器的调查结果......

Here are my findings for different browsers...

Math.pow(10,-4) Math.pow(10,-5),答案应该是 0.0001 0.00001 分别为什么chrome的最新版本显示 0.00009999999999999999 0.000009999999999999999 分别!!

For the Math.pow(10,-4) and Math.pow(10,-5), the answer should be 0.0001 and 0.00001 respectively but why chrome's latest version shows0.00009999999999999999 and 0.000009999999999999999 respectively!!

任何人都可以解释上述情况为什么chrome会这样做?

Can anyone have an explanation for the above scenario that why chrome is doing like this?

FYI - 图片中已经提到了所有浏览器的版本。

FYI - All browser's version already mentioned in images.

推荐答案


返回将 base 提升到权力的结果的依赖于实现的近似值 exponent

Returns an implementation-dependent approximation of the result of raising base to the power exponent.

https://tc39.github.io/ecma262/#sec-applying-the-exp-operator

0.0001是下一个可表示的数字,高于0.00009999999999999999。 (结果相差一个最低精度单位。)

0.0001 is the next representable number above 0.00009999999999999999. (The results differ by one unit of least precision.)

这似乎在74.0.3700.0中有所改变( changelog ),对应 V8 roll 到7.4.113,包括此提交:

This appears to have changed in 74.0.3700.0 (changelog), corresponding to a V8 roll to 7.4.113, including this commit:


https://chromium.googlesource.com/v8/v8/+/98453126c109016c9d32c6ebd89dd83f69dd8efb

[builtins] [turbofan]重构Float64Pow使用单一实现

[builtins] [turbofan] Refactor Float64Pow to use single implementation

删除特定于平台的Float64Pow实现和实用程序Pow in
支持base :: ieee754 :: pow实现。

Remove platform-specific Float64Pow implementations and utils Pow in favor of a base::ieee754::pow implementation.

这统一了编译器的pow实现,w asm和
运行时。

This unifies the implementation of pow for the compiler, wasm, and runtime.

所以他们将Pow的实现转换为其他东西。

So they switched the implementation of Pow to something else.

为了证明我们得到不同的数字而且它与浮点到小数的转换无关:

To demonstrate that we're getting a different number and it's not related to the float-to-decimal conversion:

> 10**-4 == 1e-4

或者,如果你不相信并且想要探索低级别的浮动,将数字转储为十六进制:

Or, if you're not convinced and want to explore floats at a low level, dump the number as hex:

(需要Firefox 67。)

(Requires Firefox 67.)

from_bits = b => new Float64Array(new BigUint64Array([b]).buffer)[0]
to_bits = f => new BigUint64Array(new Float64Array([f]).buffer)[0]

console.log(to_bits(Math.pow(10,-4)).toString(16))

我在Firefox中获得3f1a36e2eb1c432d,在Chrome中获得3f1a36e2eb1c432c。

I get 3f1a36e2eb1c432d in Firefox and 3f1a36e2eb1c432c in Chrome.

这篇关于跨不同浏览器的JavaScript中Math.pow()的奇怪结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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