Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 10:38 01 Aug 2025 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 : Sugestion: a small basic test program to test the basic interpreter

     Page 1 of 2    
Author Message
nbrok

Regular Member

Joined: 13/02/2023
Location: Netherlands
Posts: 60
Posted: 06:51am 09 Mar 2023
Copy link to clipboard 
Print this post

@peter and other picobasic maintainers,

You do a great job adding new features to mmbasic but sometimes a little bug is creeping into the software which is not always direct visible. Sometimes it is not discovered yet by users. The triangle was one of them, a typo is easy made in such a large project. Now many people discovered strange behavior of picobasic when doing print -2^1.5 . Sometimes it crashes completely or reboot with erasing the flash memory. The more functions added means lesser memory to use for programs. My experience with high level compilers is that their use of memory isn't as efficient as mostly wanted in embedded systems. But you probably know this already ;-)
In another project (roscom68k) we got also strange problems due the C compiler. Version X does destroy the variable stack and version y does overwrite something.... or integers aren't reserved correctly (16 bits in stead of 32 bits etc.) It is an example to tell that bugs can also be introduced by a newer C compiler version. :-(
So creating a small program to test a new compiled version will show you what is changed. Did it work without problems in version X and in version X+1 not, then you know something changed into the wrong way.
I tell you this because I've a lot of experience in programming for embedded systems. Because I used only 8 bit embedded systems I program in assembly but with the new 16bit+ controllers A higher programming language is comfortable, because a good assembly code description is rarely available for those modern processors.
A basic interpreter as big as mmbasic is, is also better programmable in a higher programming language as C.
I hope you understand this, I don't want to criticize your work.
Greetings,
Nick de pe1goo
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 5089
Posted: 08:18am 09 Mar 2023
Copy link to clipboard 
Print this post

Hi Nick,

Tom (thwill) has written a test suite for the CMM2 that goes through most of the commands. It is not a small program.
Understand that there are 128 commands, 128 functions, and many of them have "sub functions". A small program would be thousands of lines of code.

And still you would not be able to cope with all the oddities like -2^1.5

Volhout
PicomiteVGA PETSCII ROBOTS
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2170
Posted: 09:44am 09 Mar 2023
Copy link to clipboard 
Print this post

  Volhout said  
... oddities like -2^1.5


curious. the microsoft calculator also freaks on that (so probably based on the same/similar IEEE FP format)

Wolfram Alpha is happy enough

Input
-2^1.5
Result
-2.82843...
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10310
Posted: 09:51am 09 Mar 2023
Copy link to clipboard 
Print this post

  Quote  Wolfram Alpha is happy enough


Isn't that just wrong? That may be the real part of what is actually a complex number but it shouldn't be presented as the correct answer.

I will put a trap in MMbasic. FYI the C exp function returns INFINITY for this case
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2170
Posted: 10:31am 09 Mar 2023
Copy link to clipboard 
Print this post

  matherp said  

Isn't that just wrong? That may be the real part of what is actually a complex number but it shouldn't be presented as the correct answer.

I will put a trap in MMbasic. FYI the C exp function returns INFINITY for this case


I must say I don't really understand enough about what is going on here - I am "suspicious" because the result has the square root of two in it (always tweaks my spidey-sense) but beyond that I don't see any reason why it shouldn't compute. Happy to accept this is my ignorance not evidence of a bug

on the subject of trapping it, I'd say you already do (Overflow)
Edited 2023-03-09 20:33 by CaptainBoing
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2640
Posted: 10:53am 09 Mar 2023
Copy link to clipboard 
Print this post

" I don't see any reason why it shouldn't compute"

Dragging memories from deep time, In Pure Maths -1^0.5 was an "imaginary number" while in Applied Maths it was a "complex number", a practical contrivance to get a shortcut to the right result (like calculus) but not it's self having a value that can be calculated. Any negative number raised to a non-integer power is likely to have the same problem.
 
toml_12953
Guru

