Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : Maths precedence error in MMBasic? Any maths gurus out there?
Page 1 of 3 | |||||
Author | Message | ||||
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 9110 |
3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3 is calculated by MMbasic as 3.001953125 However, if I convert that to reverse polish I get 3 4 2 * 1 5 - 2 3 ^ ^ / + which evaluates to 3.00012207 Converting the reverse polish back to infix gives 3 + (4 * 2) / (1 - 5) ^ (2 ^ 3) Note the 2 ^ 3 is now bracketed hence the different answer. Is the MMBasic operator precedence wrong or right? UPDATE Wolfram Alpha also gives 3.00012207 so I suspect MMbasic is wrong Edited 2024-05-07 00:40 by matherp |
||||
homa Guru Joined: 05/11/2021 Location: GermanyPosts: 351 |
Hello Peter, an interesting observation. ChatGPT 3.5: To convert the expression "3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3" into Reverse Polish Notation (also known as postfix notation), you can use the shunting-yard algorithm. This algorithm processes operators and operands according to their precedence and associativity. Here's how you can convert the expression: 1. Initialize an empty stack for operators. 2. Initialize an empty list for the output (the reverse Polish notation). 3. Iterate through each token (operands and operators) in the infix expression. 4. If the token is an operand, add it to the output list. 5. If the token is a left parenthesis, push it onto the operator stack. 6. If the token is a right parenthesis, pop operators from the stack and add them to the output list until a matching left parenthesis is encountered. Pop and discard the left parenthesis. 7. If the token is an operator: a. While there are operators at the top of the stack with greater precedence or equal precedence and left associativity, pop them from the stack and add them to the output list. b. Push the current operator onto the stack. 8. After processing all tokens, pop any remaining operators from the stack and add them to the output list. Let's apply this algorithm to the given expression: Infix expression: "3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3" Output (Reverse Polish Notation): "3 4 2 * 1 5 - 2 3 ^ ^ / +" Here's the expression step-by-step: 1. Push "3" onto the stack. 2. Push "+" onto the stack. 3. Push "4" onto the stack. 4. Push "*" onto the stack. 5. Push "2" onto the stack. 6. Push "/" onto the stack. 7. Push "(" onto the stack. 8. Push "1" onto the stack. 9. Push "-" onto the stack. 10. Push "5" onto the stack. 11. Pop and add operators "^" (twice) to the output. 12. Pop and discard "(". 13. Pop and add "/" to the output. 14. Pop and add "*" to the output. 15. Pop and add "+" to the output. So, the reverse Polish notation is "3 4 2 * 1 5 - 2 3 ^ ^ / +". and https://www.wolframalpha.com/input?i=3+%2B+4+*+2+%2F+%28+1+-+5+%29+%5E+2+%5E+3 so I think that the MMBasic operator priority is really wrong! really really tricky. Matthias |
||||
Quazee137 Guru Joined: 07/08/2016 Location: United StatesPosts: 568 |
here is how I read it 3+((4*2)/(1-5)^(2^3)) = 3+8/-4^8 = 3+8/65536 = 3 + 0.0001220703125 in MMB4L 3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3 = 3.001953125 3+((4*2)/(1-5)^(2^3)) = 3.00012207 and 3 + 4 * 2 / (( 1 - 5 ) ^ 2) ^ 3 = 3.001953125 hope this helps Quazee137 Edited 2024-05-07 01:14 by Quazee137 |
||||
twofingers Guru Joined: 02/06/2014 Location: GermanyPosts: 1241 |
But that's what Excel says: strange ... causality ≠ correlation ≠ coincidence |
||||
JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 3800 |
Maybe MMBasic mimics GW-Basic? John |
||||
NPHighview Senior Member Joined: 02/09/2020 Location: United StatesPosts: 200 |
As an avid HP calculator (and later, SwissMicros) user, I compliment your approach. I have a rule of thumb when programming complex calculations, in whatever language: When in doubt, use parentheses liberally to enforce evaluation order. PEMDAS is one attempt to implement the arithmetic concept of associativity. Here's an excellent explanation of how that is (or isn't) implemented in various programming languages, Excel, Matlab, Wolfram Alpha, etc. Edited 2024-05-07 01:19 by NPHighview Live in the Future. It's Just Starting Now! |
||||
homa Guru Joined: 05/11/2021 Location: GermanyPosts: 351 |
Very interesting, thank you! and Libre ... Matthias |
||||
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 9110 |
More and more interesting So we have Excel and MMBasic agreeing but anything that converts to rpn disagreeing The question is whether to read 3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3 as 3+((4*2)/((1-5)^(2^3))) = 3.00012207 or 3+((4*2)/((1-5)^2)^3) = 3.001953125 Quarzee137. Please can you explain why you interpret the expression to yield the former - which is what the rpn convertor does rather than the first exponentiation operator applying to the (1-5)? |
||||
vegipete Guru Joined: 29/01/2013 Location: CanadaPosts: 1109 |
Simplify the problem: 2 ^ 2 ^ 3 = ? a) = 4 ^ 3 = 64 b) = 2 ^ 8 = 256 My take on precedence is you work left to right for equal level operators, so version a would be the correct interpretation. Visit Vegipete's *Mite Library for cool programs. |
||||
JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 3800 |
Yes, if it's done using the shunting yard algorithm (or an equivalent). (Er, maybe there's more than one variant of such anyway?) The same issue crops up with divisions of course. I was taught that division is done before multiplication and that more than one division next to one another are done left to right. (Same rule for subtractions BTW.) "Brackets" (i.e. parentheses) are done first. I would expect exponentiation to be like division - (higher precedence than division and) done left to right if more than one next to one another. But it's been a debate for quite some time!! What result would people like for: 4 * 2 / ( 1 - 5 ) / 2 / 3 and for 4 * 2 / ( 1 - 5 ) - 2 - 3 John Edited 2024-05-07 02:10 by JohnS |
||||
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 9110 |
Reading more into this it appears that exponentiation is conventionally right associative which is what rpn is doing. It is an edge case but I think MMbasic and Excel are probably both wrong if you are going to be purist From wikipedia Edited 2024-05-07 02:33 by matherp |
||||
twofingers Guru Joined: 02/06/2014 Location: GermanyPosts: 1241 |
https://en.wikipedia.org/wiki/Order_of_operations I think that a note in the manual is sufficient. IMHO. Kind regards Michael causality ≠ correlation ≠ coincidence |
||||
homa Guru Joined: 05/11/2021 Location: GermanyPosts: 351 |
Especially since RPN seems to be better defined in the sequence and there are "standardised" instructions for converting to RPN (see above according to ChatGPT). I also found this: https://www.rpn-calc.com/?q=3+4+2+*+1+5+-+2+3+%5E+%5E+%2F+%2B Matthias |
||||
JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 3800 |
GW-Basic.exe running under DOSBOX ? 3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3 3.001953 John |
||||
Volhout Guru Joined: 05/03/2018 Location: NetherlandsPosts: 4223 |
Peter, Most formula's I know that raise something to the power X, have some form of a calculation that they raise to that power. Hence there are always parenthesis around the formula. And when that is the case it is simple. You work your was from inside to outside. something like: (x+(a+b)^2)^3 makes life simple.... Volhout PicomiteVGA PETSCII ROBOTS |
||||
Quazee137 Guru Joined: 07/08/2016 Location: United StatesPosts: 568 |
At engineering class we went from slide rules to TI-84's that was set to use RPN on power on. So got use to thinking that way and thats what I saw when looking at the equation. But the left hand rule should be used now days. I was just showing how it was being handled by MMBasic that is the right way to process this equation. Yes I'm old and it ok. Quazee137 p.s. yes I still have both and a lookup book that came with slide rule. Edited 2024-05-07 04:06 by Quazee137 |
||||
TassyJim Guru Joined: 07/08/2011 Location: AustraliaPosts: 6098 |
I like the way the calculator program on my phone works. As I entered the formula, as soon as I entered the ^ an opening bracket was added. This gave me 3 + 4 * 2 / ( 1 - 5 ) ^ ( If I continued with the brackets used, I ended up with 3.00012207 If I backspaced to remove the opening bracket, I ended up agreeing with Microsoft and MMBasic. I like my calculator program. "Panecal Plus" on Android Panecal evaluates equal precedence left to right but really wants you to think about what you really want. Jim VK7JH MMedit MMBasic Help |
||||
LeoNicolas Guru Joined: 07/10/2020 Location: CanadaPosts: 479 |
From the android app HiPER Scientific Calculator |
||||
phil99 Guru Joined: 11/02/2018 Location: AustraliaPosts: 2135 |
As there is no universal answer, rather than change MMBasic as previously suggested a note in the manual on the rules of precedence would be best. When in doubt I bracket almost everything then remove pairs one at a time until the result changes, restoring those ones. |
||||
Frank N. Furter Guru Joined: 28/05/2012 Location: GermanyPosts: 828 |
This is what my CASIO calculator shows: Frank |
||||
Page 1 of 3 |
Print this page |