Math bug?
Posted: Tue Oct 03, 2023 12:13 am
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:
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:
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
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