gpt4 book ai didi

javascript - 为什么 chrome 和 safari 为 Math.pow(2, -1075) === 0 打印出 'true'

转载 作者:行者123 更新时间:2023-11-30 06:19:17 26 4
gpt4 key购买 nike

来自这篇文章 How numbers are encoded in JavaScript ,

An exponent of 0 is used in two capacities. First, if the fraction is also 0 then the whole number is 0.

这似乎表明 Math.pow(2, -1023) === 0 应该返回 true,但在 chrome 和 safari 中测试后它们都打印出 ' true' for Math.pow(2, -1075),谁能帮忙解释一下这是为什么?

最佳答案

Javascript 在幕后使用 64 位 IEEE 754 float 。这样的数字只有 11 位专门用于指数。这 11 位用于以 2 的补码表示法对有符号整数进行编码。只要说 -1075 超出范围就够了。这是为您提供的维基百科摘录:

The exponent field can be interpreted as either an 11-bit signed integer from −1024 to 1023 (2's complement) or an 11-bit unsigned integer from 0 to 2047, which is the accepted biased form in the IEEE 754 binary64 definition. If the unsigned integer format is used, the exponent value used in the arithmetic is the exponent shifted by a bias – for the IEEE 754 binary64 case, an exponent value of 1023 represents the actual zero (i.e. for 2e − 1023 to be one, e must be 1023). Exponents range from −1022 to +1023 because exponents of −1023 (all 0s) and +1024 (all 1s) are reserved for special numbers. [ref]

关于javascript - 为什么 chrome 和 safari 为 Math.pow(2, -1075) === 0 打印出 'true',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54175014/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com