SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Float Error?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

thangnc
Starting Member

7 Posts

Posted - 05/13/2013 :  01:01:40  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

India
2223 Posts

Posted - 05/13/2013 :  01:14:02  Show Profile  Reply with Quote
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

India
52325 Posts

Posted - 05/13/2013 :  01:18:30  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.04 seconds. Powered By: Snitz Forums 2000