Post Reply 
(12C) Modulo (the remainder after division )
01-09-2018, 06:03 AM (This post was last modified: 01-09-2018 07:04 AM by Gamo.)
Post: #1
(12C) Modulo (the remainder after division )
The modulo operation finds the remainder after division of one number by another.

Example: X ENTER Y R/S

6 MOD 4 = 2
7 MOD 4 = 3
1235 MOD 25 = 10
91596 MOD 123 = 84
456654 MOD 104 = 94

Special one: 98765 MOD 43210 = 12345

Program:

Code:

STO 0
  ÷
FRAC
RCL 0
  x

With this small program I do not see Round Off Error yet.

Gamo
Find all posts by this user
Quote this message in a reply
01-09-2018, 09:32 AM (This post was last modified: 01-09-2018 09:45 AM by Dieter.)
Post: #2
RE: (12C) Modulo (the remainder after division )
(01-09-2018 06:03 AM)Gamo Wrote:  The modulo operation finds the remainder after division of one number by another.
...
Code:
STO 0
  ÷
FRAC
RCL 0
  x

With this small program I do not see Round Off Error yet.

There is more than you see in FIX 2. ;-)

7 mod 3 => 0,99999999
20 mod 7 => 5,999999999
200 mod 9 => 1,999999980
1E+9 mod 6 => 4,2

Or take you own examples:

91596 mod 123 => 82,99999640
456654 mod 104 => 93,99998400
98765 mod 43210 => 12344,99999

You could add 0,4 + INTG to round the results to an integer which would help in most cases. But this limits the output to integers so that 123 mod 2,5 = 0,5 cannot be calculated. And in a way this also is like cheating. ;-)

As already noted, using b*frac(a/b) is not a good idea at all as it causes the shown roundoff errors. A better method is a – b*int(a/b). However, this requires some more steps if you want to preserve the stack:

Code:
01 X<>Y
02 STO 0
03 X<>Y
04 /
05 LASTX
06 X<>Y
07 INTG
08 x
09 RCL 0
10 X<>Y
11 -
12 GTO 00

This calculates all above examples correctly.

All these methods are limited by cases where due to limited precision a/b is returned as an integer although it actually is not.
Example: With 10-digit precision 8E+9 / 3 = 2666666667 so that the modulus is returned as 0 (frac method) or –1 (int method) instead of 2.

Dieter
Find all posts by this user
Quote this message in a reply
01-10-2018, 06:44 AM (This post was last modified: 01-10-2018 06:45 AM by Gamo.)
Post: #3
RE: (12C) Modulo (the remainder after division )
Thank You Dieter
Your program algorithm is very precise.

Gamo
Find all posts by this user
Quote this message in a reply
02-15-2018, 05:15 AM
Post: #4
RE: (12C) Modulo (the remainder after division )
Another MOD program for 12C

Code:

ENTER
ENTER
-
Roll Down
X<>Y
LSTx
/
INTG
x
-

Example:
MOD (156511533, 1546223)

156511533 ENTER 1546223 R/S result 343010

Gamo
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)