为什么 C# 中的 float 变量在 16777216 处停止递增?

2024-01-01

float a = 0;
while (true)
{
    a++;
    if (a > 16777216)
        break; // Will never break... a stops at 16777216
}

谁能向我解释一下为什么这段代码中浮点值在 16777216 处停止递增?

Edit:

或者更简单:

float a = 16777217; // a becomes 16777216

我对 IEEE-754 浮点数(32 位)的简短总结:

  • 1位符号(0表示正数,1表示负数)
  • 8 位指数(带有 -127 偏差,这里不重要)
  • 23 位“尾数”
  • With exceptions for the exponent values 0 and 255, you can calculate the value as: (sign ? -1 : +1) * 2^exponent * (1.0 + mantissa)
    • 尾数位代表binary digits after小数点分隔符,例如1001 0000 0000 0000 0000 000 = 2^-1 + 2^-4 = .5 + .0625 = .5625小数分隔符前面的值不会被存储,而是隐式假定为 1(如果指数为 255,则假定为 0,但这在这里并不重要),因此,例如,对于指数 30,此尾数示例表示该值1.5625

现在以你的例子为例:

16777216 is exactly 224, and would be represented as 32-bit float like so:

  • 符号 = 0(正数)
  • 指数 = 24(存储为 24+127=151=10010111)
  • 尾数 = .0
  • 作为 32 位浮点表示:0 10010111 00000000000000000000000
  • 因此: 值 =(+1) * 2^24 * (1.0 + .0) = 2^24 = 16777216

Now let's look at the number 16777217, or exactly 224+1:

  • 符号和指数相同
  • mantissa would have to be exactly 2-24 so that (+1) * 2^24 * (1.0 + 2^-24) = 2^24 + 1 = 16777217
  • And here's the problem. The mantissa cannot have the value 2-24 because it only has 23 bits, so the number 16777217 just cannot be represented with the accuracy of 32-bit floating points numbers!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么 C# 中的 float 变量在 16777216 处停止递增? 的相关文章

随机推荐