]> www.ginac.de Git - ginac.git/commitdiff
Improve (fix?) smod: now it really converts into symmetric representation...
authorAlexei Sheplyakov <varg@metalica.kh.ua>
Mon, 19 Jan 2009 06:44:36 +0000 (08:44 +0200)
committerJens Vollinga <jensv@balin.nikhef.nl>
Tue, 3 Feb 2009 12:16:03 +0000 (13:16 +0100)
... instead of clumsy convention inspirited by some proprietary CAS.

doc/tutorial/ginac.texi
ginac/numeric.cpp

index 3207a96fcb1c197ee3df95c72c0508e3827a3a24..fe7a2648661966aa7d0ab0768148457039c95609 100644 (file)
@@ -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)
index 91721e1b6a55a5e8a6a69ae5d93f4e4d0d8946a7..50d873a9f6c5ac0da255553db40f64fb14a75057 100644 (file)
@@ -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<cln::cl_I>(b.to_cl_N()) >> 1) - 1;
-               return numeric(cln::mod(cln::the<cln::cl_I>(a.to_cl_N()) + b2,
-                               cln::the<cln::cl_I>(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<cln::cl_I>(a_.to_cl_N());
+               const cln::cl_I b = cln::the<cln::cl_I>(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;
 }