June 11, 2020 at 6:40 pm
Yep... agreed. But you have to do something "special" for it to work. I guess I don't see any real reason why SQL Server should be any different except that it doesn't work the way most people would think according to the rules of algebraic hierarchy.
--Jeff Moden
Change is inevitable... Change for the better is not.
June 11, 2020 at 6:54 pm
The only reason why I got it right was because it has bitten so many people that work in every company that I've ever worked in including me back in my 6.5 days. So far as I'm concerned, it IS a bug but, if you manually try the same formula on the Windows Calculator, you'll find that SQL Server isn't the only place where this happens except the Windows calculator comes up with an even odder answer. If you paste the formula, it changes the formula to come up with the correct answer. It's why I almost always use parentheses for all but the simplest of formulas.
The only reason why I got it right is because I pasted the question into SSMS. It demonstrates it is good practice to put brackets in you formula with SQL Server even if you think you don't really need them and not just for the clarity of the reader.
June 12, 2020 at 12:30 pm
It seems that it is the two operators following each other "/-" that causes unexpected behavior (not the minus at the beginning).
SELECT -100.0/-100.0*10.0 => 0.1
SELECT (-1*100.0)/-100.0*10.0 =>0.1
SELECT -100.0/(-1*100.0)*10.0 => 10
SELECT (-1*100.0)/(-1*100.0)*10.0 => 10
That was a very helpful question, thanks. One can never be too careful when it comes to things that happen "automatically" (precedence, implicit conversion...), often they happen differently than you think.
June 12, 2020 at 2:24 pm
It seems that it is the two operators following each other "/-" that causes unexpected behavior (not the minus at the beginning).
SELECT -100.0/-100.0*10.0 => 0.1
SELECT (-1*100.0)/-100.0*10.0 =>0.1
SELECT -100.0/(-1*100.0)*10.0 => 10
SELECT (-1*100.0)/(-1*100.0)*10.0 => 10
That was a very helpful question, thanks. One can never be too careful when it comes to things that happen "automatically" (precedence, implicit conversion...), often they happen differently than you think.
<deleted> I skimmed your reply and made a bad reply. Lesson learned - drink coffee THEN read forum posts...
The above is all just my opinion on what you should do.
As with all advice you find on a random internet forum - you shouldn't blindly follow it. Always test on a test server to see if there is negative side effects before making changes to live!
I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.
September 1, 2020 at 6:01 am
You right, but it's absolutely wrong. Must be considered as a bug
Viewing 5 posts - 31 through 34 (of 34 total)
You must be logged in to reply to this topic. Login to reply