Page 1 of 1

Is This A Bug In The iOS ZX81 Emulator?

Posted: Tue May 23, 2023 2:28 am
by MrVertigo
Or am I doing something stupid? I can’t for the life of me work out why the second output of this routine is zero. It should be 1.

Code: Select all

10 Let C=24
20 Let X=4
30 Let A=8

40 Let C= C/X
50 Let D= A/C
60 Let F= Int D
70 Let A= (D-F)*C
80 Print F

120 Let C= C/(x-1)
130 Let D=A/C
140 Let F= Int D
150 Print F
Line 150 should return “1”. If you input the results of part one manually (C=6, X=4, A=2) and run the program from line 120, it produces the correct result: line 150 returns “1”. If you print out C, X and A between line 80 and 120, they are 6,4 and 2. If you print A and C after line 130 they are 2 and 2, as they should be. If you print out A and C after line 140 they are 2 and 2. So why on earth is F zero?

Some numbers it works properly. For example if you change the starting conditions to C=24, X=4 and A=9, it works correctly. But starting with C=6, X=2 and A=3 gives the wrong output at 150 too.

Why is this? What am I missing?

Re: Is This A Bug In The iOS ZX81 Emulator?

Posted: Tue May 23, 2023 3:34 am
by XavSnap
int.JPG
INT 0.99999999999999999 = 0

Re: Is This A Bug In The iOS ZX81 Emulator?

Posted: Tue May 23, 2023 1:28 pm
by 1024MAK
One thing to remember is that with floating point numbers, what the ZX81 stores/uses is not what us humans see.

When you enter a decimal number, the ZX81 changes this to a floating point binary representation of that number. Hence if you then get it to perform calculations, sometimes rounding can cause unexpected results.

And that’s with the “bug fixed” ROM…

Mark

Re: Is This A Bug In The iOS ZX81 Emulator?

Posted: Tue May 23, 2023 1:37 pm
by MrVertigo
Ah, thanks for these replies! They make it very clear what is happening.

Is the best solution to use a loop that subtracts C from A the required number of times, rather than dividing? Or is there a better workaround?

Re: Is This A Bug In The iOS ZX81 Emulator?

Posted: Tue May 23, 2023 1:58 pm
by Moggy
Concerning how the computer treats its binary representation of numbers, you may find the following links enlightening.

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

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

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

Re: Is This A Bug In The iOS ZX81 Emulator?

Posted: Tue May 23, 2023 2:17 pm
by MrVertigo
Thanks for these!

Re: Is This A Bug In The iOS ZX81 Emulator?

Posted: Tue May 23, 2023 10:45 pm
by 1024MAK
If it makes you feel any better/worse (delete as applicable :lol: )...

Floating point rounding errors on a ZX Spectrum 128K using the Qaop emulator:
Floating point rounding errors on a ZX Spectrum 128K
Floating point rounding errors on a ZX Spectrum 128K
Mark

Re: Is This A Bug In The iOS ZX81 Emulator?

Posted: Thu May 25, 2023 12:01 am
by MrVertigo
:D