Calculating digits of Pi in Basic

bbc/electron apps, languages, utils, educational progs, demos + more
User avatar
geraldholdsworth
Posts: 765
Joined: Tue Nov 04, 2014 9:42 pm
Location: Inverness, Scotland
Contact:

Re: Calculating digits of Pi in Basic

Post by geraldholdsworth »

BigEd wrote:
Mon Oct 14, 2019 5:56 pm
Sorry, my simple-minded changes to use peek and poke instead of arrays seems to be broken code - possibly because of operator precedence.
I ran out of time to try and find the error, and I never tried your second listing.
Well, there's always tomorrow.
Gerald Holdsworth
Repton Resource Page
www.reptonresourcepage.co.uk
User avatar
geraldholdsworth
Posts: 765
Joined: Tue Nov 04, 2014 9:42 pm
Location: Inverness, Scotland
Contact:

Re: Calculating digits of Pi in Basic

Post by geraldholdsworth »

Found it...in the first listing you use an array of integers, while in the second (and third) you are using, effectively, an array of bytes.

It was easy to change in Delphi as I had a declaration:

Code: Select all

var a: array of Byte;
which produced a wrong result, but changing it to

Code: Select all

var a: array of Integer;
produced a correct result.
Gerald Holdsworth
Repton Resource Page
www.reptonresourcepage.co.uk
User avatar
jgharston
Posts: 4256
Joined: Thu Sep 24, 2009 12:22 pm
Location: Whitby/Sheffield
Contact:

Re: Calculating digits of Pi in Basic

Post by jgharston »

scruss wrote:
Mon Oct 14, 2019 1:52 am
Couldn't get it to run at all in Matrix Brandy on the same machine. It failed with Address exception at line 100. I suspect a 64 bit problem, as it seems to run fine on a Raspberry Pi.
Ran fine here on DJPP, MinGW and WinSDL.

Code: Select all

$ bbcbasic
PDP11 BBC BASIC IV Version 0.32
(C) Copyright J.G.Harston 1989,2005-2020
>_
User avatar
jgharston
Posts: 4256
Joined: Thu Sep 24, 2009 12:22 pm
Location: Whitby/Sheffield
Contact:

Re: Calculating digits of Pi in Basic

Post by jgharston »

scarybeasts wrote:
Mon Oct 14, 2019 8:33 pm
I hadn't heard of Ctrl-B before. How does that work, is future OSWRCH activity also echoed to the printer port or something like that?
Yep, VDU 2 starts a print job, and all OSWRCH goes to the VDU and the current printer (if not changed by *FX3), and VDU 3 terminates a print job, leaving OSWRCH going to just the VDU (again, if not changed by *FX3).

Code: Select all

$ bbcbasic
PDP11 BBC BASIC IV Version 0.32
(C) Copyright J.G.Harston 1989,2005-2020
>_
User avatar
BigEd
Posts: 3739
Joined: Sun Jan 24, 2010 10:24 am
Location: West Country
Contact:

Re: Calculating digits of Pi in Basic

Post by BigEd »

There's a bug of sorts, which had me worried, but it's not so bad: it turns out that the program won't print trailing 9s. So if you ask for 768 digits (as well you might) you only get 762 - the 6 trailing 9s are not printed. But if you ask for 769 digits, all is well! And on a Master with a Pi Zero emulating a very fast 6502 second processor, that will take a tad less than 85 seconds. Or just three and a half seconds with the Pi running native ARM BAS135.
User avatar
scarybeasts
Posts: 603
Joined: Tue Feb 06, 2018 7:44 am
Contact:

Re: Calculating digits of Pi in Basic

Post by scarybeasts »

BigEd wrote:
Mon Oct 14, 2019 11:18 am
Yes, paste from clipboard was a big help. But I made the tactical error of pasting the Sharp Basic original and then fixing it up within JSBeeb, instead of working on a master copy and re-importing it. So the other half of the function, copy-from-printer, would be very handy.
This was such a good idea that I had to implement it. It's now live on https://bbc.godbolt.org/

Ctrl-B will now activate the printer on the beeb as usual, but also pop open a new browser text window where the printer output is echoed to.


Cheers
Chris
User avatar
BigEd
Posts: 3739
Joined: Sun Jan 24, 2010 10:24 am
Location: West Country
Contact:

