views:

276

answers:

2

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

Whereas 99.99 * 0.01 = 0.99

Clearly this is the age old floating point rounding issue, however the rounding error in this case seems quite large to me; what I mean is I might have expected a result of 0.99990000001 or some similar 'close' result.

And for the record I got the same answer in a JavaVM and in a .Net environment.

+5  A: 

Why 0.9998999999999999 is not close enough for you (and 0.9999000000000001 is good )?
abs(0.9999 - 0.9998999999999999) == abs (0.9999 - 0.9999000000000001)

a1ex07
Ahh yes, 0.999*8*Well spotted.
locster
+1  A: 

It is a floating point error. For (a lot) more detail see this wikipedia article. Use a decimal if you need accurate fractional results (see here).

Jeremy Roberts
Or, more precisely, use a `decimal` if you are doing calculations where the **decimal representation** of the result is important.
Daniel Pryden
Daniel is right: decimal representation isn't much more accurate than binary. It does however match the arithmetic we usually use, so when exact matches to existing processes are necessary (like in a lot of financial work), use decimal.
David Thornley