Two t-SQL (MS SQL 2017) queries:
Select 125. / -124/ 125.
Select 125. / (-124)/125.
Difference only on parentheses.
But, results are not equal:
-126.0080645161
-0.0080645120
Why the results are different?
Two t-SQL (MS SQL 2017) queries:
Select 125. / -124/ 125.
Select 125. / (-124)/125.
Difference only on parentheses.
But, results are not equal:
-126.0080645161
-0.0080645120
Why the results are different?
The addition of the parentheses in the second case is changing the order of operations. To avoid any ambiguity, you should specify parentheses explicitly. For the first result, you should use:
SELECT 125. / (-124 / 125.)
and for the second case:
SELECT (125. / -124) / 125.
The explanation is that in T-SQL, the * (Multiplication)
and / (Division)
operators have higher precedence than the - (Negative)
operator. So what is exactly evaluated is:
Expession 1: 125. / - (124 / 125)
, equals to : -126.0080645161
Expession 2: 125. / (-124) / 125
, equals to : -0.0080645120
Note, that the correct result is: -0.0080645120
.