From 89779632ca81383a8fa44244d7825947638e7fbd Mon Sep 17 00:00:00 2001 From: Richard Kreckel Date: Tue, 1 Oct 2002 20:10:00 +0000 Subject: [PATCH 1/1] * irem(), iquo(): throw an exception, when second argument vanishes. --- ginac/numeric.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/ginac/numeric.cpp b/ginac/numeric.cpp index 0274d63a..858cbbd5 100644 --- a/ginac/numeric.cpp +++ b/ginac/numeric.cpp @@ -1688,9 +1688,12 @@ const numeric smod(const numeric &a, const numeric &b) * In general, mod(a,b) has the sign of b or is zero, and irem(a,b) has the * sign of a or is zero. * - * @return remainder of a/b if both are integer, 0 otherwise. */ + * @return remainder of a/b if both are integer, 0 otherwise. + * @exception overflow_error (division by zero) if b is zero. */ const numeric irem(const numeric &a, const numeric &b) { + if (b.is_zero()) + throw std::overflow_error("numeric::irem(): division by zero"); if (a.is_integer() && b.is_integer()) return cln::rem(cln::the(a.to_cl_N()), cln::the(b.to_cl_N())); @@ -1705,9 +1708,12 @@ const numeric irem(const numeric &a, const numeric &b) * and irem(a,b) has the sign of a or is zero. * * @return remainder of a/b and quotient stored in q if both are integer, - * 0 otherwise. */ + * 0 otherwise. + * @exception overflow_error (division by zero) if b is zero. */ const numeric irem(const numeric &a, const numeric &b, numeric &q) { + if (b.is_zero()) + throw std::overflow_error("numeric::irem(): division by zero"); if (a.is_integer() && b.is_integer()) { const cln::cl_I_div_t rem_quo = cln::truncate2(cln::the(a.to_cl_N()), cln::the(b.to_cl_N())); @@ -1723,9 +1729,12 @@ const numeric irem(const numeric &a, const numeric &b, numeric &q) /** Numeric integer quotient. * Equivalent to Maple's iquo as far as sign conventions are concerned. * - * @return truncated quotient of a/b if both are integer, 0 otherwise. */ + * @return truncated quotient of a/b if both are integer, 0 otherwise. + * @exception overflow_error (division by zero) if b is zero. */ const numeric iquo(const numeric &a, const numeric &b) { + if (b.is_zero()) + throw std::overflow_error("numeric::iquo(): division by zero"); if (a.is_integer() && b.is_integer()) return cln::truncate1(cln::the(a.to_cl_N()), cln::the(b.to_cl_N())); @@ -1739,9 +1748,12 @@ const numeric iquo(const numeric &a, const numeric &b) * r == a - iquo(a,b,r)*b. * * @return truncated quotient of a/b and remainder stored in r if both are - * integer, 0 otherwise. */ + * integer, 0 otherwise. + * @exception overflow_error (division by zero) if b is zero. */ const numeric iquo(const numeric &a, const numeric &b, numeric &r) { + if (b.is_zero()) + throw std::overflow_error("numeric::iquo(): division by zero"); if (a.is_integer() && b.is_integer()) { const cln::cl_I_div_t rem_quo = cln::truncate2(cln::the(a.to_cl_N()), cln::the(b.to_cl_N())); -- 2.44.0