I am using sql server 2005 and .NET. I have a table with float column named "price". When I stores 1890 it store 1889.99966796875.
What is the reason?
I am using sql server 2005 and .NET. I have a table with float column named "price". When I stores 1890 it store 1889.99966796875.
What is the reason?
A floating point number is a special data type used for storing numbers of widely varying ranges. The trade off is in precision - which is what you are seeing as a problem. Floating point numbers
You may want to store your values with the decimal data type, or money instead.
The reason that floating numbers cannot be mapped to all numbers is because of the mismatch between the decimal and the binary system for fractions.
1889.99966796875 is the number that is the closest to 1890 in this floating point domain, so the closest this number can be reached using binary representation.
Other types, such as decimal and money, use other, much more memory consuming, techniques to store the number (for example , in a string you can store any number, but of course this is not the most performant way to do math)
a simple example : 0.3 in my own binary system :
0.1b (inary) would be 0.5 d (ecimal) so too much...
0.01b --> 0.25d (1/4 too little)
0.011 --> 0.375 (1/4 + 1/8 too much)
0.0101 --> 0.3125 (1/4 + 1/16 still too much)
...
0.010011 --> 1/4 +1/32 + 1/64 = 0.296875
Suppose my system has 6 bits to represent the fraction, 0.296875 would be the closest for this domain. The right number cannot be reached due to the decimal/binary system.
For examples see also : Floating point inaccuracy examples
And an excellent elaborate explenation of your problems is to be found here: http://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html
Another note : it is really about mismatch, not about 'quality' of systems : in decimal notation for example, you cannot represent 1/3 100% accurate, while this would be perfectly possible in other systems.
Any number that you want to store an exact number for or do math calculations on should be stored as decimal or number not float. This is an inexact data type and it will cause rounding errors and incorrect calculations. In general, float should be avoided.