1

I had divided 9501/100.0f expecting to get result of 95.01f, but for some deviant reason the result was 95.01000000002f.

I am aware of rounding errors and also that dividing two bigger floats can give improper result, but these two numbers are relative small, and they should not give bad answer.

I have changed floats to doubles, only to see the same result.

So my answer is, why am I seeing this false output?

And eventually workaround without copying number to string and back.

Izzy
  • 402
  • 6
  • 16
  • 7
    Someone else has discovered the magical properties of floating point arithemtic. This is a coming of age moment. – Tim Seguine Dec 18 '13 at 20:49
  • 2
    This is just a usual rounding error. – Jaa-c Dec 18 '13 at 20:50
  • Floating point has a hard time with dividing accurately by anything that is not a power of 2. You have just noticed that 100 is indeed not a power of 2. – Tim Seguine Dec 18 '13 at 20:55
  • 0.1 in binary is 1/2 + 1/4 + 1/8 + 1/16 + .. + 1/32876, 0.1 recurring in base2, no way to represent it. like trying to do a 1/3 in decimal. – Tony Hopkinson Dec 18 '13 at 21:13

3 Answers3

6

Floating point numbers are not precise, and dealing with them has lots of idiosyncrasies.

What Every Computer Scientist Should Know About Floating-Point Arithmetic

I also enjoy Bruce Dawson's blog entries on floating point values.

josh poley
  • 7,236
  • 1
  • 25
  • 25
1

Floating point numbers are numbers represented in binary with limited precision.

The error between expected result and actual result is caused by the fact, that the number 95.01 is infinitely periodical in binary representation.

Double has only 51 binary digits, thus there has to be some rounding before the number is stored in the double precision. Single precision has only 23 digits.

It is not possible to represent 95.01 in finite precision floatin point number without any error. However, you may trust the first 6-9 decimal digits, thus you should format the number with some meaningfull format.

V-X
  • 2,979
  • 18
  • 28
0

Ahh good, another one of us has become a man in the church of programming :)

Floating points are not exact, the precision will vary from machine to machine. 1.0f != 1.00000000000000000000000000000000000 and so on, it's more like 1.0000001002003400011 and so on (I just picked arbitrary numbers here).

MGZero
  • 5,812
  • 5
  • 29
  • 46
  • Um, no, 1.0f is 1.0000000... with any sensible floating-point implementation. Do you know of one where it isn't? – Pete Becker Dec 19 '13 at 00:42