Working out the modulo.

SYS 64738
User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Working out the modulo.

Post by Shaun_B »

Modulo is something that I use in my work quite often in PHP, C, JS etc... it's quite useful, but as far as I know, there isn't a way to say if x%4 = 0 then .... in BASIC v2. This example will work out the modulo of the two numbers entered.

Code: Select all

10 input "please enter number then divisor ";z,d
20 print z; "/"; d; "=";: gosub 63000: print c; "remainder"; z
30 end
62990 rem c will count how many times z divides into d and
62995 rem z will be the remainder (modulo) answer
63000 c=0: for i=-1 to 0: z=z-d: c=c+1 : i=z=>d: next: return
So, for instance, if you had 40 and wanted to work out the remainder if divided into six, one might:

Code: Select all

z=40: d=6: gosub 63000: if z<>4 then print "oops... cbm does not maths very well"
Regards,

Shaun.


BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.
ajordison
Developer
Developer
Posts: 59
Joined: Wed Apr 16, 2014 1:28 pm
Location: Hartlepool, UK
Contact:

Re: Working out the modulo.

Post by ajordison »

Well I haven't done modular arithmetic for a while. If you don't like for..next loops try this:

Code: Select all

10 input "please enter number then divisor ";a,b
20 print a; "/"; b; "=";int(a/b): print "remainder"; a-int(a/b)*b
30 end
It's amazing what you can dredge up!
Try out CBM prg Studio for all your Commodore development needs!
Stay a while...stay for erm quite a while!
User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Re: Working out the modulo.

Post by Shaun_B »

Top tip! My maths aren't good enough to have known that already :-)

Regards,

Shaun.
BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.
ajordison
Developer
Developer
Posts: 59
Joined: Wed Apr 16, 2014 1:28 pm
Location: Hartlepool, UK
Contact:

Re: Working out the modulo.

Post by ajordison »

Well it took a couple of goes to get it right!

Arthur.
Try out CBM prg Studio for all your Commodore development needs!
Stay a while...stay for erm quite a while!
Post Reply Previous topicNext topic

Who is online

Users browsing this forum: No registered users and 4 guests