Post Reply 
(12C) Modulo (the remainder after division )
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
Post Reply 


Messages In This Thread
RE: (12C) Modulo (the remainder after division ) - Dieter - 01-09-2018 09:32 AM



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