2

When I perform the following code, it has a problem:

float number = 999999999;
printf("%lf", number);

The result is 10000000000 instead of 999999999.

Why isn't it 999999999?

P.P
  • 117,907
  • 20
  • 175
  • 238

1 Answers1

8

Typical float can represent exactly about 232 different numbers like 1234.0 and -0.125. 999999999 is not one of them. 10000000000.0f is the closest float alternative.

The approximation occurred during the assignment

float number = 999999999;  // Really assigned 10000000000.0f
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • Thanks for your answer,but i don't know the mens about "Typical float can represent exactly about 232 different numbers like 1234.0 and -0.125. 999999999 is not one of them. ", can you show me more about it ? – zhaoguangliang Jan 03 '17 at 02:35
  • @zhaoguangliang `float` is typically 4 bytes, than is, at most, 4,294,967,296 different floating point numbers. Yet you can code `float number = endless_different_decimal_values_in_text;`. Sometimes code will use a number that is not representable as 1 of those 4 billion. What do you think should happen then? – chux - Reinstate Monica Jan 03 '17 at 02:42