Real Maths

  • Comments posted to this topic are about the item Real Maths

  • I think you'll find that it's mulitplication and addition that are commutative.

  • 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”

  • edwardwill - Tuesday, September 19, 2017 1:03 AM

    I 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”

  • 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,

  • edwardwill - Tuesday, September 19, 2017 1:03 AM

    I 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 AM

    edwardwill - Tuesday, September 19, 2017 1:03 AM

    I 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 AM

    edwardwill - Tuesday, September 19, 2017 1:03 AM

    I 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

  • TomThomson - Tuesday, September 19, 2017 6:46 AM

    I think there's a name for this property, but I can't remember what that name is.

    Associativity?

    John

  • John Mitchell-245523 - Tuesday, September 19, 2017 7:06 AM

    TomThomson - Tuesday, September 19, 2017 6:46 AM

    I 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

  • 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;

  • 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

  • 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 ?

  • 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

  • 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

  • gvoshol 73146 - Tuesday, September 19, 2017 7:15 AM

    John Mitchell-245523 - Tuesday, September 19, 2017 7:06 AM

    TomThomson - Tuesday, September 19, 2017 6:46 AM

    I 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