From: Alex M. <al...@us...> - 2005-11-07 21:55:47
|
Update of /cvsroot/gmpy/gmpy/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20916/src Modified Files: gmpy.c Log Message: Fixed gmpy.divm for non-coprime divisor and modulo, added tests for that. Index: gmpy.c =================================================================== RCS file: /cvsroot/gmpy/gmpy/src/gmpy.c,v retrieving revision 1.15 retrieving revision 1.16 diff -C2 -d -r1.15 -r1.16 *** gmpy.c 7 Nov 2005 15:43:24 -0000 1.15 --- gmpy.c 7 Nov 2005 21:55:39 -0000 1.16 *************** *** 72,76 **** * and other sundry minor warnings given by gcc * fixed hash (delegating to Python) so mp[nqz](x) will ! * hash to the same value of x for any Python number x * workaround for GMP 3.1.1 bug, mpz_root wrongly returning * 'exact' for non-exact root if dest==source, which stopped --- 72,76 ---- * and other sundry minor warnings given by gcc * fixed hash (delegating to Python) so mp[nqz](x) will ! * produce the same value as hash(x) for any Python number x * workaround for GMP 3.1.1 bug, mpz_root wrongly returning * 'exact' for non-exact root if dest==source, which stopped *************** *** 110,119 **** * 1.01: * cleanups, ensure support for Python 2.4.1 on MacOSX 10.4/XCode 2.1 * fixed memory leak on divm * fixed bug on mpq('123') [[str2mpq on string w/o a slash]] ! * added floordiv and truediv operators * * TODO: look deeper into divm (when operands are not mutually prime) - * add tests of floordiv and truediv * */ --- 110,119 ---- * 1.01: * cleanups, ensure support for Python 2.4.1 on MacOSX 10.4/XCode 2.1 + * as well as Python 2.2 and 2.3 (on MacOSX and Linux) * fixed memory leak on divm * fixed bug on mpq('123') [[str2mpq on string w/o a slash]] ! * added floordiv and truediv operators, and tests for them * * TODO: look deeper into divm (when operands are not mutually prime) * */ *************** *** 131,135 **** /* so one won't need to link explicitly to gmp.lib...: */ # pragma comment(lib,"gmp.lib") ! /* fix anomalies of .h vs .lib on Windows, also, use alloca */ #undef mpq_out_str size_t mpq_out_str _PROTO ((FILE *, int, mpq_srcptr)); --- 131,135 ---- /* so one won't need to link explicitly to gmp.lib...: */ # pragma comment(lib,"gmp.lib") ! /* fix anomalies of .h vs .lib on Windows; also, use alloca */ #undef mpq_out_str size_t mpq_out_str _PROTO ((FILE *, int, mpq_srcptr)); *************** *** 4357,4360 **** --- 4357,4361 ---- { PympzObject *num, *den, *mod, *res; + int ok; if(!PyArg_ParseTuple(args, "O&O&O&", *************** *** 4368,4371 **** --- 4369,4388 ---- return NULL; if(mpz_invert(res->z, den->z, mod->z)) { /* inverse exists */ + ok = 1; + } else { + /* last-ditch attempt: do num, den AND mod have a gcd>1 ? */ + PympzObject *gcd; + if(!(gcd = Pympz_new())) + return NULL; + mpz_gcd(gcd->z, num->z, den->z); + mpz_gcd(gcd->z, gcd->z, mod->z); + mpz_divexact(num->z, num->z, gcd->z); + mpz_divexact(den->z, den->z, gcd->z); + mpz_divexact(mod->z, mod->z, gcd->z); + Py_DECREF((PyObject*)gcd); + ok = mpz_invert(res->z, den->z, mod->z); + } + + if (ok) { mpz_mul(res->z, res->z, num->z); mpz_mod(res->z, res->z, mod->z); |