26 October 2007 16:07
Alex_Kuznetsov
When SUM of Six Floats Depends on Order of Adding
Following the discussion started by Hugo Kornelis and Old Grumpy DBA, an example of inexact calculations with float datatype:
declare @big float, @small float, @sum1 float, @sum2 float, @sum3 float, @i INT
SELECT @big = 12345678901234.50, @small = 0.01, @i = 0
SELECT @sum1 = @big, @sum2 = 0, @sum3 = 0
WHILE @i < 5 BEGIN
SELECT @sum1 = @sum1 + @small, @sum2 = @sum2 + @small, @sum3 = @sum3 + @small
SET @i = @i + 1
END
SELECT @sum2 = @sum2 + @big
SELECT @sum1, @sum2, @sum3
---------------------- ---------------------- ----------------------
12345678901234.5 12345678901234.6 0.05
(1 row(s) affected)
This is why you cannot have sums of floats in indexed views - they are not deterministic, they may depend on order of adding.