views:

420

answers:

2

I have a table that contains a float column.

There are many records in that table.

When I do a Group By it shows that the float column has only the values: 0.1 0.2 0.25 0.5 1 2 3

But when I do a SUM it results in: 58.350000000000087

Where does the 0000000000087 come from? Is it because I'm using a float type instead of a decimal/numeric? I read some forum posts about this but the math hurts my head. What would be the correct way to get accurate results?

+10  A: 

IEEE 754 (and related) floating point numbers cannot accurately represent many decimal values. When you perform arithmetic operations, such as summation, you sometimes end up with values that cannot be well represented, and the error occurs.

Please see this article for more info: What Every Computer Scientist Should Know About Floating-Point Arithmetic

Depending on the SQL server you are using, there are numeric types that are precise for decimal values, and those that aren't. For example, as you have found, float is not precise for decimals. However, money and decimal are.

Eilon
+1 - exactly. Everyone should read that article, once a year like taking medicine.
duffymo
"float is not precise. However, money and decimal are." I cringe when I read such rubbish. Float is precise for expressing fractions of powers of two, up to a given number of digits. For example, 1.25 can be expressed precisely in float. "decimal" will work well with decimal fractions, but neither of those will be good to precisely express 1/3 or 1/7.
quant_dev
Thanks for the comment - I updated the answer to say "precise for decimals".
Eilon
+2  A: 

0.1 and 0.2 cannot be expressed precisely as binary floating-point numbers, hence you get a small "surplus".

quant_dev
Good explanation in simple terms. Thanks!
metanaito