From: Alexei Sheplyakov Date: Mon, 19 Jan 2009 06:44:36 +0000 (+0200) Subject: Improve (fix?) smod: now it really converts into symmetric representation... X-Git-Tag: release_1-5-0~13 X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=commitdiff_plain;h=3f0b0165865bbb297901e9542fced88a0e32298e;ds=sidebyside Improve (fix?) smod: now it really converts into symmetric representation... ... instead of clumsy convention inspirited by some proprietary CAS. --- diff --git a/doc/tutorial/ginac.texi b/doc/tutorial/ginac.texi index 3207a96f..fe7a2648 100644 --- a/doc/tutorial/ginac.texi +++ b/doc/tutorial/ginac.texi @@ -1479,7 +1479,7 @@ evaluated immediately: @tab modulus in positive representation (in the range @code{[0, abs(b)-1]} with the sign of b, or zero) @cindex @code{mod()} @item @code{smod(a, b)} -@tab modulus in symmetric representation (in the range @code{[-iquo(abs(b)-1, 2), iquo(abs(b), 2)]}) +@tab modulus in symmetric representation (in the range @code{[-iquo(abs(b), 2), iquo(abs(b), 2)]}) @cindex @code{smod()} @item @code{irem(a, b)} @tab integer remainder (has the sign of @math{a}, or is zero) diff --git a/ginac/numeric.cpp b/ginac/numeric.cpp index 91721e1b..50d873a9 100644 --- a/ginac/numeric.cpp +++ b/ginac/numeric.cpp @@ -2337,15 +2337,18 @@ const numeric mod(const numeric &a, const numeric &b) /** Modulus (in symmetric representation). - * Equivalent to Maple's mods. * - * @return a mod b in the range [-iquo(abs(b)-1,2), iquo(abs(b),2)]. */ -const numeric smod(const numeric &a, const numeric &b) -{ - if (a.is_integer() && b.is_integer()) { - const cln::cl_I b2 = cln::ceiling1(cln::the(b.to_cl_N()) >> 1) - 1; - return numeric(cln::mod(cln::the(a.to_cl_N()) + b2, - cln::the(b.to_cl_N())) - b2); + * @return a mod b in the range [-iquo(abs(b),2), iquo(abs(b),2)]. */ +const numeric smod(const numeric &a_, const numeric &b_) +{ + if (a_.is_integer() && b_.is_integer()) { + const cln::cl_I a = cln::the(a_.to_cl_N()); + const cln::cl_I b = cln::the(b_.to_cl_N()); + const cln::cl_I b2 = b >> 1; + const cln::cl_I m = cln::mod(a, b); + const cln::cl_I m_b = m - b; + const cln::cl_I ret = m > b2 ? m_b : m; + return numeric(ret); } else return *_num0_p; }