Float doesn't change when i add 0.1 to it

后端 未结 3 1071
感情败类
感情败类 2021-01-12 21:38

I am quite a newbie to c. So when i writing a small game demo, i face a really strange problem.

 void testC()
 {
     float a = 825300160;
     float b = a +         


        
3条回答
  •  迷失自我
    2021-01-12 22:11

    The fractional portion of a float consists of 23 bits. You need 30 bits to represent 825300160, so the less significant portion of the number is dropped. Adding .1 does not make a difference - you need to add roughly 32 for the number to change:

    float a = 825300160;
    float b = a + 31.5;
    assert(a != b); // No change is detected
    float c = a + 32;
    assert(a != c); // Change is detected
    

提交回复
热议问题