1

Possible Duplicate:
Dealing with accuracy problems in floating-point numbers

Code:

float flt = 78983636;  // stores 78983632. as seen in debugger
flt = 79282056;        // stores 79282056.

Why is it not able to accurately store 1st value, but able to for the 2nd value which is larger?

Community
  • 1
  • 1
dlsou
  • 635
  • 2
  • 8
  • 18
  • "Larger" has little to do with this. Please see the answer I gave to another question (it's about Java and doubles, but the same concepts apply): http://stackoverflow.com/questions/7811112/why-is-long9223372036854665200d-giving-me-9223372036854665216/7811169#7811169 – NPE Dec 14 '11 at 16:15
  • 6
    [What Every Computer Scientist Should Know About Floating-Point Arithmetic](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) – Joe Dec 14 '11 at 16:18
  • 1
    Or to make a simple example: Assume you can only store a `n` as integer and it represents `10^n`. Now you can perfectly store 1000, 10.000 and so on but will have a hard time representing even 5 correctly. Similar principle for floats - it just gets a bit more complicated since we want to store more than just 2^something . – Voo Dec 14 '11 at 18:05

1 Answers1

6

A typical 32-bit float has about 24 bits of integer precision. That you're able to store your value precisely even though it's larger is simply because it hits a sweet spot that looks proper as an integer, while the other one does not.

Specifically the number 79282056 is stored as 1.1813947 * 226, or 79282056.2526208. So it's not exact, there's some digits after the decimal point that you omitted.

You can use this converter to inspect the mantissa, sign and exponent used to store any floating-point number.

unwind
  • 391,730
  • 64
  • 469
  • 606