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 2005 Forums
 Transact-SQL (2005)
 Problem with floats/equality

Author  Topic 

jdolier
Starting Member

2 Posts

Posted - 2008-07-24 : 18:09:38
I am trying to run a while loop that goes from 0.0 to 2.0, so I used the following basic structure:

declare @counter float
declare @limit float

set @counter = 0.0
set @limit = 2.0

while (@counter <= @limit)
begin
-- Do stuff
set @counter1 = @counter1 + .01
end

For some reason, the @counter only gets to 1.99. When it gets to 2.0 it exits the loop. I know that I could simply increase the limit to make it work, but I want to know why this problem occurs. To help trouble shoot, I created two loops, one with equality and one without. It turns out that when I set the counter variable to 1.63 or higher, the equality loop works fine (goes to 2.0), but when I set it between 0.0 and 1.62 it only goes to 1.99. Why is this?

(I included my loop code below)

declare @counter1 float
declare @counter2 float
declare @limit float

set @counter1 = 1.62
set @counter2 = 1.62
set @limit = 2.0

while (@counter1 <= @limit)
begin
if (@counter1 > 1.9) print 'Loop w/ Equality: Counter(' + cast(@counter1 as varchar) + ')'
set @counter1 = @counter1 + .01
end

while (@counter2 < @limit)
begin
if (@counter2 > 1.9) print 'Loop w/o Equality: Counter(' + cast(@counter2 as varchar) + ')'
set @counter2 = @counter2 + .01
end

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-07-24 : 18:14:44
Don't use float data type, use decimal instead. Let us know if that resolved it.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

jdolier
Starting Member

2 Posts

Posted - 2008-07-24 : 20:16:22
Worked like a charm. Thank you!
Go to Top of Page
   

- Advertisement -