(12C) Modulus
07-15-2016, 06:32 PM (This post was last modified: 07-15-2016 07:00 PM by Dieter.)
Post: #2
 Dieter Senior Member Posts: 2,398 Joined: Dec 2013
RE: (12C) Modulus
(07-15-2016 02:37 AM)Eddie W. Shore Wrote:  This program takes the modulus of two numbers:
Y MOD X = X * FRAC(Y/X)

That's not a good idea: roundoff errors will spoil the results.

124 mod 77 does not return 47 but 46,99999997
3862 mod 108 does not return 82 but 82,00000008

That's why you better use the relation y mod x = y – x * INT(y/x) instead.

Code:
01-       34  x<>y 02-     44 0  STO 0 03-       34  x<>y 04-       10  ÷ 05-    43 36  LSTX 06-       34  x<>y 07-    43 25  INTG 08-       20  x 09-  44 30 0  STO- 0 10-       33  R↓ 11-     45 0  RCL 0 12- 44,33 00  GTO 00

That's a bit longer and requires one data register, but it works.
The R↓ in line 11 was included to preserve the initiial values in T and Z.

Dieter
 « Next Oldest | Next Newest »

 Messages In This Thread (12C) Modulus - Eddie W. Shore - 07-15-2016, 02:37 AM RE: (12C) Modulus - Dieter - 07-15-2016 06:32 PM RE: (12C) Modulus - bshoring - 07-18-2016, 10:12 PM RE: (12C) Modulus - RobertM - 07-21-2016, 11:36 PM RE: (12C) Modulus - Dieter - 07-25-2016, 09:17 AM RE: (12C) Modulus - Dieter - 07-25-2016, 09:46 AM RE: (12C) Modulus - bshoring - 07-28-2016, 11:01 PM RE: (12C) Modulus - Dieter - 07-29-2016, 05:39 PM

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