Math bug?

Anything Sinclair ZX Basic related; history, development, tips - differences between BASIC on the ZX80 and ZX81
Post Reply
Andrei Khokhlov
Posts: 3
Joined: Mon Oct 02, 2023 4:57 pm

Math bug?

Post by Andrei Khokhlov »

Some time ago, I decided to understand the floating point arithmetic and wrote a simple test that calculates the area under half the period of the sine function. It's not complete, but some results can be seen here http://avhohlov.narod.ru/p9882en.htm. Test may be done also on old home computers and programmable calculators.

On real ZX81 at Yandex Museum I get wrong result and I found that code listed below print 3 instead of 1:

Code: Select all

10 LET R = 1
20 LET A = -1E-13
30 PRINT R + A
40 STOP
Value -1E-13 may be changed to -1E-10, but not -1E-8 and not +1E-13

Emulators (EightyOne Sinclair Emulator (https://sourceforge.net/projects/eighty ... -emulator/) and JtyOne (http://www.zx81stuff.org.uk/zx81/jtyone.html) print correct result.

Can someone test this on a real ZX-81?

Full code:

Code: Select all

100 LET P=3.141592654
110 LET S=0
120 LET N=100
130 LET I=0
140 GOSUB 190
150 LET I=I+1
160 IF I<N THEN GOTO 140
170 PRINT P*S/N
180 STOP
190 LET X=P*I/N
200 GOSUB 230
210 LET S=S+R
220 RETURN
230 LET R=X
240 LET A=X
250 LET Y=X*X
260 LET K=1
270 LET A=-A*Y/((K+1)*(K+2))
280 LET R=R+A
290 LET K=K+2
300 IF ABS(A)>1E-16 THEN GOTO 270
310 RETURN
Last edited by Andrei Khokhlov on Wed Oct 04, 2023 2:41 pm, edited 1 time in total.
User avatar
1024MAK
Posts: 5118
Joined: Mon Sep 26, 2011 10:56 am
Location: Looking forward to summer in Somerset, UK...

Re: Math bug?

Post by 1024MAK »

The first ZX81 ROM does indeed have a bug in the mathematical routines. It’s often referred to as the SQRT bug.

Sinclair’s first “fix” was to add a small daughter board that effectively overrides the ROM (as far as the processor is concerned).
Then Sinclair produced a second ROM version, which had another bug (not mathematical related). Finally, a third and final ROM was produced.

Hence a ZX81 may have:
  • The first ROM version with the maths bug.
  • The first ROM version with the maths bug, but with the daughter board to fix the maths bug.
  • The second ROM version.
  • The third ROM version.
Even after “fixing” the bug, rounding errors are still possible due to the way the mathematical routines work.

There is also a modern improved ROM which some users have fitted.

Mark
ZX81 Variations
ZX81 Chip Pin-outs
ZX81 Video Transistor Buffer Amp

:!: Standby alert :!:
There are four lights!
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :!:
Looking forward to summer later in the year.
Moggy
Posts: 3267
Joined: Wed Jun 18, 2008 2:00 pm

Re: Math bug?

Post by Moggy »

Looking at your test code I see in the first line of your full code that you are working to 10 figures. The ZX81 is only accurate to 9.5 figures and displays just 8 of those.

As most of the ROM mathematical problems stem from a rounding error in the 32nd bit, this may also be causing an error apart from bugged ROM code.

Running your short piece of code on a real ZX81 returns 1 when used with the later edition of the ZX81 ROM but returns 3 when used with the early version which suggests your ZX81 has the early bugged ROM fitted, your full code returns a 2/300 error when run.

Some relevant information regarding the maths errors in the ROM can be found at these links.


https://archive.org/stream/ZX-computing ... 3/mode/2up

https://archive.org/stream/ZX-computing ... 8/mode/1up

https://archive.org/stream/ZX-computing ... 7/mode/1up


The daughter board "fix" didn't work for all mathematical cases and still produced erroneous results not helped by the bugged ROM code already
mentioned.

Also your first link shows as a dangerous page in my browser.....

"We think this site may be fraudulent or contain viruses that could be harmful to your computer, so it's been blocked by your account's Web Safe settings."
Andrei Khokhlov
Posts: 3
Joined: Mon Oct 02, 2023 4:57 pm

Re: Math bug?

Post by Andrei Khokhlov »

Yes, my test code implicitly assumes double precision arithmetic (REAL*8), but ZX81 arithmetic is REAL*5 (32-bit for mantissa). So error 1E-16 unnecessary small. With 1E-8 as error limit result much more correct, but it differ from built-in sin

Code: Select all

2.0980103 vs 1.9998355 (built-n sin)
Maybe I would assume that this is the result of minor errors, but with 1E-16 limit I get result ~10.
Moggy
Posts: 3267
Joined: Wed Jun 18, 2008 2:00 pm

Re: Math bug?

Post by Moggy »

Another example of how screwed up some parts of the maths in the ROM are,enter the following and see.


PRINT 1/2 - 0.5
PRINT 0.5 - 1/2

The erroneous answer in the first example is thought to be because 0.5 is converted to one bit less than its actual true value during any mathematical evaluation.

This error is apparent in all ROM versions, however the newest enthusiast incarnation known as the Big bang ROM does not suffer from this error.

If it's any consolation, the Cray MK1 couldn't divide 15 by 3 apparently! :lol:

You may find this paper from the National physical laboratory enlightening.

https://apps.dtic.mil/sti/tr/pdf/ADA142656.pdf


NPL Report DITC 18/83
February 1983
NATIONAL PHYSICAL LABORATORY
A note on the accuracy of
two microprocessors
by
B A Wichmann
Division of Information Technology and Computing
Abstract
'An analysis has been performed of the accuracy of the mathematical
functions in the Basic monitor of the ZX81 and BBC microprocessors. The
tests used for this are those that are part of the NPL/University of
Tasmania Pascal validation suite. The results of these tests shows that
even the smallest of microprocessors can be expected to give
mathematically sound values for the standard functions
Andrei Khokhlov
Posts: 3
Joined: Mon Oct 02, 2023 4:57 pm

Re: Math bug?

Post by Andrei Khokhlov »

I was inattentive, in EightyOne emulator ROM may be selected and with zx81.edition1.rom errors present.
Post Reply