Macaulay2 Engine
Loading...
Searching...
No Matches

◆ remainderAndQuotient()

ring_elem RingZZ::remainderAndQuotient ( const ring_elem f,
const ring_elem g,
ring_elem & quot ) const
virtual

Reimplemented from Ring.

Definition at line 317 of file ZZ.cpp.

320{
321 mpz_ptr q = new_elem();
322 mpz_ptr r = new_elem();
323 int gsign = mpz_sgn(g.get_mpz());
324 mpz_t gg, ghalf;
325 mpz_init(gg);
326 mpz_init(ghalf);
327 mpz_abs(gg, g.get_mpz());
328 mpz_fdiv_qr(q, r, f.get_mpz(), gg);
329 mpz_tdiv_q_2exp(ghalf, gg, 1);
330 if (mpz_cmp(r, ghalf) > 0) // r > ghalf
331 {
332 mpz_sub(r, r, gg);
333 mpz_add_ui(q, q, 1);
334 }
335 if (gsign < 0) mpz_neg(q, q);
336
337 mpz_clear(gg);
338 mpz_clear(ghalf);
341 quot = ring_elem(q);
342 return ring_elem(r);
343}
mpz_ptr new_elem() const
Definition ZZ.cpp:34
void mpz_reallocate_limbs(mpz_ptr _z)
Definition gmp-util.h:46
mpz_srcptr get_mpz() const
Definition ringelem.hpp:127

References ring_elem::get_mpz(), mpz_reallocate_limbs(), and new_elem().

Referenced by quotient(), and remainder().