2 * mod - routines to handle numbers modulo a specified number
4 * Copyright (C) 1999 David I. Bell
6 * Calc is open software; you can redistribute it and/or modify it under
7 * the terms of the version 2.1 of the GNU Lesser General Public License
8 * as published by the Free Software Foundation.
10 * Calc is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
13 * Public License for more details.
15 * A copy of version 2.1 of the GNU Lesser General Public License is
16 * distributed with calc under the filename COPYING-LGPL. You should have
17 * received a copy with calc; if not, write to Free Software Foundation, Inc.
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * @(#) $Revision: 30.1 $
21 * @(#) $Id: mod.cal,v 30.1 2007/03/16 11:09:54 chongo Exp $
22 * @(#) $Source: /usr/local/src/bin/calc/cal/RCS/mod.cal,v $
24 * Under source code control: 1990/02/15 01:50:34
25 * File existed as early as: before 1990
27 * Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
31 obj mod {a}; /* definition of the object */
33 global mod_value = 100; /* modulus value (value of N) */
40 if (!isreal(a) || !isint(a))
41 quit "Bad argument for lmod function";
49 if (digits(mod_value) <= 20)
50 print a.a, "(mod", mod_value : ")" :;
52 print a.a, "(mod N)" :;
65 return (a % mod_value) != b.a;
67 return (b % mod_value) != a.a;
90 quit "Adding non-integer";
91 x.a = (a.a + b) % mod_value;
96 quit "Adding non-integer";
97 x.a = (a + b.a) % mod_value;
100 x.a = (a.a + b.a) % mod_value;
115 x.a = mod_value - a.a;
126 quit "Multiplying by non-integer";
127 x.a = (a.a * b) % mod_value;
132 quit "Multiplying by non-integer";
133 x.a = (a * b.a) % mod_value;
136 x.a = (a.a * b.a) % mod_value;
145 x.a = a.a^2 % mod_value;
155 if (++x.a == mod_value)
176 x.a = minv(a.a, mod_value);
193 return x * inverse(y);
208 x.a = pmod(y.a, z, mod_value);
213 if (config("resource_debug") & 3) {
214 print "obj mod {a} defined";
215 print "mod_value defined";
216 print "set mod_value as needed";