Royi Namir
Royi Namir

Reputation: 148524

SQL server strange math result?

I think Im going mad

SELECT 30.0/(-2.0)/5.0;
SELECT 30.0/-2.0/5.0;

enter image description here

what is happening here ?

edit

who said that x/(y)/z will be (x/y)/z and not x(y/z) ? –

Upvotes: 2

Views: 167

Answers (2)

Andomar
Andomar

Reputation: 238106

Your second select is executed as:

select 30.0/-(2.0/5.0)

Looks like the minus sign causes SQL Server to invert the last division-- pretty sure that's a bug!

Upvotes: 3

KM.
KM.

Reputation: 103597

SELECT 30.0/(-2.0)/5.0; is fairly clear:

SELECT 30.0/(-2.0)=-15.0
-15.0/5.0=-3.0

the second select is being interpreted as: SELECT 30.0/-(2.0/5.0);

2.0/5.0=0.4
30.0/-0.4=-75.0

see: Operator Precedence (Transact-SQL)

Upvotes: 7

Related Questions