ZX81 Modulo example.

Anything Sinclair ZX Basic related; history, development, tips - differences between BASIC on the ZX80 and ZX81
Shaun_B
Posts: 474
Joined: Wed Apr 22, 2009 10:22 am

ZX81 Modulo example.

Post by Shaun_B »

As 2014 is BASIC's 50th anniversary (and also, if you re-arrange the numbers in the year 2014 you will get 1024 - true fact), I've been doing some basic experiments etc...

Modulo is a pretty useful function in C and C-alike languages, so I thought I'd write something that will do modulo on 8-bit computers.

Here's my example:

Code: Select all

10 LET Z=0
20 LET R=0
30 LET D=0
40 LET I=0
50 PRINT "PLEASE ENTER A NUMBER FOLLOWED BY THE DIVISOR"
60 INPUT Z
70 INPUT D
80 PRINT Z;"/";D;" = "
90 GOSUB 1000
100 PRINT R;" REMAINDER ";Z
110 STOP
1000 FOR I=1 TO 0 STEP -1
1010 LET Z=Z-D
1020 LET R=R+1
1030 LET I=(Z>=D)
1040 NEXT I
1050 RETURN
It's quite slow but I think the logic is correct.

I've used a FOR loop essentially like a DO/WHILE loop in C/PHP etc... It didn't work at first so I reversed the logic and that seemed to fix it.

Regards,

Shaun.
Shaun_B
Posts: 474
Joined: Wed Apr 22, 2009 10:22 am

Re: ZX81 Modulo example.

Post by Shaun_B »

Just had a top tip through... due to my maths not being very good, I hadn't realised that you could do this... here is a faster method:

Code: Select all

10 LET Z=0
20 LET D=0
30 PRINT "PLEASE ENTER A NUMBER THEN A DIVISOR"
40 INPUT Z
50 INPUT D
60 PRINT Z;"/";D;" = ";INT Z/D;" REMAINDER ";Z-INT Z/D*D
Regards,

Shaun.
poglad
Posts: 133
Joined: Mon Mar 24, 2014 3:11 pm
Location: Aberdeen, Scotland

Re: ZX81 Modulo example.

Post by poglad »

I was just going to reply but you beat me to it! But your original code is fine for a ZX80 as it works with only integer arithmetic. :D

Great stuff! Any more?
User avatar
PokeMon
Posts: 2264
Joined: Sat Sep 17, 2011 6:48 pm

Re: ZX81 Modulo example.

Post by PokeMon »

Well yes - something like this: :mrgreen:

In fact you have been a bit faster but you can surely omit the first two statements as the INPUT line will overwrite any value you define first.
mod.jpg
mod.jpg (52.55 KiB) Viewed 6955 times
poglad
Posts: 133
Joined: Mon Mar 24, 2014 3:11 pm
Location: Aberdeen, Scotland

Re: ZX81 Modulo example.

Post by poglad »

Oh well, if we're posting our code...! ;)
Attachments
mod.png
mod.png (65.87 KiB) Viewed 6954 times
Shaun_B
Posts: 474
Joined: Wed Apr 22, 2009 10:22 am

Re: ZX81 Modulo example.

Post by Shaun_B »

poglad wrote:I was just going to reply but you beat me to it! But your original code is fine for a ZX80 as it works with only integer arithmetic. :D

Great stuff! Any more?
Ah yes, I forgot about the ZX80.

In my original listing, the FOR/NEXT loop is essentially like a DO...WHILE loop in C. Did I mention that already? Probably.

Thanks for the responses guys,

Regards,

Shaun.
Shaun_B
Posts: 474
Joined: Wed Apr 22, 2009 10:22 am

Re: ZX81 Modulo example.

Post by Shaun_B »

And so I two become an entry on Code Golf @ Stack Exchange. Or the ZX80 version will.

Cheers guys.
sirmorris
Posts: 2811
Joined: Thu May 08, 2008 5:45 pm

Re: ZX81 Modulo example.

Post by sirmorris »

Shaun_B wrote: Thu May 08, 2014 10:25 pm(and also, if you re-arrange the numbers in the year 2014 you will get 1024 - true fact
:lol: :lol: :lol:
Shaun_B
Posts: 474
Joined: Wed Apr 22, 2009 10:22 am

Re: ZX81 Modulo example.

Post by Shaun_B »

Here is the ZX80 version, as I couldn't find the >= on the keyboard, I had a work-around which you'll see once you load the file.

Also, it doesn't handle zero currently. According to the method that I'm employing x/0 is not infinity - it's x remainder infinity - I may add this into V2.

Regards,

Shaun.
Attachments
zx80-remainder.o
(204 Bytes) Downloaded 226 times
solar penguin
Posts: 5
Joined: Wed Apr 19, 2017 8:29 pm

Re: ZX81 Modulo example.

Post by solar penguin »

Shaun_B wrote: Tue Mar 28, 2017 2:01 pm Here is the ZX80 version, as I couldn't find the >= on the keyboard, I had a work-around which you'll see once you load the file.

Also, it doesn't handle zero currently. According to the method that I'm employing x/0 is not infinity - it's x remainder infinity - I may add this into V2.

Regards,

Shaun.
Hi Shaun,

If you're going for fewest bytes, a better of doing Z>=D without >= is just NOT Z<D
Post Reply