X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=blobdiff_plain;f=ginac%2Fnumeric.cpp;h=50d873a9f6c5ac0da255553db40f64fb14a75057;hp=5649353f308f68a0c26e715034b84977c5702979;hb=b9dc3d71083fe1a2f90a34db24b57b06ad51ebde;hpb=def26469ff96228c66e877bb5594e7d9a24b638f diff --git a/ginac/numeric.cpp b/ginac/numeric.cpp index 5649353f..50d873a9 100644 --- a/ginac/numeric.cpp +++ b/ginac/numeric.cpp @@ -278,8 +278,9 @@ static const cln::cl_F read_real_float(std::istream& s) return x; } -numeric::numeric(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst) +void numeric::read_archive(const archive_node &n, lst &sym_lst) { + inherited::read_archive(n, sym_lst); value = 0; // Read number as string @@ -324,6 +325,7 @@ numeric::numeric(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst) } setflag(status_flags::evaluated | status_flags::expanded); } +GINAC_BIND_UNARCHIVER(numeric); static void write_real_float(std::ostream& s, const cln::cl_R& n) { @@ -373,8 +375,6 @@ void numeric::archive(archive_node &n) const n.add_string("number", s.str()); } -DEFAULT_UNARCHIVE(numeric) - ////////// // functions overriding virtual functions from base classes ////////// @@ -2278,11 +2278,14 @@ const numeric fibonacci(const numeric &n) // F(2n+2) = F(n+1)*(2*F(n) + F(n+1)) if (n.is_zero()) return *_num0_p; - if (n.is_negative()) - if (n.is_even()) + if (n.is_negative()) { + if (n.is_even()) { return -fibonacci(-n); - else + } + else { return fibonacci(-n); + } + } cln::cl_I u(0); cln::cl_I v(1); @@ -2334,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; }