SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SET ROWCOUNT and table variable


SET ROWCOUNT and table variable

Author
Message
mm_sharma82
mm_sharma82
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 23
following script doesn't go in infinite loop
DECLARE @i float,@rc int
set @i = 0

while @i <> 1
begin
set @i = @i + 0.8
set @i = @i + 0.1
set @i = @i + 0.1
print @i
end

But below script is going in infinite loop
DECLARE @i float,@rc int
set @i = 0

while @i <> 1
begin
set @i = @i + 0.7
set @i = @i + 0.1
set @i = @i + 0.1
set @i = @i + 0.1

print @i
end


Can any one know the answer on this??

Regards
Manmohan
Open Minded
Open Minded
SSC Veteran
SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)

Group: General Forum Members
Points: 266 Visits: 567
Please try to see the trailing decimal digits using
 print convert(decimal(38,36),@i) 



I think the 1st example behaved well because it was dealing with an even number (8), while the 2nd does not because it was with odd numbers (7).

Because float comparison does not behave uniformly in all cases, I think this is why float comparison should not be used to test for equality, this is "equal", = and "not equal", <>.

But float may be used for less than or greater than, if accuracy/significance is required then the ideal less actual result may be used re: "while (ideal - actual) > 0.0001". The latter assumes the operations will converge close to the ideal point, otherwise there must be additional codes to check if divergence is happening in order to put a stop operation.
paul.knibbs
paul.knibbs
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2615 Visits: 6232
Gopinath Srirangan (8/27/2010)
Hi,
But another sample query as shown below gives expected result which contradicts from while loop.

DECLARE @i float
set @i = .9
set @i = @i + .1


You're only adding one float there, so the cumulative error isn't that great. In the original WHILE loop there were ten separate additions of 0.1, which caused more of an issue.
Steve Eckhart
Steve Eckhart
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1096 Visits: 8664
Actually, we're doing the exact same thing here. This was the original query:

while @i <> 1
begin
declare @a table(a int)
set @rc = @i + 0.9
set rowcount @rc
insert into @a select id from sysobjects
set @i = @i + 0.1
end



So, there was only one floating point addition which made @i<>1 TRUE, but in the if statement @i<>1 is FALSE.



Steve Eckhart
paul.knibbs
paul.knibbs
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2615 Visits: 6232
Steve Eckhart (8/27/2010)
Actually, we're doing the exact same thing here. This was the original query:

while @i <> 1
begin
declare @a table(a int)
set @rc = @i + 0.9
set rowcount @rc
insert into @a select id from sysobjects
set @i = @i + 0.1
end



So, there was only one floating point addition which made @i<>1 TRUE, but in the if statement @i<>1 is FALSE.


I don't see that. RC is set to @i + 0.9, then later 0.1 is added to @i. I'm not seeing the point where the value of @rc is copied into @i, which it would have to be for your statement to be correct. Maybe I'm just misreading the code?
Steve Eckhart
Steve Eckhart
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1096 Visits: 8664
Thanks, Paul. That was the part that I missed that caused the misunderstanding on my part. I got in my head that we were starting @i at 0.9, but we're not. We're starting @i at 0 and incrementing by 0.1 which causes us to "miss" 1.0.



Steve Eckhart
Michael Poppers
Michael Poppers
SSC Eights!
SSC Eights! (835 reputation)SSC Eights! (835 reputation)SSC Eights! (835 reputation)SSC Eights! (835 reputation)SSC Eights! (835 reputation)SSC Eights! (835 reputation)SSC Eights! (835 reputation)SSC Eights! (835 reputation)

Group: General Forum Members
Points: 835 Visits: 416
mccork (8/25/2010)
The answer was almost obvious from the "do not run it on production server" recommendation.
<<snip>>

Agreed (and frankly, I wouldn't have chosen the right answer otherwise :-))!
nisan.al
nisan.al
Valued Member
Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)

Group: General Forum Members
Points: 74 Visits: 164
I can't say i completely understand this issue.
The while statement dont not stop because @i will never be equal to 1
So how come the following select return 1 ?

DECLARE @i float

set @i = 0

set @i = @i + 0.2
set @i = @i + 0.2
set @i = @i + 0.2
set @i = @i + 0.2
set @i = @i + 0.2

SELECT 1
WHERE @i=1
Carlo Romagnano
Carlo Romagnano
SSCertifiable
SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)

Group: General Forum Members
Points: 5079 Visits: 3327
nisan.al (8/29/2010)
I can't say i completely understand this issue.
The while statement dont not stop because @i will never be equal to 1
So how come the following select return 1 ?

DECLARE @i float

set @i = 0

set @i = @i + 0.2
set @i = @i + 0.2
set @i = @i + 0.2
set @i = @i + 0.2
set @i = @i + 0.2

SELECT 1
WHERE @i=1

As said above, the increment of 0.2 (precise) is different from 0.1 (approximate)

I run on tuttopodismo
Hugo Kornelis
Hugo Kornelis
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11274 Visits: 12005
Carlo Romagnano (8/30/2010)
nisan.al (8/29/2010)
I can't say i completely understand this issue.
The while statement dont not stop because @i will never be equal to 1
So how come the following select return 1 ?

DECLARE @i float

set @i = 0

set @i = @i + 0.2
set @i = @i + 0.2
set @i = @i + 0.2
set @i = @i + 0.2
set @i = @i + 0.2

SELECT 1
WHERE @i=1

As said above, the increment of 0.2 (precise) is different from 0.1 (approximate)

No, that is not the reason. Neither 0.1, nor 0.2 can be represented precisely in binary floating point.
The binary equivalent of 0.2 is 0.000110001100011000110......
The binary equivalent of 0.1 is 0.000011000110001100011......

The real reason is that, with all rounding errors, you only see the effect if it stacks sufficiently. Because the calculation is done with some extra bits, most rounding errors remain invisible, especially becuase in random operations the rounding tend to even out each other. Repeating the same operation means repeating the same rounding error - and that will become visible when repeated often enough.

DECLARE @i float;

set @i = 0;

set @i = @i + 0.2;
set @i = @i + 0.2;
set @i = @i + 0.2;
set @i = @i + 0.2;
set @i = @i + 0.2;

SELECT 1
WHERE @i=1;

set @i = @i + 0.2;
set @i = @i + 0.2;
set @i = @i + 0.2;
set @i = @i + 0.2;
set @i = @i + 0.2;

SELECT 2
WHERE @i=2;


The first SELECT returns a value (as indicated by nisan.al), because the sum of the five rounding errors is so small that it has not yet reached the significant bits for the comparison. The second SELECT does not return a value, so obviously the sum of ten rounding errors does bring it into the significant bits.

A good way to visualize this, is to check the corresponding rounding errors when using decimal notation. Calculate 1/with one decimal place (0.3). Add it three times (0.9). Compare it to the integer 1 (0.9 rounds to 1, so conclusion is: 1 = the sum of three (1/3)).
Now add the same rounded 1/3 fifteen times more (5.4). Compare to the integer 6 (5.4 rounds to 5; not equal, so now the conclusion is that 6 <> the sum of eighteen (1/3)).

The flloating point calculations obviously use lots more siginificant digits, but so do the comparisons; the problem remains the same.


Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search