views:

114

answers:

7

Hello,

I am doing this small exercise.

declare @No decimal(38,5);
set @No=12345678910111213.14151;

select @No*1000/1000,@No/1000*1000,@No;

Results are:

12345678910111213.141510    
12345678910111213.141000    
12345678910111213.14151

Why are the results of first 2 selects different when mathematically it should be same?

A: 

The first does @No*1000 then divides it by 1000. The intermediates values are always able to represent all the decimal places. The second expression first divides by 1000, which throws away the last two decimal places, before multiplying back to the original value.

Marcelo Cantos
+1  A: 

because the intermediary type is the same as the argument's - in this case decimal(38,5). so dividing first gives you a loss of precision that's reflected in the truncated answer. multiplying by 1000 first doesn't give any loss of precision because that doesn't overload 38 digits.

oedo
+1  A: 

It's probably because you lose part of data making division first. Notice that @No has 5-point decimal precision so when you divide this number by 1000 you suddenly need 8 digits for decimal part:

123.12345 / 1000 = 0.12312345

So the value has to be rounded (0.12312) and then this value is multiply by 1000 -> 123.12 (you lose 0.00345.

I think that's why the result is what it is...

Crozin
+2  A: 

because of rounding, the second sql first divides by 1000 which is 12345678910111.21314151, but your decimal is only 38,5, so you lose the last three decimal points.

Charles Bretana
+2  A: 

because when you divide first you get:

12345678910111.21314151

then only six decimal digits are left after point:

12345678910111.213141

then *1000

12345678910111213.141
Andrey
+2  A: 

it is not going to do algebra to convert 1000/1000 to 1. it is going to actually follow the order of operations and do each step.

@No*1000/1000
 yields:  @No*1000  =  12345678910111213141.51000
          then /1000=  12345678910111213.141510

and

    @No/1000*1000
yields:  @No/1000  = 12345678910111.213141
         then *1000= 12345678910111213.141000

by dividing first you lose decimal digits.

KM
A: 

You can get around the problem by using CONVERT or CAST on the first value in your expression to increase the number of decimal places and avoid a loss of precision.

DECLARE @num decimal(38,5)
SET @num = 12345678910111213.14151

SELECT CAST(@num AS decimal(38,8)) / 1000 * 1000
Anthony Faull