Results 1 to 19 of 19

Thread: [RESOLVED] Expression Calculator

  1. #1

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,682

    Resolved [RESOLVED] Expression Calculator

    Recently we had reason to use an infix calculator I wrote. During testing two questions came up and I' seeking other opinions.

    The first was concerning expressions like -9^2. What does that equal? Pumping that into the Google search bar or Wolfram Alpha it equals -81.
    But it appears that both interpret that to mean -(9^2).

    The second question was my implementation of multiplication by juxtaposition, especially my giving it a higher precedence than regular multiplication. e.g.

    6/2(2+1) = 1 multiplication by juxtaposition
    6/2*(2+1)=9

    For those unfamiliar with multiplication by juxtaposition here is an explanation.

    https://www.youtube.com/watch?v=lLCDca6dYpA


    In the section labeled ‘Current Calculators’ in the link below it seems that not all calculators answer the same.

    https://www.youtube.com/watch?v=4x-BcYCiKCk


    Here are all of the test ran. Format is expression TAB answer. All numbers represented by decimal.

    Code:
    6/2(2+1)	1
    6/2*(2+1)	9
    84/14*9/18	3
    22/7	3.1428571428571428571428571429
    7(3.1428571428571428571428571427)	21.999999999999999999999999999
    -4(2+3)/(8-6)	-10
    -4(2-3^2)/(8-6)	14
    3-5^2	-22
    3(2+5)	21
    3/3(2+5)	0.1428571428571428571428571429
    3/3*(2+5)	7
    4*3(4-2(6-3))/3	-8
    (8-6)+10	12
    (5*9)/(10+5)	3
    (8*8)-(11*4)	20
    ((40*2)-10)/5	14
    (16*6)+(30+5)	131
    56-25-12/48-29	1.75
    20*5-12*5*60-55	-3555
    (20*5-12*5*60-55)%10	-5
    -1^2	1
    -2^2	4
    -(2^2)	-4
    0-3^2	-9
    -3^5	-243
    3^-2	0.111111111111111
    -3^-2	0.111111111111111
    0.5^3	0.125
    -0.5^3	-0.125
    1,000^2	1000000
    √0	0
    √24	4.89897948556636
    sqrt(24)	4.89897948556636
    Math.Pow Test
    Code:
            For d As Double = 1.0# To 5.0# Step 1.0#
                Dim ans As Double = Math.Pow(-2.0#, d)
                Debug.WriteLine("-2 raised to {0} = {1}?", d, ans)
            Next
    Last edited by dbasnett; Nov 6th, 2023 at 06:46 PM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,613

    Re: Expression Calculator

    It seems like you might have a typo. I would say that -(9^2) would be -81, not -9.

    I find the second example interesting, as I would evaluate both of them to 1, but especially the second one. It comes down to whether somebody follows My Dear Aunt Sally, or puts multiplication and division on a first come first served basis.

    In both cases, what is in the parentheses gets evaluated first, so it looks like this:

    6/2(3) vs 6/2 * 3

    If you follow the My Dear Aunt Sally rule, then multiplication takes precedence over division, and you'd end up with 1 in both cases, whereas if you follow the rule of FCFS, then the division happens first followed by the multiplication, solely because of firstism. I've heard both as being asserted to be true, and there isn't a clear reason to choose one over the other.
    My usual boring signature: Nothing

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,613

    Re: Expression Calculator

    After watching the first video, I would say that the issue has to do with word processors, not with the equations. If you could actually write X over Y, that would make it clear, but since it requires an equation editor to write that in any modern word processor, and equation editors can be a pain in the tuckus, everybody writes X/Y. That isn't clear, because there could be anything at all to the left or right of the equation. In fact, one could even misinterpret my writing to suggest that "writes" is a variable, or that ". That" has some kind of meaning.

    If people wanted to be clear, they could do so either by using an equation editor, or by using parentheses. Both of those require a little more work, but they can be FAR more clear: 6/(2(1+2)) has two extra characters and leaves no doubt as to what the equation means.

    So....basically, we're lazy. Also, I never learned PEMDAS in school.
    My usual boring signature: Nothing

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,484

    Re: Expression Calculator

    The first was concerning expressions like -9^2. What does that equal? Pumping that into the Google search bar or Wolfram Alpha it equals -9.
    But it appears that both interpret that to mean -(9^2).
    I remember being told that math was unambiguous in school. Oh, how naďve I was.

    But really. What should it mean to you and your company? I treat it as negative 9 to the second power or (0 - 9) ^ 2. You may treat it differently.

    For those unfamiliar with multiplication by juxtaposition here is an explanation.
    In the video, she says "mathematicians are quite lazy so they drop the parenthesis".

    If you can enforce the behavior, then I would require users to use parenthesis to explicitly state what it is that they're trying to achieve. If you cannot enforce the behavior then allow the user to specify if multiplication by juxtaposition should be prioritized or not and then alter your business logic based on that suggestion.
    "Code is like humor. When you have to explain it, it’s bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,377

    Re: Expression Calculator

    Quote Originally Posted by Shaggy Hiker View Post
    If you follow the My Dear Aunt Sally rule, then multiplication takes precedence over division, and you'd end up with 1 in both cases, whereas if you follow the rule of FCFS, then the division happens first followed by the multiplication, solely because of firstism. I've heard both as being asserted to be true, and there isn't a clear reason to choose one over the other.
    That's actually the problem... multiplication isn't first any more than addition is ... IF I remember the rules of the road here, Multiply and Divide are actually equal order, while add and subtract are equal with each other... and barring any further instruction from parenthesis, it's left to right.
    So 6/2(3) -> 6/2 * 3 -> 3 * 3 -> 9

    the PEMDAS or Dear Aunt Sally we've all been taught it a misnomer.


    Order of Operations Steps:
    Parentheses
    The first step is to solve the operation within parentheses or brackets. Parentheses are used to group things together. Work out all groupings from inside to out.

    Exponents
    Work out the exponential expressions after the parentheses.

    Multiplication and Division
    Next, moving from left to right, multiply and/or divide, whichever comes first.

    Addition and Subtraction
    Lastly, moving from left to right, add and/or subtract, whichever comes first.
    Source: https://www.splashlearn.com/math-voc...-of-operations


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    PowerPoster
    Join Date
    Nov 2017
    Posts
    2,887

    Re: Expression Calculator

    Quote Originally Posted by dbasnett View Post
    Recently we had reason to use an infix calculator I wrote. During testing two questions came up and I' seeking other opinions.

    The first was concerning expressions like -9^2. What does that equal? Pumping that into the Google search bar or Wolfram Alpha it equals -9.
    But it appears that both interpret that to mean -(9^2).
    Taking a completely mathematical approach to this, the correct solution to -9^2, written on paper as -9 {superscript} 2 is -81. That problem spoken in English would be "The negative of 9 squared."

    If the problem spoken in English is "Negative 9 squared", then it would need to be written on paper as (-9) {superscript} 2.

  7. #7
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    838

    Re: Expression Calculator

    the rule of priority says the calculus goes from left to right so in your second example :

    6/2(2+1) = 1 multiplication by juxtaposition
    6/2*(2+1)=9

    both should give the same result : 9 : you start by the division then the multiplication (of course the quote are calculated first)

    byr the way, all info here : https://en.wikipedia.org/wiki/Order_of_operations
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  8. #8
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    913

    Re: Expression Calculator

    The first was concerning expressions like -9^2. What does that equal? Pumping that into the Google search bar or Wolfram Alpha it equals -9.
    But it appears that both interpret that to mean -(9^2).
    This comes down to what order of precedence does unary minus have? When I deal with an infix expression I have unary minus (and unary plus) the highest below parenths. Hence -9^2 means (-9)^2 which is 81. If unary minus has a precedence below that of ^ (which I would consider unusual) , then you have -(9^2) which is -81. Note that 0-9^2 should be non-ambiguous as -81 (as here - is a binary operator and not a unary operator) and that some parsers infer -9^2 as 0-9^2 as they don't really process unary operators - just translate them into binary ones. It all comes down to the precedence rules in operation. Also that when evaluating multiple ^ these are processed right to left for equal precedence and not left to right as is usual with say * and /. So 2^3^3 would be 2 ^(3 ^ 3) and not (2^3)^3.
    Last edited by 2kaud; Nov 7th, 2023 at 04:46 AM.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,682

    Re: Expression Calculator

    Thanks shaggy for catching the typo.

    In Wolfram 6/2(2+1) equals 9. But try this Wolfram link.

    Would we all agree that 6/2(2+1) can be clarified like this,
    Code:
       6
    -------
    2(2+1)
    or
    Code:
     6
    ---(2+1)
     2
    IF it has to be one line and clarified and multiplication by juxtaposition has the same precedence as multiply and the answer should be 1 then we need this
    6/(2(2+1))

    IF it has to be one line and clarified and multiplication by juxtaposition has a higher precedence than multiply and the answer should be 9 then we need this
    6/2*(2+1)

    The latter rule results in less typing for clarification.

    My little help file will clarify this and exponentiation of -9^2.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  10. #10
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    913

    Re: Expression Calculator

    6/2(2+1)
    This is 6/2*(2+1)

    This is 6/2*3 which as / * have the same precedence is evaluated as 6/2 which is 3 then 3 * 3 which is 9.

    If you want the answer 1, then
    6/(2(2+1))

    which is
    6/(2*(2+1))

    which is 6/(2*3) which is 6/6 which is 1.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  11. #11

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,682

    Re: Expression Calculator

    Quote Originally Posted by 2kaud View Post
    This is 6/2*(2+1)

    This is 6/2*3 which as / * have the same precedence is evaluated as 6/2 which is 3 then 3 * 3 which is 9.

    If you want the answer 1, then
    6/(2(2+1))

    which is
    6/(2*(2+1))

    which is 6/(2*3) which is 6/6 which is 1.
    Did you read post #9? I get it that you disagree.

    You might consider watching this,

    https://www.youtube.com/watch?v=4x-BcYCiKCk
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  12. #12
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    913

    Re: Expression Calculator

    Well I've dug out my Casio calculator which gives 9 for 6/2*(2+1) and 1 for 6/2(2+1). This means that the implied multiplication 2(2+1) has a higher precedence than / and *. I haven't come across this meaning before where explicit and implicit multiplication has different precedence.

    I've also tried a couple of expression parsers and they treat 6/2(2+1) as an invalid expression - including c++. I can't comment of VB/TB.

    PS Calculator Plus for Windows gives a result of 2 for 6/2(2+1)! I never did trust that...
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  13. #13
    PowerPoster
    Join Date
    Nov 2017
    Posts
    2,887

    Re: Expression Calculator

    Quote Originally Posted by 2kaud View Post
    Well I've dug out my Casio calculator which gives 9 for 6/2*(2+1) and 1 for 6/2(2+1). This means that the implied multiplication 2(2+1) has a higher precedence than / and *. I haven't come across this meaning before where explicit and implicit multiplication has different precedence.

    I've also tried a couple of expression parsers and they treat 6/2(2+1) as an invalid expression - including c++. I can't comment of VB/TB.

    PS Calculator Plus for Windows gives a result of 2 for 6/2(2+1)! I never did trust that...
    When entering 6/2(2+1) on a TI-92 calculator, when the entered expression is displayed it adds the implied * between the 2 and the ( and thus gives the result of 9.

    When entering 6/2(2+1) on a TI-89 calculator, when the entered expression is displayed it adds the implied * between the 2 and the ( and thus gives the result of 9.

    When entering 6/2(2+1) on a TI-86 calculator, it gives the result of 9.

    When entering 6/2(2+1) on a TI-Npire CX2 CAS calculator, when the entered expression is displayed it separates 6/2 as a properly displayed fraction, and then to the right of that entire fraction it displays *(2+1) and gives the result of 9.

    So the TI expression parser seems to not give this special operational priority, both historically and currently.

    dbasnett - I think in the end, as long as these seemingly ambiguous cases are all documented as to how they are evaluated, that should be good enough. But I would encourage any end users of your software to always be explicit in their grouping so as to remove any chance of unexpected results.

  14. #14
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,613

    Re: Expression Calculator

    I feel like I learned that multiplication and division have equal precedence, but that I then learned about he My Dear Aunt Sally precedence only recently...and I feel like I learned it here, or at least from some programming resource. Either way, I think making it explicit what you intend is probably superior to letting people decide for themselves.
    My usual boring signature: Nothing

  15. #15
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    913

    Re: Expression Calculator

    making it explicit what you intend is probably superior to letting people decide for themselves
    Yes. When in doubt use brackets. When major calculator makers can't agree..........
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  16. #16
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,613

    Re: Expression Calculator

    I moved this in hopes that it might get the math forum moving a bit. It's not really a coding question, after all.
    My usual boring signature: Nothing

  17. #17

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,682

    Re: Expression Calculator

    Quote Originally Posted by Shaggy Hiker View Post
    I feel like I learned that multiplication and division have equal precedence, but that I then learned about he My Dear Aunt Sally precedence only recently...and I feel like I learned it here, or at least from some programming resource. Either way, I think making it explicit what you intend is probably superior to letting people decide for themselves.
    I was taught PEMDAS with multiplication and division having same precedence, same for addition and subtraction. From my reading PEMDAS is a North America thing. PEJMDAS seems prevalent in other places.

    I documented 6/2(1+2) = 1 as well as -9^2 = 81. Users of the library will know and can do as they will.

    As others have pointed out explicit grouping removes all doubt.

    I'm ok with the move.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  18. #18
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    913

    Re: Expression Calculator

    I documented 6/2(1+2) = 1 as well
    For users, I would document the operator precedence. Consider:

    [highest to lowest]
    brackets
    unary +, -
    ^ (right to left)
    <implied multiplication>
    *, - (left to right)
    binary +, - (left to right)
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  19. #19

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,682

    Re: Expression Calculator

    Quote Originally Posted by 2kaud View Post
    For users, I would document the operator precedence. Consider:

    [highest to lowest]
    brackets
    unary +, -
    ^ (right to left)
    <implied multiplication>
    *, - (left to right)
    binary +, - (left to right)
    FWIW there is a method in the class that returns all operators / functions and precedence.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width