September 19, 2017 at 12:07 am
Comments posted to this topic are about the item Real Maths
September 19, 2017 at 1:03 am
I think you'll find that it's mulitplication and addition that are commutative.
September 19, 2017 at 1:22 am
Nice one, thanks Mike
Recently had to debug a compound interest calc that was not delivering expected results - hey voila - integer division...
____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
September 19, 2017 at 1:24 am
edwardwill - Tuesday, September 19, 2017 1:03 AMI think you'll find that it's multiplication and addition that are commutative.
Concur.
____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
September 19, 2017 at 1:53 am
I'd forgotten that integer division truncates rather than rounds, so if 3.5 had been an option I'd have got it wrong.
The moral is, never rely on implicit conversions. I always convert everything explicitly even if unnecessary as it's foolproof and clearer,
September 19, 2017 at 5:04 am
edwardwill - Tuesday, September 19, 2017 1:03 AMI think you'll find that it's mulitplication and addition that are commutative.
Correct.
The commutative property refers to the order of the operands.
a + b = b + a
or
a * b = b * a
Obviously, a / b <> b / a so division is not commutative.
But this was a good exercise in integer and real math. I almost missed the typing of the columns.
September 19, 2017 at 6:46 am
gvoshol 73146 - Tuesday, September 19, 2017 5:04 AMedwardwill - Tuesday, September 19, 2017 1:03 AMI think you'll find that it's mulitplication and addition that are commutative.Correct.
The commutative property refers to the order of the operands.
a + b = b + a
or
a * b = b * a
Obviously, a / b <> b / a so division is not commutative.But this was a good exercise in integer and real math. I almost missed the typing of the columns.
gvoshol 73146 - Tuesday, September 19, 2017 5:04 AMedwardwill - Tuesday, September 19, 2017 1:03 AMI think you'll find that it's mulitplication and addition that are commutative.Correct.
The commutative property refers to the order of the operands.
a + b = b + a
or
a * b = b * a
Obviously, a / b <> b / a so division is not commutative.But this was a good exercise in integer and real math. I almost missed the typing of the columns.
Perhaps what mhtanner meant to say was that for complex, real and rational arithmetic a*(b/c) = (a*b)/c (but that has exactly nothing to do with commutativity). Of for integer arithmetic and natural number arithmetic (a*b)/c isn't always the same as a*(b/c). I think there's a name for this property, but I can't remember what that name is.
Tom
September 19, 2017 at 7:06 am
TomThomson - Tuesday, September 19, 2017 6:46 AMI think there's a name for this property, but I can't remember what that name is.
Associativity?
John
September 19, 2017 at 7:15 am
John Mitchell-245523 - Tuesday, September 19, 2017 7:06 AMTomThomson - Tuesday, September 19, 2017 6:46 AMI think there's a name for this property, but I can't remember what that name is.Associativity?
John
Right. But division isn't associative either.
1 / (2 / 4) <> (1 / 2) / 4
September 19, 2017 at 7:37 am
Great question !
But it leads me to more questions;
what data type is the result of something like this SELECT 3.0/4.0
and why is the result of that different than this
CREATE TABLE #a (a REAL, b REAL, c REAL);
INSERT INTO #a SELECT 2,3,4;
SELECT b/c AS Total from #a;
September 19, 2017 at 7:50 am
Well, it's the same answer, just rendered differently. Actually, it's different data types. Run this, and all will become clear.
SELECT
4.0 AS Col1
, CAST(4.0 AS real) AS Col2
INTO #MyTest;
SELECT * FROM #MyTest;
EXEC sp_help #MyTest;
John
September 19, 2017 at 8:05 am
Excellent Answer, that is most of what I was looking to learn.
The other part of the question that I failed to express clearly is
What determines the data types for a query like this SELECT 3.0/4.0 ? is numeric a default for values that contain a decimal ?
September 19, 2017 at 8:31 am
Good question. Let's investigate.
USE tempdb;
SELECT
-1 AS Col1
, 0 AS Col2
, 1 AS Col3
, 65 AS Col4
, 32768 AS Col5
, 2147483648 AS Col6
, 9223372036854775808 AS Col7
, 2.2 AS Col8
, 2.0000002 AS Col9
, 2.0000000000000002 AS Col10
, 2.0000000000000000000000000000000000002 AS Col11
, 222222.02 AS Col12
, 4.0/3.0 AS Col13
INTO #MyTest;
SELECT * FROM #MyTest;
EXEC sp_help '#MyTest';
Looks as if it uses int if it can, otherwise numeric with the smallest scale and precision possible.
John
September 19, 2017 at 8:50 am
Another Excellent Answer.
Seeing/Learning how you come to the answer is a big help.
And perhaps now I should be smart enough to use sp_help more often.
Thank you so much for your time
September 19, 2017 at 9:22 am
gvoshol 73146 - Tuesday, September 19, 2017 7:15 AMJohn Mitchell-245523 - Tuesday, September 19, 2017 7:06 AMTomThomson - Tuesday, September 19, 2017 6:46 AMI think there's a name for this property, but I can't remember what that name is.Associativity?
John
Right. But division isn't associative either.
1 / (2 / 4) <> (1 / 2) / 4
All arithmetic binary operators in T-SQL which deliver an arithmetic result are left-associative (because any operator has the same precedence as itself, and evaluation is left to right except when this is overriden by precedence differences or by brackets). In particular, division is left-associative. Some (eg multiplication and addition) are associative (ie both left associative and right associative).
Tom
Viewing 15 posts - 1 through 15 (of 16 total)
You must be logged in to reply to this topic. Login to reply