Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 16:51 25 Nov 2024 Privacy Policy
Jump to

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: Netherlands
Posts: 4223
Posted: 07:02am 07 May 2024
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 6783
Posted: 07:59am 07 May 2024
Copy link to clipboard 
Print this post

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: Germany
Posts: 554
Posted: 06:19pm 07 May 2024
Copy link to clipboard 
Print this post


Edited 2024-05-08 04:21 by Plasmamac
Plasma
 
zeitfest
Guru

Joined: 31/07/2019
Location: Australia
Posts: 482
Posted: 11:32pm 07 May 2024
Copy link to clipboard 
Print this post

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 States
Posts: 118
Posted: 02:48am 08 May 2024
Copy link to clipboard 
Print this post

  zeitfest said  Interesting thread.  What is the result on maximites or early micromites  ?


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 Kingdom
Posts: 3800
Posted: 06:29am 08 May 2024
Copy link to clipboard 
Print this post

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: Netherlands
Posts: 4223
Posted: 08:01am 08 May 2024
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 6783
Posted: 08:21am 08 May 2024
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 3800
Posted: 08:29am 08 May 2024
Copy link to clipboard 
Print this post

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: Australia
Posts: 482
Posted: 12:29pm 08 May 2024
Copy link to clipboard 
Print this post

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 States
Posts: 339
Posted: 02:16pm 08 May 2024
Copy link to clipboard 
Print this post

  matherp said  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


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 Kingdom
Posts: 2127
Posted: 02:29pm 08 May 2024
Copy link to clipboard 
Print this post

  Mixtel90 said  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.

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: Germany
Posts: 1241
Posted: 03:02pm 08 May 2024
Copy link to clipboard 
Print this post

Another point: Geoff writes in his Micromite/Picomite Manual:
  Quote  Compatibility
MMBasic implements a large subset of Microsoft's GW-BASIC. [...]
If GW-Basic behaves as JohnS writes above:
  Quote  GW-Basic.exe running under DOSBOX

? 3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3
3.001953
, 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: Netherlands
Posts: 4223
Posted: 06:33pm 08 May 2024
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 42
Posted: 07:52pm 08 May 2024
Copy link to clipboard 
Print this post

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: Germany
Posts: 1241
Posted: 07:59pm 08 May 2024
Copy link to clipboard 
Print this post

  Volhout said  [...]
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.

I can agree with that.

  Quote  P.S. is there a note in the GW_Basic manual for this ? Guess not....

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

  Quote  Does ((x+y)^3)  take less time to process than ((((((x+y))))^3)  --- and yes, off to test it...


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 States
Posts: 569
Posted: 08:05pm 08 May 2024
Copy link to clipboard 
Print this post

Have FUN testing in other languages
FUN

pick program language far right


have FUN

Quazee137
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3800
Posted: 08:36pm 08 May 2024
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 42
Posted: 08:47pm 08 May 2024
Copy link to clipboard 
Print this post

  twofingers said  

Result:
a: 2777.573
b: 3206.915


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: Australia
Posts: 6098
Posted: 08:58pm 08 May 2024
Copy link to clipboard 
Print this post

From the Quickbasic V4.5 manual
  Quote  BASIC evaluates mathematical expressions from left to right, following the rules of algebraic precedence: exponentiation is performed first, then multiplication and division, then addition and subtraction. The following example program illustrates algebraic precedence:
Type the following line in the Immediate window, then press ENTER:
PRINT 2*3+2^3-2/3
The output should appear as follows:
13.33333
Parentheses can confirm or override the normal precedence. Try this example:
Type the following line in the Immediate window, then press ENTER:
PRINT (2 * 3) + (2 ^ 3) - (2 / 3)
The output should appear as follows:
13.33333
2. Type CLS in the Immediate window and press ENTER to clear the output screen.
The result is the same as in the last example because the precedence of operations dictated by the parentheses is no different from the usual order. However, the next example program produces a different result:
Type the following line in the Immediate window, then press enter:
PRINT 2 * (3+2) ^ (3-2/3)
The output should appear as follows:
85.49879
2. Type CLS in the Immediate window and press ENTER to clear the output screen.
The result here is different because the calculations within parentheses are per¬ formed first. Within parentheses, the usual precedence still applies, of course. The expression (3-2/3) evaluates as (3-0.6666667), not (1/3).
You might want to use parentheses to control the order of calculations, rather than depending on precedence. Parentheses prevent errors and make your programs easier to understand.

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:
  Quote  Every programmer is occasionally tempted to write a line or two of code that is
so elegantly tricky that all who read it will stand in awe of its author’s intelligence.
Poetic justice is that it is usually that same programmer who gets stumped, later on,
trying to understand his or her own creation.


Jim
Edited 2024-05-09 07:01 by TassyJim
VK7JH
MMedit   MMBasic Help
 
     Page 2 of 3    
Print this page
© JAQ Software 2024