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 2 of 3 | |||||
Author | Message | ||||
Volhout Guru Joined: 05/03/2018 Location: NetherlandsPosts: 4223 |
Since you can achieve both answers with mmbasic, mmbasic is good enough. It is up to the coder to know what he is doing. Volhout PicomiteVGA PETSCII ROBOTS |
||||
Mixtel90 Guru Joined: 05/10/2019 Location: United KingdomPosts: 6783 |
As MMBasic is supposed to be simulating an 80's "boot to BASIC" home computer then I'd expect it to work more like Microsoft BASIC or GWBASIC than FORTH. It would have been fairly unusual to see RPN in that situation. IIRC the rule for the early BASICs was always BODMAS, in strict left to right order as that's how the relatively crude parsers worked. Edited 2024-05-07 18:00 by Mixtel90 Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
Plasmamac Guru Joined: 31/01/2019 Location: GermanyPosts: 554 |
Edited 2024-05-08 04:21 by Plasmamac Plasma |
||||
zeitfest Guru Joined: 31/07/2019 Location: AustraliaPosts: 482 |
Interesting thread. What is the result on maximites or early micromites ? @Plasmamac, what is the screen ? |
||||
EDNEDN Senior Member Joined: 18/02/2023 Location: United StatesPosts: 118 |
Unknown. But I'm of the opinion on PicoMite's there should be an 'Option' setting that is defaulted to the current behavior. And the user has the ability to set the evaluation to a strict BODMAS mode. Maybe an RPN mode could also be available. Edited 2024-05-08 14:33 by EDNEDN |
||||
JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 3800 |
I'm wondering how many programs genuinely have code that does double consecutive exponentiations (with no parentheses to force the order) and wasn't tested to check it worked as intended? (I've never met one.) The need for an OPTION looks miniscule. John Edited 2024-05-08 16:30 by JohnS |
||||
Volhout Guru Joined: 05/03/2018 Location: NetherlandsPosts: 4223 |
Afaik the excel implementation (working from the inside) is applicable to science, engineering. The application of raising a power to the power (right to left) has application in pure mathematica, and cryptography. But with placing brackets you can enforce what you desire. Raising ro the power has its priority in rules, but not compared to itself. That is different from other operators. A+B+C=A+(B+C) but not for power. Volhout Edited 2024-05-08 18:10 by Volhout PicomiteVGA PETSCII ROBOTS |
||||
Mixtel90 Guru Joined: 05/10/2019 Location: United KingdomPosts: 6783 |
IMHO this is contrived. You shouldn't be using consecutive exponentiation without brackets to define the intention, especially in any normal programming language. You have to bear in mind that the line is going to go through a parser, not a mathematician's brain, and you are at it's mercy. Poor programming, I'm afraid, as the result isn't predictable. The answer depends on the parser, not on the maths. Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 3800 |
If you think this is bad... try C's precedence rules LOL (Peter will know what I mean...) John |
||||
zeitfest Guru Joined: 31/07/2019 Location: AustraliaPosts: 482 |
To quote (Wiki) " In most programming languages with an infix exponentiation operator, it is right-associative, that is, a^b^c is interpreted as a^(b^c).[50] This is because (a^b)^c is equal to a^(b*c) and thus not as useful. In some languages, it is left-associative, notably in Algol, Matlab, and the Microsoft Excel formula language. " This came up as an issue a few years ago when I was writing a C program to interpret Fortran source. Fortran has an operator ** for exponentiation (C doesn't, according to my ancient Kernighan and Ritchie manual) and it is indeed right-associative and can be concatenated. The shunting-yard algorithm works fine. If it works for van Dijkstra, most languages, and Google (you can paste the expression above into it and see the result), then that is good enough for me ! |
||||
toml_12953 Guru Joined: 13/02/2015 Location: United StatesPosts: 339 |
Most BASICs (and other computer languages) do exponentiation left to right. 2^3^4 = (2^3)^4 = 4096 Mathematicians, Wolfram and Python do it right to left 2^3^4 = 2^(3^4) = 2.4178516E+24 Python: 2**3**4 = 2**(3**4) = 2417851639229258349412352 Which is right? They both are! You have to decide ahead of time which system you want to follow.As long as you're consistent, you're right. Edited 2024-05-09 00:21 by toml_12953 |
||||
stanleyella Guru Joined: 25/06/2022 Location: United KingdomPosts: 2127 |
I agree, braces or brackets around the different maths to show the intention of the equation... following "standard" math? depends on the basic and what it uses I guess. |
||||
twofingers Guru Joined: 02/06/2014 Location: GermanyPosts: 1241 |
Another point: Geoff writes in his Micromite/Picomite Manual: If GW-Basic behaves as JohnS writes above: , then it should stay as it is, perhaps with a note in the manual. Regards Michael causality ≠ correlation ≠ coincidence |
||||
Volhout Guru Joined: 05/03/2018 Location: NetherlandsPosts: 4223 |
Michael, Manual ? I am sorry to say, but many issues discussed here on the form despite the fact that they are described in the manual. So it will be missed by some. Especially with something like this. Tom_12953's example is so extreme that you will catch it. But Peters original point (post 1) is a difference in the 4'th digit. This will go unnoticed (most likely). It is a matter of coding skills. Careful coding avoids the problem. Cutting corners will risk getting wrong results. And when you are tuning for speed, start with the correct syntax, and then remove brackets until the result changes. Volhout P.S. is there a note in the GW_Basic manual for this ? Guess not.... Edited 2024-05-09 04:36 by Volhout PicomiteVGA PETSCII ROBOTS |
||||
PEnthymeme Regular Member Joined: 27/12/2023 Location: United KingdomPosts: 42 |
What a fascinating thread -- learned so much... One question -- in forming an expression with brackets to clarify what I want the order of operations to be.... does adding brackets make the interpreter work harder... Does ((x+y)^3) take less time to process than ((((((x+y))))^3) --- and yes, off to test it... Px |
||||
twofingers Guru Joined: 02/06/2014 Location: GermanyPosts: 1241 |
I can agree with that. I think the authors of the GW-Basic manual may not have been aware of the problem. But that doesn't mean Peter can't do better if he thinks it makes sense. Best regards Michael Dim x,y, a,b x=2 y=2 Timer =0 For i = 1 To 100000 a=((x+y)^3) Next Print "a:"Timer Timer =0 For i = 1 To 100000 b=((((((x+y))))^3)) Next Print "b:"Timer End Result: a: 2777.573 b: 3206.915 Edited 2024-05-09 06:11 by twofingers causality ≠ correlation ≠ coincidence |
||||
Quazee137 Guru Joined: 07/08/2016 Location: United StatesPosts: 569 |
Have FUN testing in other languages FUN pick program language far right have FUN Quazee137 |
||||
JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 3800 |
I mentioned GW-BASIC a couple of times precisely because Geoff refers to it :) We could always ask Geoff what he would like to happen... John |
||||
PEnthymeme Regular Member Joined: 27/12/2023 Location: United KingdomPosts: 42 |
Beat me to it - darn you cooking for the family ;-) Interesting result - again, never really considered the impact of () on my code. Thanks for sharing. Px |
||||
TassyJim Guru Joined: 07/08/2011 Location: AustraliaPosts: 6098 |
From the Quickbasic V4.5 manual There might be a few remaining errors in the copy and paste from the PDF Numerical Recipes in C doesn't mention Power in it's table of precedence but I like this quote: Jim Edited 2024-05-09 07:01 by TassyJim VK7JH MMedit MMBasic Help |
||||
Page 2 of 3 |
Print this page |