Domanda

Part of one of my UDFs has a section that tries to find the number of decimal places after a float and returns the number multiplied up by powers of 10 such that it's an integer. Using 1.0005 as an example, here's the segment:

DECLARE @num FLOAT = 1.0005
DECLARE @exponent AS INT = 0

WHILE @num - FLOOR(@num) > 0.000001
BEGIN
SET @exponent += 1
SET @num *= 10
END

When I debug this in SSMS 2012, on the 'while' line if I evaluate ? CASE WHEN @num - FLOOR(@num) > 0.000001 THEN 1 ELSE 0 END in the immediate window on its fourth iteration onwards I get 0 - however, the loop keeps running indefinitely.

Any ideas?

È stato utile?

Soluzione

I tried this, to see what is going on ... @z doesn't change ...

DECLARE @num FLOAT = 1.0005
DECLARE @exponent AS INT = 0
declare @x float
declare @z float

WHILE @num - FLOOR(@num) > 0.000001
BEGIN
SET @exponent += 1
SET @num *= 10
set @x = FLOOR(@num)
set @z = @num - FLOOR(@num)
END
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top