Strange integer behaviour

Anything Sinclair ZX Basic related; history, development, tips - differences between BASIC on the ZX80 and ZX81
my216
Posts: 4
Joined: Sun Mar 17, 2024 9:07 pm

Strange integer behaviour

Post by my216 »

It's been 40 years since I last wrote any code for the ZX81 and I've forgotten any idiosyncrasies it may have had.

If I assign a number to a variable that is greater than 8 digits and then retrieve it with Print it is returned rounded.For example:

LET A = 123456789

PRINT A

1234567900

Also, if I convert A to a string it becomes "1234567900".

Can anyone remind me what is happening here? Thanks.
stroebeljc
Posts: 67
Joined: Thu Apr 23, 2020 6:02 am

Re: Strange integer behaviour

Post by stroebeljc »

The ZX81 is only capable of precision to 8 digits. Thus, the rounding.
John
Moggy
Posts: 3267
Joined: Wed Jun 18, 2008 2:00 pm

Re: Strange integer behaviour

Post by Moggy »

Internally accurate to 9 - 10 digits with 8 displayed after rounding.
stroebeljc
Posts: 67
Joined: Thu Apr 23, 2020 6:02 am

Re: Strange integer behaviour

Post by stroebeljc »

Internally accurate down to 1/(2^32) ~= 0.0000000002328306437. So, yes, 9 to 10 digits. 32/log2(10)=9.63, to be more precise.
Last edited by stroebeljc on Mon Mar 18, 2024 2:49 pm, edited 1 time in total.
John
my216
Posts: 4
Joined: Sun Mar 17, 2024 9:07 pm

Re: Strange integer behaviour

Post by my216 »

But it's not just printing. As converting the stored value to a string will also give the rounded to 8 digits number.
stroebeljc
Posts: 67
Joined: Thu Apr 23, 2020 6:02 am

Re: Strange integer behaviour

Post by stroebeljc »

Sort of like the calculators of the day.
calc_2.jpg
John
User avatar
zx81jens
Posts: 202
Joined: Sat May 10, 2008 8:26 am
Location: a Basement in Nienburg / Germany

Re: Strange integer behaviour

Post by zx81jens »

Hello Users.

This is shown on Page 28:

https://www.historybit.it/wp-content/up ... amming.pdf

Greetings
Jens
eyerything will be okay in the end.
if it´s not okay, it´s not the end.

and: uıɐbɐ ʎɐqǝ uo pɹɐoqʎǝʞ ɐ ʎnq ɹǝʌǝ ɹǝʌǝu ןןıʍ ı
my216
Posts: 4
Joined: Sun Mar 17, 2024 9:07 pm

Re: Strange integer behaviour

Post by my216 »

yep, but what it doesn't say is that although all digits of 4294967295 are apparently stored, if you do STR$ A (assuming the number is in A) you do not get "4294967295" but "4294967300"
User avatar
1024MAK
Posts: 5118
Joined: Mon Sep 26, 2011 10:56 am
Location: Looking forward to summer in Somerset, UK...

Re: Strange integer behaviour

Post by 1024MAK »

The internal ROM function that converts a floating point number to a string is almost certainly the same routine that converts it for printing to the screen.

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.
User avatar
Paul
Posts: 1517
Joined: Thu May 27, 2010 8:15 am
Location: Germanys west end

Re: Strange integer behaviour

Post by Paul »

1024MAK wrote: Tue Mar 19, 2024 4:28 pm The internal ROM function that converts a floating point number to a string is almost certainly the same routine that converts it for printing to the screen.

Mark
You can delete the "almost"
In theory, there is no difference between theory and practice. But, in practice, there is.
Post Reply