all groups > sql server programming > august 2007 >
You're in the

sql server programming

group:

Why float value will change when it is saved to DB


Why float value will change when it is saved to DB George Shui
8/26/2007 6:40:02 PM
sql server programming:
I feel it strange that after float value 1.19 saved to DB, and it becomes to
1.189999..., but not all the values has this problem, only a few transactions
will have this problem.

My Table Structure
ColumnnName float 8 not null.

My SP
Insert TableName(ColumnName)
select ColumnName
From openxml (@hDoc,'//Reply')
with (
ColumnName float './ColumnName'
)

Can anyone help me? Thanks in advance.
Re: Why float value will change when it is saved to DB George Shui
8/26/2007 7:54:01 PM

But I use the same table stucture and same SP in our Test and Dev
enviroment, No such thing happen. Also I put the same xml to excute in our
Test enviroment, it should be fine. Does it caused by enviroment difference
or it will be saved randomly.

[quoted text, click to view]
Re: Why float value will change when it is saved to DB Dan Guzman
8/26/2007 8:58:05 PM
Not all decimal values can be exactly represented by float and real data
types. The behavior follows the IEEE 754 specification and is why these a
labeled as approximate types in the Books Online.

If you need exact decimal values, use a decimal data type.

--
Hope this helps.

Dan Guzman
SQL Server MVP

[quoted text, click to view]
Re: Why float value will change when it is saved to DB Dan Guzman
8/27/2007 12:00:00 AM
[quoted text, click to view]

More accurately, SSMS will round the float value for display but Query
Analyzer, OSQL and SQLCMD will not. The underlying database value is the
same in both SQL 2000 and SQL 2005.

I'm glad I was able to help you out.

--
Dan Guzman
SQL Server MVP

[quoted text, click to view]
Re: Why float value will change when it is saved to DB George Shui
8/27/2007 5:18:03 AM
Thanks, you are right.

Because Sql server 2005 will format the value, but sql server 2000 will not.
So my eye has cheated me.

Thanks.

[quoted text, click to view]
Re: Why float value will change when it is saved to DB Dan Guzman
8/27/2007 6:28:07 AM
[quoted text, click to view]

I expect consistent results regardless of the environment as long as the
same value is inserted. The behavior may appear random because only certain
float values are affected For example:

SELECT CAST(1.19 as float) --1.1899999999999999
SELECT CAST(1.29 as float) --1.29

Also, tools like SSMS may format values for display purposes so you will see
1.19 in the GUI even though the underlying database value is
1.1899999999999999. SQLCMD will show the actual value:

SQLCMD -E -S MyServer -Q"SELECT CAST(1.19 AS float)"

--
Hope this helps.

Dan Guzman
SQL Server MVP

[quoted text, click to view]
Re: Why float value will change when it is saved to DB George Shui
8/27/2007 8:12:02 PM
Thank you so much. You are really handing.

But I have one more silly question, if I use SSMS, how can I get the value
as what I get by Query Analyzer 2000?

Thanks in advance.



[quoted text, click to view]
Re: Why float value will change when it is saved to DB George Shui
8/29/2007 7:48:01 PM
Thanks. Got it. Hope this function can be added in later version.

[quoted text, click to view]
Re: Why float value will change when it is saved to DB Dan Guzman
8/29/2007 9:31:51 PM
[quoted text, click to view]

Sorry, but I don't believe SSMS provides the option to change the display
behavior. You'll need to use SQLCMD in that case.

--
Hope this helps.

Dan Guzman
SQL Server MVP

[quoted text, click to view]
Re: Why float value will change when it is saved to DB Dan Guzman
8/30/2007 12:00:00 AM
[quoted text, click to view]

Me too. There is a suggestion on Connect for this but it current closed by
design. If this is important to you, consider voting accordingly:

http://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=126117

--
Hope this helps.

Dan Guzman
SQL Server MVP

[quoted text, click to view]
Re: Why float value will change when it is saved to DB Alex Kuznetsov
8/30/2007 7:48:20 AM
On Aug 27, 6:28 am, "Dan Guzman" <guzma...@nospam-
[quoted text, click to view]

Dan, I think it is not random when you consider how these numbers look
in binary format.

Alex Kuznetsov, SQL Server MVP
http://sqlserver-tips.blogspot.com/
Re: Why float value will change when it is saved to DB George Shui
8/30/2007 10:00:02 PM
Thank you all , Dan has made me understood. Float value is stored in a
arthimatic expression, and its value is caculated in run time. It that right?
So it should not be radom, because the same number is converted to the same
expression.

Please tell me if I'm wrong. Thanks.

[quoted text, click to view]
Re: Why float value will change when it is saved to DB Dan Guzman
8/30/2007 10:28:22 PM
[quoted text, click to view]

I agree. I was trying to convey to George that float behavior is not random
but might appears so because not all values are affected.

--
Hope this helps.

Dan Guzman
SQL Server MVP

[quoted text, click to view]
Re: Why float value will change when it is saved to DB George Shui
8/31/2007 6:32:05 AM
Great, Thank you. In this discussion, I learn more than expected.

[quoted text, click to view]
Re: Why float value will change when it is saved to DB Dan Guzman
8/31/2007 6:52:59 AM
[quoted text, click to view]

I'm not sure whether or not your understanding is complete. I think it is
more correct to say that the specified decimal value is converted to float
at run time and converting that float value back to decimal will not
necessarily yield the original decimal value. You can read more about IEEE
754 at http://en.wikipedia.org/wiki/IEEE_754. Here is the relevant exceprt
from the Books Online:

<Excerpt
href="ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/udb9/html/686eb8a8-fc51-4118-9ee2-3c2db9998f06.htm">

The float and real data types are known as approximate data types. The
behavior of float and real follows the IEEE 754 specification on approximate
numeric data types.

Approximate numeric data types do not store the exact values specified for
many numbers; they store an extremely close approximation of the value. For
many applications, the tiny difference between the specified value and the
stored approximation is not noticeable. At times, though, the difference
becomes noticeable. Because of the approximate nature of the float and real
data types, do not use these data types when exact numeric behavior is
required, such as in financial applications, in operations involving
rounding, or in equality checks. Instead, use the integer, decimal, money,
or smallmoney data types.

Avoid using float or real columns in WHERE clause search conditions,
especially the = and <> operators. It is best to limit float and real
columns to > or < comparisons.

The IEEE 754 specification provides four rounding modes: round to nearest,
round up, round down, and round to zero. Microsoft SQL Server 2005 uses
round up. All are accurate to the guaranteed precision but can result in
slightly different floating-point values. Because the binary representation
of a floating-point number may use one of many legal rounding schemes, it is
impossible to reliably quantify a floating-point value.

</Excerpt>

[quoted text, click to view]

Correct.

--
Hope this helps.

Dan Guzman
SQL Server MVP

[quoted text, click to view]
AddThis Social Bookmark Button