Joined: 13/02/2015
Location: United States
Posts: 442
Posted: 10:56am 09 Mar 2023
Copy link to clipboard 
Print this post

  matherp said  
  Quote  Wolfram Alpha is happy enough


Isn't that just wrong? That may be the real part of what is actually a complex number but it shouldn't be presented as the correct answer.

I will put a trap in MMbasic. FYI the C exp function returns INFINITY for this case


In many BASICs, including the ANSI/ISO Standard for Full BASIC, exponentiation has priority over negation (or unary minus) so

-2^1.5

is evaluated as -(2^1.5)

which gives -2.828427...
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4044
Posted: 10:58am 09 Mar 2023
Copy link to clipboard 
Print this post

  CaptainBoing said  
  Volhout said  
... oddities like -2^1.5


curious. the microsoft calculator also freaks on that (so probably based on the same/similar IEEE FP format)

Wolfram Alpha is happy enough

Input
-2^1.5
Result
-2.82843...

You may have meant (-2)^1.5

Which is not possible in solely real (floating point).

I think WA is seeing it as -(2^1.5), which is OK.

John
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2170
Posted: 11:13am 09 Mar 2023
Copy link to clipboard 
Print this post

  JohnS said  
I think WA is seeing it as -(2^1.5), which is OK.


I tested for that as soon as Peter threw some shade on it considering 2^1.5 is the same (without the negate) which made me very suspicious. Annoyingly (-2)^1.5 gives the same result, so WA seems to be insistent and now I don't know what to think especially as Excel throws a wobbler on it as well.

By consensus alone, WA is starting to look a bit dodgy which really irks me. I might just gnaw a leg off in desperation.
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10310
Posted: 11:17am 09 Mar 2023
Copy link to clipboard 
Print this post

In all versions of MMbasic unary operators take precedence over everything else

i.e. negation NOT and where applicable INV
Edited 2023-03-09 21:20 by matherp
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4044
Posted: 11:18am 09 Mar 2023
Copy link to clipboard 
Print this post

When I used WA with (-2)^1.5 it gave me a complex number, which is OK.
edit: just did it again and yep, complex number.

Strictly, C's exp with the bad input gives a "domain error" (which may but need not set errno to EDOM) and you get an implementation-defined result value.

The idea is that you never pass in a bad number but if you do you can get almost anything back (though things such as SIGFPE appear not to be allowed).

For many languages -2^1.5 would be -(2^1.5) and that's OK for MMBasic if written that way.

John
Edited 2023-03-09 21:23 by JohnS
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10310
Posted: 11:22am 09 Mar 2023
Copy link to clipboard 
Print this post

John

The "bug" on the RP2040 is that exp was not setting errno which it does on every other version, not does it throw a FP exception. This means I have had to add a test for it returning "inf"
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4044
Posted: 11:24am 09 Mar 2023
Copy link to clipboard 
Print this post

  matherp said  John

The "bug" on the RP2040 is that exp was not setting errno which it does on every other version, not does it throw a FP exception. This means I have had to add a test for it returning "inf"

Lovely - not!

Any fix is a fix, so good :)

(As I posted, looks like an FP exception is expressly prohibited by the C standard.)

John
Edited 2023-03-09 21:25 by JohnS
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 7937
Posted: 11:32am 09 Mar 2023
Copy link to clipboard 
Print this post

Easily sorted. Don't use silly maths. Check your bounds first. :)
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2170
Posted: 12:00pm 09 Mar 2023
Copy link to clipboard 
Print this post

 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4311
Posted: 12:10pm 09 Mar 2023
Copy link to clipboard 
Print this post

  Volhout said  Tom (thwill) has written a test suite for the CMM2 that goes through most of the commands. It is not a small program.
Understand that there are 128 commands, 128 functions, and many of them have "sub functions". A small program would be thousands of lines of code.


That's somewhat overselling my work.

