Please have a look at the code below and explain to me why there is a deviance in the final results. Note that the difference is the introduction of the brackets in the second calculation. Thanks!
Code:
DECLARE @A decimal(38,19) = 7958011.98
DECLARE @B decimal(38,19) = 10409029441
DECLARE @C decimal(38,19) = 10000000000
DECLARE @Z1 decimal(38,19)
DECLARE @Z2 decimal(38,19)
SET @Z1 = @A * @B / @C
SET @Z2 = @A * (@B / @C)
SELECT @Z1 AS [Correct],
@Z2 AS [Wrong]
Results:
Correct = 8283518.0991650000000000000
Wrong = 8283510.5860060000000000000
The intermediate datatypes are different because of this MSDN article
That is,
(@B / @C)evaluated first, follows rules like this. The intermediate datatype then affects the multiplication by@AYou can see the intermediate and final types here (before assigning to a decimal(38,19) type
So, instead of 1.0409029441 you get 1.040902 for your 2nd math
Note, your 1st is wrong too. It is actually 8283518.099165070318