views:

503

answers:

3

Precision loss is one thing, but precision gain???

I have a text file w/ the following coordinates:

41.88694340165634   -87.60841369628906

When I paste this into SQL Server Mgmt Studio table view, it results in this:

41.886943401656339  -87.608413696289062

Am I dreaming? How is this possible?

I'm pasting from notepad, and it's raw text. Same problem if I type the characters directly.

Where does sql server get the extra precision from?

+3  A: 

According to Books-On-Line:

Float: Approximate-number data types for use with floating point numeric data. Floating point data is approximate; therefore, not all values in the data type range can be represented exactly.

Emphasis mine.

G Mastros
you're missing the point of the issue..it's ADDING precision, not SUBTRACTING it. Where could it possibly be getting the extra decimal places from? It's just pulling them out of the sky.
Scott
@Scott A float can not hold EXACTLY 41.88694340165634, so the float becomes something close such as 41.886943401656339.
Shannon Severance
A: 

I haven't personally seen this, but it might just be that the SQL server management studio shows the "representation" of the float i.e. how the value would be stored in the db. Remember that floats are all approximate anyway.

nagul
but that would explain the phenomenon of precision LOSS, not precision ADDITION.
Scott
Not really. As @hova points out, it's not precision per se. It's to do with how floats are stored. Since 41.88694340165634 cannot be stored in the bits available for a float, it stores the nearest avabilable representation. Have a look here to understand how floats are internally represented: http://steve.hollasch.net/cgindex/coding/ieeefloat.html
nagul
Consider a hypothetical situation:Let's assume that 1.0 cannot be stored in the x bits a hypothetical flaot has. What can we do then ? We can store the closest number to 1 that can be put in a flaot, which would be 0.99999 or 0.999 or 0.9 or 1.000001 or some other such approximation. The important thing here is that we're not increasing precision, just storing the nearest value to 1.0 we can. Hence, flaot is an "approximate" datatype, same as float.
nagul
+5  A: 

It's not adding precision, it's just rounding it to the nearest IEEE floating point representation. When you convert that back to decimal, it only LOOKS like it gained precision.

hova
thanks for the insights, makes sense now.
Scott