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

时间:2023-05-05
本文介绍了为什么浮点变量在 C# 中在 16777216 处停止递增?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..
float a = 0;
while (true)
{
    a++;
    if (a > 16777216)
        break; // Will never break... a stops at 16777216
}

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

Can anyone explain this to me why a float value stops incrementing at 16777216 in this code?

或者更简单:

float a = 16777217; // a becomes 16777216

推荐答案

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

Short roundup of IEEE-754 floating point numbers (32-bit) off the top of my head:

  • 1 位符号(0 表示正数,1 表示负数)
  • 8 位指数(带 -127 偏差,此处不重要)
  • 23 位尾数"
  • 除指数值 0 和 255 外,您可以将值计算为:(sign ? -1 : +1) * 2^exponent * (1.0 + mantissa)
    • 尾数位表示二进制小数分隔符,例如1001 0000 0000 0000 0000 000 = 2^-1 + 2^-4 = .5 + .0625 = .5625 不存储小数点分隔符前面的值,而是隐式假定为 1 (如果指数为 255,则假定为 0,但这在这里并不重要),因此对于 30 的指数,例如,这个尾数示例表示值 1.5625
    • 1 bit sign (0 means positive number, 1 means negative number)
    • 8 bit exponent (with -127 bias, not important here)
    • 23 bits "mantissa"
    • With exceptions for the exponent values 0 and 255, you can calculate the value as: (sign ? -1 : +1) * 2^exponent * (1.0 + mantissa)
      • The mantissa bits represent binary digits after the decimal separator, e.g. 1001 0000 0000 0000 0000 000 = 2^-1 + 2^-4 = .5 + .0625 = .5625 and the value in front of the decimal separator is not stored but implicitly assumed as 1 (if exponent is 255, 0 is assumed but that's not important here), so for an exponent of 30, for instance, this mantissa example represents the value 1.5625

      现在来看你的例子:

      16777216 正好是 224,并且会像这样表示为 32 位浮点数:

      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

      现在让我们看看数字 16777217,或者正好是 224+1:

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

      • 符号和指数相同
      • 尾数必须正好是 2-24,这样 (+1) * 2^24 * (1.0 + 2^-24) = 2^24 + 1 = 16777217
      • 问题就出在这里.尾数不能有值 2-24 因为它只有 23 位,所以数字 16777217 不能用精度表示32 位浮点数!
      • sign and exponent are the same
      • 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!

      这篇关于为什么浮点变量在 C# 中在 16777216 处停止递增?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!