Steve Drain
Posts: 105
Joined: Tue Oct 30, 2012 2:08 pm
Location: Exeter UK

Re: Introduction to BBC BASIC

Sun May 12, 2019 2:27 pm

RichardRussell wrote:
Sun May 12, 2019 1:39 pm
If you know the language inside-out like I do there are often big wins to be made by using 'novel' techniques, and in a library I don't mind if the code is unreadable so long as it works.
FWIW I used a few tricks to truncate my arrays so that operations were not wasted on the leading zero parts. :)

Heater
Posts: 12751
Joined: Tue Jul 17, 2012 3:02 pm

Re: Introduction to BBC BASIC

Sun May 12, 2019 2:34 pm

jahboater,
Too often I see people claiming language A is so much better than an old language B, and yet the translator and run-time library for language A is mostly written in language B.
If language A needs to use another language for its heavy lifting, then it doesn't inspire confidence.
I used to be a hard line supporter of the idea that "real" programming languages should be self hosting, they should emit native code for the machine, their compilers should be written in the same language and they should be able to rebuild themselves from that.

In recent years my attitude has softened about this. If you happen to have an assembler but no high level language perhaps the best thing to do is use that assembler to create a high level language. In that case I would argue language A, the HLL, is better than language B the assembler, for most tasks.

We can step up the level of abstraction and say, for example, if you have a C compiler the best thing you can do with it is create a higher level language, more suited to expressing solutions to the problems you have.

At this point it's not so important that the new improved language B cannot rebuild itself or uses libraries written in A.

In the extreme... if you have a C++ compiler, the best thing to do with it is use it to build a higher level language that is actually understandable. Under no circumstances use C++ to do anything else :)

User avatar
RichardRussell
Posts: 541
Joined: Thu Jun 21, 2012 10:48 am

Re: Introduction to BBC BASIC

Sun May 12, 2019 2:35 pm

Steve Drain wrote:
Sun May 12, 2019 2:13 pm
Even so, the million digit fibo was achingly slow.
As you may have seen in the other thread, the 'classic BASIC' version (which runs with only trivial modifications in BBCSDL, and probably would in ARM BASIC 6) took just over two-and-a-half hours on my RPi 3; all those GOTOs and GOSUBs can't be helping! I'd be happy to see your version because it should form a better template for me to use with my BigNum library if it ever gets written (and, yes, I am coming around to the idea that 10^n limbs are perhaps better after all).

jahboater
Posts: 4478
Joined: Wed Feb 04, 2015 6:38 pm

Re: Introduction to BBC BASIC

Sun May 12, 2019 3:47 pm

Heater wrote:
Sun May 12, 2019 2:34 pm
In the extreme... if you have a C++ compiler, the best thing to do with it is use it to build a higher level language that is actually understandable. Under no circumstances use C++ to do anything else :)
:lol: :lol: :lol:

jahboater
Posts: 4478
Joined: Wed Feb 04, 2015 6:38 pm

Re: Introduction to BBC BASIC

Sun May 12, 2019 4:01 pm

RichardRussell,

You could perhaps use GMP for testing your BBC BASIC bignum implementation.
Its easy to use.
For big numbers write results of both to a file and compare the two.

For the fibo challenge I compared hand written code results to the GMP result, in a loop for increasing N
It found a discrepancy around fibo(23000) or so (I cant remember exactly). Oddly the million digit result was fine. The bug was a rare integer overflow during a partial result. Only an exhaustive test could have found it, and GMP is fast enough for that sort of testing (within reason).

User avatar
ScriptBasic
Posts: 1254
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: Introduction to BBC BASIC

Sun May 12, 2019 4:13 pm

jahboater wrote:
Sun May 12, 2019 3:47 pm
Heater wrote:
Sun May 12, 2019 2:34 pm
In the extreme... if you have a C++ compiler, the best thing to do with it is use it to build a higher level language that is actually understandable. Under no circumstances use C++ to do anything else :)
:lol: :lol: :lol:
Why Chrome and JavaScript are on my avoid at all cost list.

