0

i would like to know what is my problem when i try to print some float values, for example, in this simple programme :

float n = 127.998 ;
printf("%f",n);

The execution gives : 127.998001.

  • So why i have the additional 1 back of this number ?

attached image

Community
  • 1
  • 1
hicham ATR
  • 37
  • 5
  • See also http://stackoverflow.com/questions/12635537/c-floating-point-precision – Ari0nhh Aug 02 '16 at 07:17
  • 2
    Obligatory [What Every Computer Scientist Should Know About Floating-Point Arithmetic](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) – flau Aug 02 '16 at 07:52

1 Answers1

0

Some values cannot be accurately stored in a floating point data type. There is no guarantee that your float n = 127.998 will actually be stored as exactly 127.998 . For values that cannot be accurately represented in floating point types, the closest value to that is stored instead, which is what you got.

Magisch
  • 7,312
  • 9
  • 36
  • 52