(12C) Modulus

07152016, 02:37 AM
(This post was last modified: 07152016 02:38 AM by Eddie W. Shore.)
Post: #1




(12C) Modulus
HP 12C Modulus
This program takes the modulus of two numbers: Y MOD X = X * FRAC(Y/X) In this program, X > 0 and Y > 0. Code:
Input: Y [ENTER] X [R/S], Result: Y MOD X Test 1: 124 MOD 77 = 47 Test 2: 3862 MOD 108 = 82 

07152016, 06:32 PM
(This post was last modified: 07152016 07:00 PM by Dieter.)
Post: #2




RE: (12C) Modulus
(07152016 02:37 AM)Eddie W. Shore Wrote: This program takes the modulus of two numbers: That's not a good idea: roundoff errors will spoil the results. Consider your two examples: 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 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 

07182016, 10:12 PM
Post: #3




RE: (12C) Modulus
(07152016 06:32 PM)Dieter Wrote:(07152016 02:37 AM)Eddie W. Shore Wrote: This program takes the modulus of two numbers: Here's a routine I wrote about a month ago. One step longer, but it uses only the stack and LastX register. I wrote it for the HP38C, but the steps are exactly the same for the HP12C (but the keycodes would be different). For the two examples given by Dieter, it gets the right results. Code: 01 x<>y Regards, Bob 

07212016, 11:36 PM
Post: #4




RE: (12C) Modulus
From "ENTER: Reverse Polish Notation Made Easy", JeanDaniel Dodin / Keith Jarett, (pg 115):
Code: 01 36 ENTER 

07252016, 09:17 AM
(This post was last modified: 07252016 10:10 AM by Dieter.)
Post: #5




RE: (12C) Modulus
(07182016 10:12 PM)bshoring Wrote: Here's a routine I wrote about a month ago. One step longer, but it uses only the stack and LastX register. Great. In the meantime I got exactly the same solution. Many/most other HPs offer a R↑ which can replace the three consecutive R↓, making the program even shorter. (07212016 11:36 PM)RobertM Wrote: From "ENTER: Reverse Polish Notation Made Easy", JeanDaniel Dodin / Keith Jarett, (pg 115): That's a nice one as well. Here's another 10/11 step solution: Code: 01 ENTER The first four lines copy the content of Y to Z and T while X and Y are left unchanged. However, all these solutions destroy the stack, while the version I initially posted keeps the values of Z and T. That's why it requires one data register. I wonder if it is possible to do it only with the stack, i.e. without a data register, while Z and T are still preserved. Here's another challenge: what about returning y mod x as well as y div x (i.e. the integer quotient of y and x). There are many applications where both values are required at the same time. That's why I once suggested a DIVMOD command for the 34s that returns these two values. Now, what do you think ?) Dieter 

07252016, 09:46 AM
(This post was last modified: 07292016 05:40 PM by Dieter.)
Post: #6




RE: (12C) Modulus
(07252016 09:17 AM)I Wrote: Here's another challenge: what about returning y mod x as well as y div x (i.e. the integer quotient of y and x). There are many applications where both values are required at the same time. That's why I once suggested a DIVMOD command for the 34s that returns these two values. I just realized that the code in the previous post can be extended easily to return both values at the same time – it's just two more steps: Code: 01 ENTER The same can be done with the Dodin/Jarett program by inserting ENTER R↓ between step 08 and 09. The above version returns the remainder in X and y div x in Y. 3782 [ENTER] 72 [R/S] => 38 [X<>Y] 52 There even is a third useful result: [LastX] => 3744 The largest number ≤ Y that is divisible by X. Dieter 

07282016, 11:01 PM
(This post was last modified: 07282016 11:02 PM by bshoring.)
Post: #7




RE: (12C) Modulus
(07252016 09:46 AM)Dieter Wrote:(07252016 09:17 AM)I Wrote: Here's another challenge: what about returning y mod x as well as y div x (i.e. the integer quotient of y and x). There are many applications where both values are required at the same time. That's why I once suggested a DIVMOD command for the 34s that returns these two values. Nice program. When I run 3782 [ENTER] 72 [R/S], it leaves 38 in X only and 52 in Y, Z, & T. Works for me. Regards, Bob 

07292016, 05:39 PM
(This post was last modified: 07292016 05:43 PM by Dieter.)
Post: #8




RE: (12C) Modulus  
« Next Oldest  Next Newest »

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