From 543da39c22680aaacb7634d5356708949ede63fb Mon Sep 17 00:00:00 2001 From: Richard Kreckel Date: Wed, 21 May 2003 02:46:55 +0000 Subject: [PATCH 1/1] * numeric::power(): try harder finding unit exponents. * numeric::power_dyn(): likewise. --- ginac/numeric.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/ginac/numeric.cpp b/ginac/numeric.cpp index 37a1ba3f..1d4de9f5 100644 --- a/ginac/numeric.cpp +++ b/ginac/numeric.cpp @@ -758,8 +758,9 @@ const numeric numeric::div(const numeric &other) const * returns result as a numeric object. */ const numeric numeric::power(const numeric &other) const { - // Efficiency shortcut: trap the neutral exponent by pointer. - if (&other==_num1_p) + // Shortcut for efficiency and numeric stability (as in 1.0 exponent): + // trap the neutral exponent. + if (&other==_num1_p || cln::equal(cln::the(other.value),cln::the(_num1.value))) return *this; if (cln::zerop(cln::the(value))) { @@ -782,7 +783,8 @@ const numeric numeric::power(const numeric &other) const * an ex object, where the result would end up on the heap anyways. */ const numeric &numeric::add_dyn(const numeric &other) const { - // Efficiency shortcut: trap the neutral element by pointer. + // Efficiency shortcut: trap the neutral element by pointer. This hack + // is supposed to keep the number of distinct numeric objects low. if (this==_num0_p) return other; else if (&other==_num0_p) @@ -842,8 +844,10 @@ const numeric &numeric::div_dyn(const numeric &other) const * heap anyways. */ const numeric &numeric::power_dyn(const numeric &other) const { - // Efficiency shortcut: trap the neutral exponent by pointer. - if (&other==_num1_p) + // Efficiency shortcut: trap the neutral exponent (first try by pointer, then + // try harder, since calls to cln::expt() below may return amazing results for + // floating point exponent 1.0). + if (&other==_num1_p || cln::equal(cln::the(other.value),cln::the(_num1.value))) return *this; if (cln::zerop(cln::the(value))) { -- 2.45.2