jahboater
Posts: 4478
Joined: Wed Feb 04, 2015 6:38 pm

Re: Introduction to BBC BASIC

Sun May 12, 2019 4:49 pm

Heater wrote:
Sun May 12, 2019 2:34 pm
In the extreme... if you have a C++ compiler, the best thing to do with it is use it to build a higher level language that is actually understandable. Under no circumstances use C++ to do anything else :)
In the C99 Rationale document it says:-
The Committee is content to let C++ be the big and ambitious language. While some features of
C++ may well be embraced, it is not the Committee’s intention that C become C++.
Thank heaven for that!

User avatar
RichardRussell
Posts: 541
Joined: Thu Jun 21, 2012 10:48 am

Re: Introduction to BBC BASIC

Sun May 12, 2019 4:59 pm

jahboater wrote:
Sun May 12, 2019 4:01 pm
It found a discrepancy around fibo(23000) or so (I cant remember exactly). Oddly the million digit result was fine. The bug was a rare integer overflow during a partial result.
Eek, that's horrible. I honestly cannot promise to be that thorough: GMP may be fast enough to perform an exhaustive test, but my library (if I ever finish it) won't be! ;)

Heater
Posts: 12751
Joined: Tue Jul 17, 2012 3:02 pm

Re: Introduction to BBC BASIC

Sun May 12, 2019 5:13 pm

I had some similar strange things going on in my C++ version.

Also in the GNU Smalltalk version, some fibo's came our right others did not. Turned out that GST as delivered in Debian cannot multiply big numbers reliably.

Perhaps some fuzzing is required. Generate random big integer values or random numbers of digits and test those.

However, that wont find some obvious bugs, I had a bug that triggered when rolling over from 999999999 sometimes. So dreaming up a few test cases for these known corners is a good idea.

Heater
Posts: 12751
Joined: Tue Jul 17, 2012 3:02 pm

Re: Introduction to BBC BASIC

Sun May 12, 2019 5:16 pm

jahboater,
In the C99 Rationale document it says:-
Phew! Thank God for that.

I can imagine a C standards committee meeting:

A quite room with subdued lighting, full of bearded old guys sitting in comfortable chairs smoking pipes, reading though the latest C++ enhancements proposals. Chuckling quietly to themselves and mumbling "kids today" as they come across the latest twists in the ongoing train wreck that is C++.

After a few hours and a nap they all agree there is no change to C required and that those of them that are still alive will meet again next year. After one last brandy their chauffeur's arrive to take them home.

User avatar
RichardRussell
Posts: 541
Joined: Thu Jun 21, 2012 10:48 am

Re: Introduction to BBC BASIC

Sun May 12, 2019 5:39 pm

Heater wrote:
Sun May 12, 2019 5:13 pm
However, that wont find some obvious bugs, I had a bug that triggered when rolling over from 999999999 sometimes. So dreaming up a few test cases for these known corners is a good idea.
Is there a GMP test suite?

User avatar
ScriptBasic
Posts: 1254
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: Introduction to BBC BASIC

Sun May 12, 2019 5:47 pm

Is there a GMP test suite?
Feel free to use the ScriptBasic 2 line GMP fibo example for verification. The results are instant and you can redirect the output to a file.
Last edited by ScriptBasic on Sun May 12, 2019 5:52 pm, edited 1 time in total.

Heater
Posts: 12751
Joined: Tue Jul 17, 2012 3:02 pm

Re: Introduction to BBC BASIC

Sun May 12, 2019 5:48 pm

Yes:

This is the compilation process:

Code: Select all

    $ configure  --host=arm-linux  --build=i686-linux  --enable-cxx
    $ make clean
    $ make
    $ cd tests/mpz
    $ make check
See tests directory in the repo: https://gmplib.org/repo/gmp/file/tip/

jahboater
Posts: 4478
Joined: Wed Feb 04, 2015 6:38 pm

Re: Introduction to BBC BASIC

