Reputation: 109
Why do I get different results from this two queries?
First query:
DECLARE @b FLOAT;
SET @b = 100
SELECT @b / 3 + @b / 3 + @b / 3
Second query:
DECLARE @flt FLOAT;
SET @flt = 100 / 3
SELECT @flt + @flt + @flt
I read much about float type in SQL but didn't get what is difference in this two query
(result of first one is 100 and on second 99)
Upvotes: 1
Views: 201
Reputation: 806
It looks like the calculation is made first, then it is rounded according to the highest type. Ie.
In the first case :
First, we have performed calculations, i.e. @ b / 3 (33.33333333333) + @ b / 3 (33.33333333333) + @ b / 3 (33.3333333333333)
And now we round it, so we have 100
In the second case :
At first we have performed calculations, i.e. 100/3 ((33.33333333333))
rounded up to the highest type, i.e. rounded to 33
And now we have further calculations @ flt + @ flt + @ flt (33 + 33 + 33)
so we have 99
Upvotes: 2