(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
 Gamo Senior Member Posts: 698 Joined: Dec 2016
(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
01-09-2018, 09:32 AM (This post was last modified: 01-09-2018 09:45 AM by Dieter.)
Post: #2
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
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
01-10-2018, 06:44 AM (This post was last modified: 01-10-2018 06:45 AM by Gamo.)
Post: #3
 Gamo Senior Member Posts: 698 Joined: Dec 2016
RE: (12C) Modulo (the remainder after division )
Thank You Dieter
Your program algorithm is very precise.

Gamo
02-15-2018, 05:15 AM
Post: #4
 Gamo Senior Member Posts: 698 Joined: Dec 2016
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
12-05-2019, 12:03 PM (This post was last modified: 12-06-2019 04:31 AM by Gamo.)
Post: #5
 Gamo Senior Member Posts: 698 Joined: Dec 2016
RE: (12C) Modulo (the remainder after division )
This little MOD function routine must be the perfect one of all, I hope.

Procedure:

x mod y = remainder

X [ENTER] Y [R/S] display Answer

Program:
Code:
 01    ÷ 02  LSTx 03  X<>Y 04  ENTER 05  INTG 06    - 07    x 08  FIX 0

Example:

98765 mod 4321

98765 [ENTER] 4321 [R/S] display 3703

Gamo
12-05-2019, 12:36 PM
Post: #6
 Joe Horn Senior Member Posts: 1,767 Joined: Dec 2013
RE: (12C) Modulo (the remainder after division )
Although that routine is very short, it suffers from roundoff error. It even gets the answer wrong for your example (as can be seen if you set FIX 9, as Dieter pointed out previously in this thread). The 10-step routine you posted previously gets the correct answer.

<0|ɸ|0>
-Joe-
 « Next Oldest | Next Newest »

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