Sunday, March 11, 2012

7088.03 - 7088.03 = -9.09494701772928E-13

I've been having odd problems like this recently.
I will do a calculation on my data and get a value (for example 7088.03) and compare it to a previous value in another table (also 7088.03) and I want the difference of the two but I get a number that is non-zero (in this case: -9.09494701772928E-13) for equivalent values.

Does anyone know what the cause is? I've already checked to make sure the datatypes are the same.

The reason is that you are using float / real data types. These are approximate data types and follow the IEEE 754 specification. So depending on the operation you are doing or arithmetic manipulation it is possible that you get the same value with different precisions. This is expected and by design. If you need exact scale/precision then you should use data type like decimal or numeric. See BOL for more details on using float/real starting with topic below:

http://msdn2.microsoft.com/en-us/library/ms187912.aspx

No comments:

Post a Comment