Simple arithmetic

  • Sean Lange (12/10/2014)


    Can you show me an example of any programming language that does not support the mathematical agreed upon order of operations?

    That's rather easy to do. 🙂

    In SQL -,+,*, and / associate the same way:

    8-4-2 = (8-4)-2 , and

    8/4/2 = (8/4)/2 , and

    8/4*2 = 4

    Presumably you think that's "the mathematical agreed on order of operations" (as if there were such a thing when the context isn't defined :laugh:)

    In BCPL they associate opposite ways:

    8-4-2 = (8-4)-2 , and

    8/4/2 = 8/(4/2) , and

    8/4*2 = 1

    (see section 5.1 of The BCPL Reference Manual which points out that in BCPL * and /and rem associate to the right while + and - associate to the left.

    Since that's not the same order as SQL, it's presumably not your imaginary "mathematical agreed on order".

    The question above is NOT a syntax memory test, it tests your understanding of the order of operations for math.

    Wrong, it's a syntax memory test since the order of operations depends on the programming language or the mathematical language, as the case may be.

    Tom

  • i saw lots of people on social sites having problems with:

    1+1+1+1+1+1+1+1+1+1+1+1x0+1 = ?

    most of the answers was 1. So saying Simple may apply differently to others :hehe:

    I wonder how the answers would go to this:

    1x1x1x1x1x1x1x1x1x1x1x1x0x1 = ?

    Which leads to a great gag bet. You and a friend (or not a friend :-)) each pick a baseball team, and you bet that the SUM of your team's runs for the season will be greater than the PRODUCT of your friend's team's runs.

    Don Simpson



    I'm not sure about Heisenberg.

  • BEDMAS? Thanks. Never heard that one before. For me it was always PEMDAS with the mnemonic "Please Excuse My Dear Aunt Sally."

  • TomThomson (12/10/2014)


    Sean Lange (12/10/2014)


    Can you show me an example of any programming language that does not support the mathematical agreed upon order of operations?

    That's rather easy to do. 🙂

    In SQL -,+,*, and / associate the same way:

    8-4-2 = (8-4)-2 , and

    8/4/2 = (8/4)/2 , and

    8/4*2 = 4

    Presumably you think that's "the mathematical agreed on order of operations" (as if there were such a thing when the context isn't defined :laugh:)

    In BCPL they associate opposite ways:

    8-4-2 = (8-4)-2 , and

    8/4/2 = 8/(4/2) , and

    8/4*2 = 1

    (see section 5.1 of The BCPL Reference Manual which points out that in BCPL * and /and rem associate to the right while + and - associate to the left.

    Since that's not the same order as SQL, it's presumably not your imaginary "mathematical agreed on order".

    The question above is NOT a syntax memory test, it tests your understanding of the order of operations for math.

    Wrong, it's a syntax memory test since the order of operations depends on the programming language or the mathematical language, as the case may be.

    Bah. Picking a language from the 60s? Come on Tom you sound like you are trying to defend this just to be argumentative.

    In actual code I would never be so sloppy as to not include proper parenthesis when required anymore than I would not be explicit with datatypes or schemas.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • This was removed by the editor as SPAM

  • Sean Lange (12/10/2014)


    In actual code I would never be so sloppy as to not include proper parenthesis when required anymore than I would not be explicit with datatypes or schemas.

    Maybe you wouldn't, but there are plenty that would!



    Alvin Ramard
    Memphis PASS Chapter[/url]

    All my SSC forum answers come with a money back guarantee. If you didn't like the answer then I'll gladly refund what you paid for it.

    For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • Fun one.

    Thanks,

  • TomThomson (12/10/2014)


    Xavon (12/10/2014)


    paul.knibbs (12/10/2014)


    Emil Bialobrzeski (12/10/2014)


    i saw lots of people on social sites having problems with:

    1+1+1+1+1+1+1+1+1+1+1+1x0+1 = ?

    most of the answers was 1. So saying Simple may apply differently to others :hehe:

    Slightly unfair question, though, because the answer depends on operator precedence. For instance, I was never taught at school that multiplication takes priority over addition, and if you just process that statement left to right, then the answer *is* 1--you add a load of 1s together, multiply that by zero, and add 1 to the result.

    Wow, really? Proper order of operations is fairly important in most high level maths. I remember having drills wherein we had to take math expressions and add parentheses to show how the statement should be parsed.

    Yes, operator precedence is very important. That's why both computer languages and mathematical calculi and notations define operator precedence rigorously. There are pleny of languages/calculi where precedence is defined to be purely left to right except unless there are special cases like for example the use of brackets or some other precedence modifying syntax (eg in mathematics the lambda calculus and some of its derivatives, and in computing several programming languages based on them) and also notations where precedence is purely left to right or purely right to left - think of Polish notation and reverse Polish notation, which are respectively left to right and right to left: both are commonly used in both maths and computing. Given the expression 1+1x0+1 the value could be 3,1, or 2 depending on the precedence rules so unless either the language/notation is specified or the precedence rules are defined without mentioning the language/notation there's no knowing what the value is. Sure, in a junior high school maths test the answer will probably be 2 because no-one has yet been taught that there's more than one possible notation, but that doesn't mean that that's always the right answer.

    I'm not sure how you can get 3 from that. Would like to see it with brackets to explain....

  • jlennartz (12/10/2014)


    Fun one.

    Thanks,

    +1

    Not all gray hairs are Dinosaurs!

  • Sean Lange (12/10/2014)


    Bah. Picking a language from the 60s? Come on Tom you sound like you are trying to defend this just to be argumentative

    You asked if you could be shown an example, and I showed you one. It happens to be the language from which C and C++ were derived, as well as a language that was implemented on several times as many architectures as SQL has been, was used to write several operating systems and compilers, and was the language used by the Xerox PARC ALTO project, so very influential in the history of computing. The reason I picked it as my example was that it had more influence on my early ideas about compiler structure than other languages - I first met it when Martin R came back from MIT to Cambridge and gave a seminar on it in the math lab (I think in 1968, but it could have been '67) and I used ideas from quite a lot.

    Of course I could have picked a language which just didn't bother with operator precedence because the language syntax made that a meaningless concept - I think LISP was the first such (but far from the last) right back in the 1950s (and it's still going strong today) but there may have been an earlier one that either I never came across or I have forgotten about. One of the languages I designed and led the implementation of in the 70s had no operator precedence (and was used for a dozen years, maybe more, in implementation of ICL's 7900 range of devices), at least one of the functional languages from the late 70s and early 80s had no operator precedence, in the 1980s the declarative DML for the UK's Alvey Flagship project had no operator precedence. I can't think of any 1990s or 2000s examples because I've been mostly out of touch with new language designs since 1996, but I'll bet there are new LISP-based languages with no operator precedence several times per decade.

    Incidentally, I wonder whether you think the exponentiation operator is right associative or left associative in your "standard" rules for evaluating numeric expressions?

    Tom

  • antony-688446 (12/10/2014)


    TomThomson (12/10/2014)


    Xavon (12/10/2014)


    paul.knibbs (12/10/2014)


    Emil Bialobrzeski (12/10/2014)


    i saw lots of people on social sites having problems with:

    1+1+1+1+1+1+1+1+1+1+1+1x0+1 = ?

    most of the answers was 1. So saying Simple may apply differently to others :hehe:

    Slightly unfair question, though, because the answer depends on operator precedence. For instance, I was never taught at school that multiplication takes priority over addition, and if you just process that statement left to right, then the answer *is* 1--you add a load of 1s together, multiply that by zero, and add 1 to the result.

    Wow, really? Proper order of operations is fairly important in most high level maths. I remember having drills wherein we had to take math expressions and add parentheses to show how the statement should be parsed.

    Yes, operator precedence is very important. That's why both computer languages and mathematical calculi and notations define operator precedence rigorously. There are pleny of languages/calculi where precedence is defined to be purely left to right except unless there are special cases like for example the use of brackets or some other precedence modifying syntax (eg in mathematics the lambda calculus and some of its derivatives, and in computing several programming languages based on them) and also notations where precedence is purely left to right or purely right to left - think of Polish notation and reverse Polish notation, which are respectively left to right and right to left: both are commonly used in both maths and computing. Given the expression 1+1x0+1 the value could be 3,1, or 2 depending on the precedence rules so unless either the language/notation is specified or the precedence rules are defined without mentioning the language/notation there's no knowing what the value is. Sure, in a junior high school maths test the answer will probably be 2 because no-one has yet been taught that there's more than one possible notation, but that doesn't mean that that's always the right answer.

    I'm not sure how you can get 3 from that. Would like to see it with brackets to explain....

    I agree with you - my mistake, + has precedence and x has precedence both deliver 2, as does no precedence with right to left evaluation order while no precedence with left to right evaluation order delivers 1. I remember start up with something far more complex and simplified it, and clearly I got the simplification wrong.

    Tom

  • Thanks Tom.

    Enlightening posts, as always.

  • TomThomson (12/10/2014)


    Incidentally, I wonder whether you think the exponentiation operator is right associative or left associative in your "standard" rules for evaluating numeric expressions?

    One thing I would never attempt is to debate anything regarding mathematics with someone as yourself. I am at an extreme disadvantage there for sure. 😛

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • You can in fact get three from this.

    1+1+1+1+1+1+1+1+1+1+1+1x0+1 = 3

    Here is how it works:

    1+1+1+1+1+1+1+1+1+1+1 = 11 and

    1X0 + 1 = 1 and

    11 and 1 is 12

    and 1 and 2 is 3

    🙂

    M.

    Not all gray hairs are Dinosaurs!

  • Miles Neale (12/10/2014)


    You can in fact get three from this.

    1+1+1+1+1+1+1+1+1+1+1+1x0+1 = 3

    Here is how it works:

    1+1+1+1+1+1+1+1+1+1+1 = 11 and

    1X0 + 1 = 1 and

    11 and 1 is 12

    and 1 and 2 is 3

    🙂

    M.

    I like it. But the natural way to get 3 with the rules I think you are working to might be

    1+1+1+1+1+1+1+1+1+1 = 10 = 1 because 1+0 = 1

    1+1 = 2

    2 +1*0 = 2

    2+1 = 3

    Of course you can get all sorts of answers like that: if you were using octal notation instead of decimal you would get 5, base 7 gives you 6, base 6 gives you 3, again, and so on.

    Another interesting variant is to change the ring you are doing arithmetic in instead of collapsing numbers by adding their digits together: if for example you are looking at elements in the seven element field you will get 5, and in the 2 element field you will get 0.

    warning: all those calculations done in my head, not written down and checked properly, so the numbers may be wrong - the point is simply that there are lots of ways of changing the rules of arithmetic (as opposed to changing the syntax of the notation) to get lots of different answers.

    Miles' rule change (effectively a change so that 9+1 = 1 instead of 9+1 = 10) is one that has been the source of some amusing questions over the years, but it's far from the only possible rule change. Changing the ring used is of course a much less drastic change because it doesn't throw away any of the ability to cancel additions, ie you can still deduce that a+x = b+x implies a = b, whereas with a change like Miles' you can't (because 0+1 = 9+1 is true but 0 = 1 is false) and cancelling is not always possible.

    edit: :blush: that should not have said it's because 0 = 1 is false, but because 0 = 9 is false. I'm getting careless!

    Tom

Viewing 15 posts - 16 through 30 (of 30 total)

You must be logged in to reply to this topic. Login to reply