Bugs in log and trig functions of Basic 4r32

discussion of beeb/electron applications, languages, utils and educational s/w
Post Reply
User avatar
BigEd
Posts: 1814
Joined: Sun Jan 24, 2010 10:24 am
Location: West
Contact:

Bugs in log and trig functions of Basic 4r32

Post by BigEd » Fri Sep 18, 2015 11:40 am

I set out to investigate the accuracy of 4 different versions of BBC Basic, from 1982 to 1988, and discovered - horror - some major inaccuracies in the 1988 version, 4.32. I found these problems in emulation and then checked on a real machine.

I hope no-one was planning on designing any rockets or nuclear reactors with this version of Basic!

Here are some failing cases:

Code: Select all

Function        Basic 4r32     Correct value
P.LN(1.03125)   3.07692308E-2  3.07716587E-2
P.ASN(0.03125)  3.126527E-2    3.12550885E-2
P.ACS(0.03125)  1.53953106     1.53954124
P.ATN(0.9375)   0.753140099    0.753151281
In case it's interesting, what I did was cook up some identities, so for example

Code: Select all

X-LOG(EXP(X))
should mathematically be zero. So if we compute lots and add up the errors, we see how far out the 5-byte floats are. And comparing the 4 versions, we can see a general improvement in accuracy followed by a catastrophe:

Code: Select all

E=0:FORI=0.03125TO1.5STEP.0625:E=E+ABS(ACS(COS(I))-I)/I:N.:P.E
1.43308147E-7
1.24047257E-7
1.17482481E-7
3.25589539E-4
We ought to get a good 8 digits of accuracy from each calculation. But in 4r32 it can be much worse - so I looked for particular values which gave large errors:

Code: Select all

>P.LN(EXP(2.125))
2.12499212
>P.ASN(SIN(0.03125))
3.12601765E-2
>P.ACS(COS(0.03125))
3.12601733E-2
>P.TAN(ATN(0.9375))
0.93747899
For most calculations I tried, Basic 4.8 and 4r32 give exactly the same answers. So I suspect what happened is a small tweak intended to improve performance which actually introduced an accuracy bug. I might dig into the code to see what's changed.

duikkie
Posts: 2852
Joined: Fri Feb 07, 2014 3:28 pm
Contact:

Re: Bugs in log and trig functions of Basic 4r32

Post by duikkie » Fri Sep 18, 2015 12:49 pm

i will contact Abdul Khan :)

User avatar
BigEd
Posts: 1814
Joined: Sun Jan 24, 2010 10:24 am
Location: West
Contact:

Re: Bugs in log and trig functions of Basic 4r32

Post by BigEd » Mon Sep 21, 2015 11:27 am

?? I don't think I understand the reference.

I just checked the BBC BASIC versions for Windows and DOS on x86 and they don't have these defects. Also checked the Z80 version on my Amstrad NC100 and that's OK too.

User avatar
BigEd
Posts: 1814
Joined: Sun Jan 24, 2010 10:24 am
Location: West
Contact:

Re: Bugs in log and trig functions of Basic 4r32

Post by BigEd » Sat Aug 27, 2016 11:16 am

Just checked the 1986 Basic V 1.00 for ARM Second Processor - for the specific tabulated test cases, it gets the right answers. For the 4 identities, it's better than 112 parts per billion:
1.11160466e-7 acs(cos())
6.69610005e-9 asn(sin())
1.43862392e-8 ln(exp())
9.80659047e-9 tan(atn())

User avatar
boba
Posts: 92
Joined: Mon Jul 16, 2012 7:43 pm
Location: The Kingdom of Fife
Contact:

Re: Bugs in log and trig functions of Basic 4r32

Post by boba » Sun Aug 28, 2016 8:15 am

BigEd wrote:I hope no-one was planning on designing any rockets or nuclear reactors with this version of Basic!
BigEd wrote:?? I don't think I understand the reference.
Father of Pakistan's nuclear programme.
Probably didn't use a Beeb though.

User avatar
BigEd
Posts: 1814
Joined: Sun Jan 24, 2010 10:24 am
Location: West
Contact:

Re: Bugs in log and trig functions of Basic 4r32

Post by BigEd » Sun Aug 28, 2016 8:34 am

Aha! Thank you!

(Although we did hear last night that the UK's Atomic Weapons Establishment used some Beeb-related computational capability.)

User avatar
daveejhitchins
Posts: 4139
Joined: Wed Jun 13, 2012 5:23 pm
Location: Newton Aycliffe, County Durham
Contact:

Re: Bugs in log and trig functions of Basic 4r32

Post by daveejhitchins » Sun Aug 28, 2016 9:20 am

During my time at Filtronic, when we were based in Charlestown, Shipley, we had a systems department (separate from the Defence department [Components]) that dealt with some unknown military group (they usually arrived in black cars with 'smoky' windows :shock: ) Both our department and their group used Beebs and lots of custom software, that we produced for them. That was the time that two techies, from Acorn, arrived to demonstrate the ARM second processor. Unfortunately, we couldn't justify the expenditure :? Which was a huge disappointment, at the time :(

Dave H :D
Parts: UM6502CE, GAL22V10D, GAL16V8D, AS6C62256A, TC514400AZ, WD1772, R6522, TMS27C512, AT28C256
Products: ARA II, ABR, ATI, AP6, MGC, AP5 . . .
For a price list, contact me at: Retro Hardware AT dave ej hitchins DOT plus DOT com

User avatar
BeebMaster
Posts: 2537
Joined: Sun Aug 02, 2009 4:59 pm
Location: Lost in the BeebVault!
Contact:

Re: Bugs in log and trig functions of Basic 4r32

Post by BeebMaster » Sun Aug 28, 2016 12:13 pm

I think the accuracy of mathematical functions was a known bug and subject of continual improvement throughout the development of BBC BASIC. There is a summary of the changes throughout the years, including the accuracy of LN, in Appendix H of the Archimedes 400 series BBC BASIC User Guide, which I can't find online, but there is an HTML version of the later edition where it is is found at Appendix I here.

There are other information sources as well, I can't place my hands on them just at the moment, but I think there is an Acorn Application Note about different versions of BASIC, also I believe there is a BASIC II user guide pamphlet, one for BASIC III (ie. HIBASIC for the second processor rather than the US version of normal processor BASIC) and the manual for the MOS 3.50 upgrade also details the differences between BASIC 4 and BASIC 4r32.
Image

User avatar
BigEd
Posts: 1814
Joined: Sun Jan 24, 2010 10:24 am
Location: West
Contact:

Re: Bugs in log and trig functions of Basic 4r32

Post by BigEd » Wed Jun 13, 2018 10:03 am

Just to note: the adventure continued, with some follow-up here:
viewtopic.php?p=201814#p201814

Post Reply