Post Reply 
[Prime CAS] ichinrem negative results
03-07-2014, 11:54 PM
Post: #3
RE: [Prime CAS] ichinrem negative results
(03-07-2014 08:25 AM)Didier Lachieze Wrote:  The CAS ichinrem function (Integer Chinese Remainder) returns often negative results. ... While both results [235 9009] and [-8774 9009] are valid and equivalent, I would prefer to have always the same result a modulo b with a in the interval [0,b[

The same problem drives me crazy on the HP 50g. Like the Prime, its CAS modulo functions (not MOD itself) sometimes return negative results. I think your only hope is to create a user-defined function or program which uses the old MOD function to rectify the result.

Here's a tiny program that takes the matrix input for ichinrem and returns the smallest positive result:
Code:
EXPORT chin(M0)
BEGIN
LOCAL m;
m:=ichinrem(M0);
RETURN m(1) MOD m(2);
END;
Example:
ichinrem([[12,23],[123,234]]) --> [-5025, 5382]
chin([[12,23],[123,234]]) --> 357

If you want the modulus also, change "RETURN m(1) MOD m(2)" in the program to "RETURN [m(1) MOD m(2),m(2)]". Then the program would return [357, 5382] for the above example.

I really wish there were a more elegant way to do this, but that's how I've handled it on the 50g for many years.

<0|ΙΈ|0>
-Joe-
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: [Prime CAS] ichinrem negative results - Joe Horn - 03-07-2014 11:54 PM



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