Sun May 12, 2019 7:04 pm

If you want a simple example, here is the million digit fibo code :)

Code: Select all

#include <stdio.h>
#include <gmp.h>

int
main( void )
{
  mpz_t res;
  mpz_init(res);
    
  mpz_fib_ui( res, 4784969 );

  gmp_printf( "%Zd\n", res );
}
Compile with:

cc fibo.c -o fibo -lgmp
Last edited by jahboater on Sun May 12, 2019 7:19 pm, edited 1 time in total.

User avatar
ScriptBasic
Posts: 1254
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: Introduction to BBC BASIC

Sun May 12, 2019 7:18 pm

It doesn't get more basic then that.

Looks like my fibo function in the GMP extension module.

User avatar
RichardRussell
Posts: 541
Joined: Thu Jun 21, 2012 10:48 am

Re: Introduction to BBC BASIC

Sun May 12, 2019 7:47 pm

jahboater wrote:
Sun May 12, 2019 7:04 pm
If you want a simple example, here is the million digit fibo code :)
Yes, but as I said the bottleneck is going to be how long BBC BASIC takes, so any tests are going to need to be selective rather than exhaustive. A fast GMP doesn't help with that, which is why I wondered if there's a test suite designed to catch edge cases without having to perform thousands of tests. I appreciate that its usefulness would be limited because edge cases that are likely to catch out GMP are probably different from those that might break BBC BASIC (especially if I decide to use 10^n limbs).

Heater
Posts: 12751
Joined: Tue Jul 17, 2012 3:02 pm

Re: Introduction to BBC BASIC

Sun May 12, 2019 8:21 pm

Yes, in my experience that is right, your edge cases will depend on your implementation.

One should strive to create test cases that exercise all the decisions made your code. The if's and loop conditions. They should check using numbers one up or down from whatever the condition is. It can be a pain to get good coverage.

jahboater
Posts: 4478
Joined: Wed Feb 04, 2015 6:38 pm

Re: Introduction to BBC BASIC

Sun May 12, 2019 10:20 pm

Heater wrote:
Sun May 12, 2019 8:21 pm
Yes, in my experience that is right, your edge cases will depend on your implementation.

One should strive to create test cases that exercise all the decisions made your code. The if's and loop conditions. They should check using numbers one up or down from whatever the condition is. It can be a pain to get good coverage.
The why we have "gcov" on the Pi - the test coverage program.
You keep adding tests to the test suite until the test coverage is complete as possible.
Not sure if anything like that can be done in BASIC though.

Heater
Posts: 12751
Joined: Tue Jul 17, 2012 3:02 pm

Re: Introduction to BBC BASIC

Sun May 12, 2019 10:53 pm

jahboater,

I know and love gcov.

When you are writing C++ you need the all the help of gcov and the sanitizers that you can get.
You keep adding tests to the test suite until the test coverage is complete as possible.
That is a good start.

But consider a simple conditional statement like:

Code: Select all

if (i <= 100) {
     /* something */
} else {
     /* somethingElse */
}
You can get full coverage with gcov by testing with values of "i" of 50 and 500.

BUT what if you have accidentally used "<=" instead of just "="? Or what if that "100" is not quite correct?

Well then you have a bug and gcov did not tell you.

What you need is a test that checks for correct operation with values of "i" one less and one more than the condition.

Whilst you are at it, it might be wise to check with the maximum and minimum possible values of "i". In case it has been declared signed when it should be unsigned or vice versa.

Often it's very hard to contrive such tests when the conditional is buried many levels of block/function nesting down.

Then what about all the possible overflow situations and so on...

I'm afraid it's hopeless.

jahboater
Posts: 4478
Joined: Wed Feb 04, 2015 6:38 pm

Re: Introduction to BBC BASIC

Sun May 12, 2019 11:07 pm

Heater wrote:
Sun May 12, 2019 10:53 pm
j

But consider a simple conditional statement like:

Code: Select all

