Why does the addition of two float numbers is incorrect in C?

前端 未结 3 1505
鱼传尺愫
鱼传尺愫 2021-01-19 08:03

I have a problem with the addition of two float numbers. Code below:

float a = 30000.0f;
float b = 4499722832.0f;

printf(\"%f\\n\", a+b);

3条回答
  •  南方客
    南方客 (楼主)
    2021-01-19 08:30

    Floating-point values cannot represent all integer values.

    Remember that single-precision floating-point numbers only have 24 (or 23, depending on how you count) bits of precision (i.e. significant figures). So as values get larger, you begin to lose low-end precision, which is why the result of your calculation isn't quite "correct".

提交回复
热议问题