## ZX81 Modulo example.

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

### ZX81 Modulo example.

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: 312
Joined: Wed Apr 22, 2009 9:22 am

### Re: ZX81 Modulo example.

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.

Posts: 133
Joined: Mon Mar 24, 2014 2:11 pm
Location: Aberdeen, Scotland

### Re: ZX81 Modulo example.

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.

Great stuff! Any more?

PokeMon
Posts: 2137
Joined: Sat Sep 17, 2011 5:48 pm

### Re: ZX81 Modulo example.

Well yes - something like this:

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 (52.55 KiB) Viewed 759 times

Posts: 133
Joined: Mon Mar 24, 2014 2:11 pm
Location: Aberdeen, Scotland

### Re: ZX81 Modulo example.

Oh well, if we're posting our code...!
Attachments
mod.png (65.87 KiB) Viewed 758 times

Shaun_B
Posts: 312
Joined: Wed Apr 22, 2009 9:22 am

### Re: ZX81 Modulo example.

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.

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: 312
Joined: Wed Apr 22, 2009 9:22 am

### Re: ZX81 Modulo example.

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

Cheers guys.

sirmorris
Posts: 2667
Joined: Thu May 08, 2008 4:45 pm
Location: oxon, uk

### Re: ZX81 Modulo example.

Shaun_B wrote:
Thu May 08, 2014 9:25 pm
(and also, if you re-arrange the numbers in the year 2014 you will get 1024 - true fact
--=== ZXpand+ ... A more moderner all-in-one upgrade solution for ZX81 ===--
* SD/MMC storage * 32K Hires-capable RAM * AY sound chip * Joystick port * Serial *
* BASIC enhancements - plug & play * Reset button * Auto-boot facility *

Shaun_B
Posts: 312
Joined: Wed Apr 22, 2009 9:22 am

### Re: ZX81 Modulo example.

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

solar penguin
Posts: 3
Joined: Wed Apr 19, 2017 7:29 pm

### Re: ZX81 Modulo example.

Shaun_B wrote:
Tue Mar 28, 2017 1: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

### Who is online

Users browsing this forum: No registered users and 1 guest