Why does a float variable stop incrementing at 16777216 in C#?

后端 未结 4 1129
南笙
南笙 2020-12-03 00:18
float a = 0;
while (true)
{
    a++;
    if (a > 16777216)
        break; // Will never break... a stops at 16777216
}

Can anyone explain this t

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-03 01:17

    16777217 cannot be represented exactly with a float. The next highest number that a float can represent exactly is 16777218.

    So, you try to increment the float value 16777216 to 16777217, which cannot be represented in a float.

提交回复
热议问题