Re: Calculating digits of Pi in Basic

Post by BigEd »

Most excellent, thanks!

Is there a way to make this discoverable? I see there's now a Virtual MHz readout which is extra info: possibly we could have "Printer: Offline" text nearby? Or maybe the Help panel?
Soruk
Posts: 847
Joined: Mon Jul 09, 2018 11:31 am
Location: Basingstoke, Hampshire
Contact:

Re: Calculating digits of Pi in Basic

Post by Soruk »

scruss wrote:
Mon Oct 14, 2019 1:52 am
jgharston wrote:
Sun Oct 13, 2019 2:23 pm
Timing just the calculation, 50 digits in 44 seconds:
Or in BBC BASIC for SDL 2 on a i7-4790K @ 4.00GHz, 4400 digits in 44 seconds.

Couldn't get it to run at all in Matrix Brandy on the same machine. It failed with Address exception at line 100. I suspect a 64 bit problem, as it seems to run fine on a Raspberry Pi.

A Raspberry Pi 4 manages 5000 digits in 44 seconds.
Very late to this party, but it seems to be a bug that has since been fixed, it runs fine on 32-bit and 64-bit on V1.22.3 (or the current pull from git).

Edit: A 32-bit Linux VM running under VirtualBox on an AMD FX8350 (2013 vintage) at 4GHz does 4400 digits in 17 seconds. A RasPi 2 at 900MHz takes 183 seconds and a 1.6GHz Atom 93 seconds. Using the 'tbrandy' build as the SDL output is relatively slow compared to outputting to a terminal window.
Matrix Brandy BASIC VI (work in progress)
Kweepa
Posts: 31
Joined: Mon Dec 16, 2013 11:45 pm
Contact:

Re: Calculating digits of Pi in Basic

Post by Kweepa »

https://twitter.com/Kweepa/status/1285723677170913280
50 digits in around 20 seconds.
Algorithm from:
https://rosettacode.org/wiki/Pi#BASIC_version

Code: Select all

1MO.2:M=192:DIMB%(M):V.5:A=0:F.I=0TOM:B%(I)=20:N.:F.C%=M TO14S.-7:D%=0:A%=C%+C%-1:F.P%=C%TO1S.-1:D%=D%*P%+B%(P%)*100:B%(P%)=D%MODA%:D%=D%DIVA%:A%=A%-2:N.
6N=E%+D%DIV100:U=N DIV10:F.I=0TO1:MOVE608+420*SINA,528+420*COSA:GC.0,RND(7):P.;U;:A=A+.121:U=N MOD10:N.:E%=D%MOD100:N.:V.1
User avatar
BigEd
Posts: 3739
Joined: Sun Jan 24, 2010 10:24 am
Location: West Country
Contact:

Re: Calculating digits of Pi in Basic

Post by BigEd »

A splendid presentation of the result!
Edit: and the bizarrely encoded reply with rotated text is remarkable indeed, but opaque.
Kweepa
Posts: 31
Joined: Mon Dec 16, 2013 11:45 pm
Contact:

Re: Calculating digits of Pi in Basic

Post by Kweepa »

Details of the compression are here: https://www.dompajak.com/bbcmicrobot.html
Here's the uncompressed code:

Code: Select all

0MO.4:?6548=16:M=126:DIMB%(M):F.C%=M TO14S.-7:D=0:A=C%*2-1:F.P%=C%TO1S.-1:D%=D%*P%+B%(P%)*100+2E3:B%(P%)=D%“A-20:D%=D%/A:A=A-2:N.:N%=E%+D%/100:U=N%‘10:F.I=0TO1:K=K+.18:x=64*COSK:y=-64*SI.K:PL.4,608-7*y,528+7*x
1F.Y=0TO7:V=Y?(49280+U*8):V.1559;((16V)*48+(32V)*96+(64V)*192+(128V)*384)‘256,(1V)*3+(2V)*6+(4V)*12+(8V)*24,0;0;0;5:PL.17,x,y:PL.0,-x+y/8,-x/8-y:N.:U=N%“10:N.:E%=D%“100:N.:V.1
Unfortunately he's using single characters to encode some keywords for further compression, but you get the idea :(
Post Reply

Return to “8-bit acorn software: other”