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

前端 未结 3 1512
鱼传尺愫
鱼传尺愫 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:31

    From wikipedia

    Single precision, called "float" in the C language family, and "real" or "real*4" in Fortran. This is a binary format that occupies 32 bits (4 bytes) and its significand has a precision of 24 bits (about 7 decimal digits).

    So your number doesn't actually fit in float. You can use double instead.

提交回复
热议问题