Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Float Error?

Author  Topic 

thangnc
Starting Member

7 Posts

Posted - 2013-05-13 : 01:01:40
Hi all!
My code:
declare @f1 float, @f2 float, @f float;
select @f1 = 0.3, @f2 = 0.3
set @f = @f1 + @f2
select @f +=@f1
select @f = @f -0.9
select @f
----------------
result @f:
-1.11022302462516E-16
why @f not = 0?
How to do @f = 0?

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-05-13 : 01:14:02
You can use dec(p,s) data type for this kind of arithmetic calculations

refer this link to know the reason behind the above result
http://sqlblog.com/blogs/hugo_kornelis/archive/2007/10/17/so-called-exact-numerics-are-not-at-all-exact.aspx

--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-13 : 01:18:30
float and real are only approximate numerical data types. so they actually precision may differ from what you see. SO if you want to deal with accurate numeric data use decimal or numeric

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -