20.02.2013 Views

ZX Computings - OpenLibra

ZX Computings - OpenLibra

ZX Computings - OpenLibra

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

I first became aware of the bug<br />

in the Sinclair 8K ROM after<br />

writing a fairly fast BASIC program<br />

to obtain the smallest<br />

prime factor of any odd number<br />

up to 2 32 - 1 in a matter of<br />

seconds rather than minutes.<br />

When I tried the program out on<br />

2 32 - 5, the largest prime<br />

number in this range, it seemed<br />

to be taking a long time to run.<br />

On breaking in and checking the<br />

divisor, I found that it had gone<br />

well past the square root of the<br />

number (about 65536). On<br />

checking the square root, I<br />

found that the <strong>ZX</strong>81 was<br />

holding a 12 digit number {instead<br />

of 65536) as the square<br />

root of the 10 digit number<br />

2 32 - 5.<br />

Further investigation revealed<br />

errors in exponentiation and<br />

even in subtraction. In the<br />

event all these errors were to<br />

trace back to a programming<br />

mistake in subtraction. To see<br />

how they arose, it is best to<br />

follow a simple example<br />

through to its source.<br />

The <strong>ZX</strong>81 gives SQR {%)<br />

correctly as .5. But SQR .25 is<br />

given as 1.3591409. Listing<br />

the ROM shows that SQR X is<br />

evaluated as X**.5. The instruction<br />

"PRINT .25**.5"<br />

gives the same wrong answer<br />

1.3591409. The function<br />

X**Y in turn is evaluated as<br />

EXP (Y • LN X). The instruction<br />

"PRINT LN .25" returns the<br />

answer 0.61370564 instead<br />

of - 1.3862944. Two has<br />

been added to the correct<br />

answer. At this stage we could<br />

well suspect that addition is at<br />

fault.<br />

The last stage of detection<br />

requires one to split the LN<br />

routine in two, by uploading it<br />

into RAM and running it there.<br />

Happily this works. At least the<br />

first half runs and that is all we<br />

need. To evaluate LN X the<br />

<strong>ZX</strong>81 has to add the log of the<br />

exponent and the log of the<br />

mantissa of X. In the case of<br />

.25, the log of the exponent is<br />

correctly evaluated as - 2 log<br />

insight<br />

Frank O'Hara looks at the bug in the<br />

original 8K ROM, and explains how it<br />

occurred. He reveals a new bug which<br />

only comes to light when running a<br />

zx printer, and gives a checking<br />

routine which prints the powers of<br />

two from two squared to two to the<br />

32nd, exactly.<br />

2. The log of the mantissa<br />

should then yield zero. But .25<br />

is held approximately on the<br />

<strong>ZX</strong>81, unlike (%), which is held<br />

exactly. The result of this is that<br />

log of the mantissa comes out<br />

as - !6 32 , an approximation to<br />

zero. Finally, the sum of - 2 log<br />

2 and - Vi 32 adds two to the<br />

correct answer because of the<br />

bug in subtraction.<br />

This brings us to the source<br />

of the bug in subtraction.<br />

Because subtraction just<br />

changes the sign and adds, it<br />

will help to think of simply adding<br />

on a negative number, (n<br />

floating point addition, the addend<br />

(ie the number being added<br />

on) has to be lined up with<br />

the other number (the<br />

"augend") to make the addition<br />

possible. If the addend is<br />

too small to make any ap-<br />

10 FOR R=0 TO 31<br />

20 LPR INT RT 1.. R . .0P0P65<br />

30 NEXT R<br />

10 PRINT RT<br />

FROM 1 TO<br />

20 LET N = 1<br />

2,. 3;<br />

32" ,<br />

POWERS OF" J?<br />

30 FOR X = 1 TO 32<br />

40 LET N=2*.W<br />

50 LET R = INT (N./10P.1<br />

60 LET B=N-1O0*R<br />

70 PRINT " " RND X

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!