I have a set of automated MMBasic integration tests that run on CMM2, MMB4W and MMB4L and probably cover 50%+ of the language fundamentals, but nothing in the GPIO, graphics or audio area. For what it is worth those tests sum to about 180K of MMBasic; however my style is verbose so I imagine you could rewrite them in less than half that.

I will add a test for -2^1.5 to them ;-)

Currently the test-suite doesn't run on the PicoMites because they don't support #INCLUDE, MM.INFO(CURRENT) and (at least until recently) MM.CMDLINE$. However I believe I have workarounds in hand and will be pursuing this in the background over the next few months, at which point perhaps I'll be posting a handful of minor bugs at edge conditions for Peter to look at ... though I may just fix them myself going forward.

Best wishes,

Tom
Edited 2023-03-09 22:17 by thwill
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4044
Posted: 11:54am 10 Mar 2023
Copy link to clipboard 
Print this post

IIRC, MMBasic was highly influenced by GW-Basic, so does that Basic parse -2^1.5 in the same way? It's unusual, to say the least.

(Either way, Peter's fix for exponential is good & needed to stop a Pico crash.)

John
 
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 420
Posted: 08:46pm 10 Mar 2023
Copy link to clipboard 
Print this post

  CaptainBoing said  
  matherp said  

Isn't that just wrong? That may be the real part of what is actually a complex number but it shouldn't be presented as the correct answer.

I will put a trap in MMbasic. FYI the C exp function returns INFINITY for this case


I must say I don't really understand enough about what is going on here - I am "suspicious" because the result has the square root of two in it (always tweaks my spidey-sense) but beyond that I don't see any reason why it shouldn't compute. Happy to accept this is my ignorance not evidence of a bug on the subject of trapping it, I'd say you already do (Overflow)


Oddly, just taught this to some students as part of GCSE revisions lessons.  

If we deconstruct what's going on, we have an exponent, so we need to evaluate -2 raised to the power of 1.5. However, there is an ambiguity in the way the expression is written because exponentiation has a higher precedence than negation (by definition in the way we teach it). That means that, technically, the expression is evaluated as -(2^(1.5)), not (-2)^(1.5).

Therefore, we evaluate 2^(1.5) first, which is approximately equal to 2.828. Then we can apply the negation, so the final result is approximately equal to -2.828.

Sorry to go all treachery on this one.  

We use:

1 - Parentheses
2 - Exponents and Roots
3 - Multiplication
4 - Division
5 - Addition
6 - Subtraction

PEMDAS  -- so the exponents are higher priority than multiplication -2 is -1 x 2 - so it has priority 3.

If we wanted -2^1.5 to apply the exponent to the -2 -- we should write (-2)^1.5

I'll get back in my box now  ;-)

Edit:  Implying the existence of brackets is the bane of my life... ;-)

N
Edited 2023-03-11 06:49 by Nimue
Entropy is not what it used to be
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4311
Posted: 09:00pm 10 Mar 2023
Copy link to clipboard 
Print this post

Like MMBasic it appears unary minus (and the other unary operators) has the highest operator precedence in BBC Basic.

And when I tried to discuss it with my Maths teacher wife she just wanted to have an argument and disown me for using the term "minus" rather than "negative" or "subtract" .

Best wishes,

Tom
Edited 2023-03-11 07:29 by thwill
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4044
Posted: 09:09pm 10 Mar 2023
Copy link to clipboard 
Print this post

  thwill said  Like MMBasic it appears unary minus (and the other unary operators) has the highest operator precedence in BBC Basic.

Best wishes,

Tom

Simple hand-written code (aka an ad hoc parser) to do the parsing is something like:

see the minus, remember it
see the number
do the minus
see the exponential operator (the ^)
etc.

A grammar-driven parser (*) would normally use the mathematical rules (along the lines Nimue posted).

And thus the different outcomes.

So long as you know which language implementation does what and don't keep swapping between them, no problem.

(*) left as an exercise for the reader

John
 
     Page 1 of 2    
Print this page
The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025