if (i <= 100) {
     /* something */
} else {
     /* somethingElse */
}
You can get full coverage with gcov by testing with values of "i" of 50 and 500.

BUT what if you have accidentally used "<=" instead of just "="? Or what if that "100" is not quite correct?

Well then you have a bug and gcov did not tell you.
Its not there to find bugs. That's not in its remit. The best it can do is tell you which bits of code have been executed in the tests and which have not. Different thing altogether.

User avatar
ScriptBasic
Posts: 1254
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: Introduction to BBC BASIC

Sun May 12, 2019 11:50 pm

Richard,

The challenge was intesting and fun but trying to do a BIGINIT library from scratch for a BASIC meant for beginners seems like overkill. The 5 hour run on my RPi 3 B+ was more than I bargained for. I'm done with this and will standardize on GMP going forward.

Heater
Posts: 12751
Joined: Tue Jul 17, 2012 3:02 pm

Re: Introduction to BBC BASIC

Sun May 12, 2019 11:56 pm

jahboater,

I agree. Just say'in.

Like I said "I'm afraid it's hopeless"

User avatar
RichardRussell
Posts: 541
Joined: Thu Jun 21, 2012 10:48 am

Re: Introduction to BBC BASIC

Mon May 13, 2019 9:09 am

ScriptBasic wrote:
Sun May 12, 2019 11:50 pm
trying to do a BIGINIT library from scratch for a BASIC meant for beginners seems like overkill.
Yes BBC BASIC is meant for beginners (all BASICs are meant for beginners - it's in the name!) but crucially not only for beginners! I've listed some of the very much 'non-beginner' applications of BBC BASIC before; some can be found here. I've also said that BBC BASIC is pretty much the only programming language (apart from assembler) that I've used in the last 35 years or so, apart from dabbling in C. And don't forget that BBC BASIC is the engine behind 'LB Booster', a language that has been used to write a paid-for commercial application: Cabinet Planner.

So a BigInt library is something BBC BASIC for SDL 2.0 needs (BBC BASIC for Windows already has one). Not necessarily written in BASIC admittedly - a wrapper around GMP or similar would be fine (and easier) - but it's an interesting challenge. I may well not finish it, but that doesn't mean it's not a worthwhile exercise.

User avatar
RichardRussell
Posts: 541
Joined: Thu Jun 21, 2012 10:48 am

Re: Introduction to BBC BASIC

Mon May 13, 2019 1:32 pm

I've released a new version of BBC BASIC for SDL 2.0; the full announcement is here. A couple of changes that were suggested in this forum have been incorporated:

1. The initial 'Choose IDE' dialogue now contains the informative text "You need to select an IDE. You may want to try them both and see which you prefer".

2. The mouse scroll wheel is now ignored at the 'immediate mode' (output window) prompt, so that it doesn't result in 'surprising' characters appearing. It continues to have the expected effect in most GUI windows.

User avatar
RichardRussell
Posts: 541
Joined: Thu Jun 21, 2012 10:48 am

Re: Introduction to BBC BASIC

Mon May 20, 2019 8:29 pm

RichardRussell wrote:
Mon May 13, 2019 9:09 am
So a BigInt library is something BBC BASIC for SDL 2.0 needs (BBC BASIC for Windows already has one). Not necessarily written in BASIC admittedly - a wrapper around GMP or similar would be fine
I've got a native (100% BBC BASIC code) BigInt library to the stage where it will run the Fibonacci challenge (that is, it supports unsigned addition, subtraction and multiplication, but not division and no signed arithmetic). Disappointingly, it's currently even slower than the 'classic BASIC' solution, I think because the generic library routines are less well 'tuned' to the specific requirements of the Fibo challenge, despite taking advantage of some BBC BASIC features like array arithmetic.

I'll play a bit more, but if I can't improve the performance to at least match the classic BASIC solution I probably won't pursue it any further. A BBC BASIC wrapper to GMP would be much easier and much faster, the downside being the need to ship (or link with) GMP on every platform.

Return to “Other programming languages”