(12C) Modulus
07-18-2016, 10:12 PM
Post: #3
 bshoring Member Posts: 258 Joined: Dec 2013
RE: (12C) Modulus
(07-15-2016 06:32 PM)Dieter Wrote:
(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

Here's a routine I wrote about a month ago. One step longer, but it uses only the stack and Last-X register. I wrote it for the HP-38C, but the steps are exactly the same for the HP-12C (but the keycodes would be different). For the two examples given by Dieter, it gets the right results.

Code:
01-     x<>y 02-     Enter 03-     Enter 04-     R↓ 05-     R↓ 06-     R↓ 07-     ÷ 08-     LSTX 09-     x<>y 10-     INTG 11-     x 12-     - 13-     GTO 00

Regards,
Bob
 « 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)