• Mike John (10/18/2013)


    Float is not an exact data type. The internal storage format can mean rounding is being done that may not be visible to you. It would be interesting to get the exact internal value for both of these, just retrieve the varbinary conversion of them both and compare that. I expect you will see they are very slightly different.

    As a general rule using float for monetary values is not a good idea, I would either use money (if the maths rules it uses are OK for you) or more likely numeric or decimal with an appropriate scale and precision for your needs.

    Mike John

    Yes, the problem is most likely happening because FLOAT is not an exact numeric. And the solution (assuming exact numeric arithmetic is accurate enough - "exact" in the name refers to accuracy of representation of base 10 fractions, not to the accuracy of long chains of calculation) and has adequate range) would be to switch to using an exact numeric representation. If that isn't possible, just cast the numbers to an appropriate exact numeric type before comparing them and see if that works.

    But the problem here may not be rounding; rounding in FLOAT generally introduces much smaller errors than rounding in exact numeric types (which usually throw away quite a lot of accuracy whenever they put the result of a computation into a field of predefined and precision). This doesn't make any difference to what the solution is, until we have a version of SQL that supports modern floating point standards. FLOAT does introduce a particular rounding error which decimal doesn't because SQL hasn't kept up with the recognised international standard on floating point, the representation error introduced by using a binary base for numbers which were expressed originally in base 10 - for example a quantity like 0.2 has no finite binary representation,so it's going to be rounded to something that does have a finite (53 bit, in the case of float) binary representation and that could be the source of the problem; but it's just as likely that the floating point arithmetic that calculated the numbers, because it doesn't introduce fairly large rounding errors every time it tores a value, has produced more accurate answers than exact numerics would and that the error is that the FLOAT rounding error is not big enough. Since the two values were calculated down different paths, the total rounding error will probbly be different in the two cases, which means the two answers will be different unless you round them to something a bit less accurate.

    Tom