Strange Result for Math.pow() in JavaScript across different Browsers

后端 未结 1 774
遥遥无期
遥遥无期 2021-01-22 11:17

Maybe this only happens in chrome latest version.

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

相关标签:
1条回答
  • 2021-01-22 12:07

    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 is the next representable number above 0.00009999999999999999. (The results differ by one unit of least precision.)

    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] Refactor Float64Pow to use single implementation

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

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

    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:

    (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))

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

    0 讨论(0)
提交回复
热议问题