
Nov 6th, 2023, 02:36 PM
#1
[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=4xBcYCiKCk
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)/(86) 10
4(23^2)/(86) 14
35^2 22
3(2+5) 21
3/3(2+5) 0.1428571428571428571428571429
3/3*(2+5) 7
4*3(42(63))/3 8
(86)+10 12
(5*9)/(10+5) 3
(8*8)(11*4) 20
((40*2)10)/5 14
(16*6)+(30+5) 131
562512/4829 1.75
20*512*5*6055 3555
(20*512*5*6055)%10 5
1^2 1
2^2 4
(2^2) 4
03^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.

Nov 6th, 2023, 04:06 PM
#2
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

Nov 6th, 2023, 04:17 PM
#3
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

Nov 6th, 2023, 04:19 PM
#4
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.

Nov 6th, 2023, 05:02 PM
#5
Re: Expression Calculator
Originally Posted by Shaggy Hiker
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/mathvoc...ofoperations
tg

Nov 6th, 2023, 05:04 PM
#6
Re: Expression Calculator
Originally Posted by dbasnett
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.

Nov 7th, 2023, 04:21 AM
#7
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)

Nov 7th, 2023, 04:42 AM
#8
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 09^2 should be nonambiguous as 81 (as here  is a binary operator and not a unary operator) and that some parsers infer 9^2 as 09^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)

Nov 7th, 2023, 09:32 AM
#9
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,
or
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.

Nov 7th, 2023, 11:09 AM
#10
Re: Expression Calculator
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)

Nov 7th, 2023, 11:17 AM
#11
Re: Expression Calculator
Originally Posted by 2kaud
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=4xBcYCiKCk

Nov 7th, 2023, 12:30 PM
#12
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)

Nov 7th, 2023, 02:09 PM
#13
Re: Expression Calculator
Originally Posted by 2kaud
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 TI92 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 TI89 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 TI86 calculator, it gives the result of 9.
When entering 6/2(2+1) on a TINpire 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.

Nov 7th, 2023, 05:33 PM
#14
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

Nov 8th, 2023, 04:34 AM
#15
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)

Nov 8th, 2023, 09:06 AM
#16
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

Nov 8th, 2023, 10:41 AM
#17
Re: Expression Calculator
Originally Posted by Shaggy Hiker
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.

Nov 8th, 2023, 10:59 AM
#18
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)

Nov 8th, 2023, 11:43 AM
#19
Re: Expression Calculator
Originally Posted by 2kaud
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.
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
