[Python-checkins] cpython: Many cleanups of redundant code in mpd_qrem_near():

stefan.krah python-checkins at python.org
Wed Jun 20 23:36:38 CEST 2012


http://hg.python.org/cpython/rev/95221b37bf04
changeset:   77526:95221b37bf04
user:        Stefan Krah <skrah at bytereef.org>
date:        Wed Jun 20 23:34:58 2012 +0200
summary:
  Many cleanups of redundant code in mpd_qrem_near():

  1) _mpd_qdivmod() uses the context precision only in two places, and
     the new code should be exactly equivalent to the previous code.

  2) Remove misleading comment.

  3) The quotient *is* an integer with exponent 0, so calling mpd_qtrunc()
     is pointless.

  4) Replace two instances of identical code by a single one.

  5) Use _mpd_cmp_abs() instead of mpd_cmp_total_mag(): the operands
     are not special.

  6) Don't clear MPD_Rounded in the status (with the current code it should
     not be set within the function).

files:
  Modules/_decimal/libmpdec/mpdecimal.c |  42 ++++++--------
  1 files changed, 17 insertions(+), 25 deletions(-)


diff --git a/Modules/_decimal/libmpdec/mpdecimal.c b/Modules/_decimal/libmpdec/mpdecimal.c
--- a/Modules/_decimal/libmpdec/mpdecimal.c
+++ b/Modules/_decimal/libmpdec/mpdecimal.c
@@ -6679,7 +6679,7 @@
     mpd_context_t workctx;
     MPD_NEW_STATIC(btmp,0,0,0,0);
     MPD_NEW_STATIC(q,0,0,0,0);
-    mpd_ssize_t expdiff, floordigits;
+    mpd_ssize_t expdiff, qdigits;
     int cmp, isodd, allnine;
 
     if (mpd_isspecial(a) || mpd_isspecial(b)) {
@@ -6716,53 +6716,45 @@
         b = &btmp;
     }
 
-    workctx = *ctx;
-    workctx.prec = a->digits;
-    workctx.prec = (workctx.prec > ctx->prec) ? workctx.prec : ctx->prec;
-
-    _mpd_qdivmod(&q, r, a, b, &workctx, status);
-    if (mpd_isnan(&q) || mpd_isnan(r) || q.digits > ctx->prec) {
-        mpd_seterror(r, MPD_Division_impossible, status);
+    _mpd_qdivmod(&q, r, a, b, ctx, status);
+    if (mpd_isnan(&q) || mpd_isnan(r)) {
         goto finish;
     }
     if (mpd_iszerocoeff(r)) {
         goto finish;
     }
 
-    /* Deal with cases like rmnx078:
-     * remaindernear 999999999.5 1 -> NaN Division_impossible */
     expdiff = mpd_adjexp(b) - mpd_adjexp(r);
     if (-1 <= expdiff && expdiff <= 1) {
 
-        mpd_qtrunc(&q, &q, &workctx, &workctx.status);
         allnine = mpd_coeff_isallnine(&q);
-        floordigits = q.digits;
+        qdigits = q.digits;
         isodd = mpd_isodd(&q);
 
         mpd_maxcontext(&workctx);
         if (mpd_sign(a) == mpd_sign(b)) {
+            /* sign(r) == sign(b) */
             _mpd_qsub(&q, r, b, &workctx, &workctx.status);
-            if (workctx.status&MPD_Errors) {
-                mpd_seterror(r, workctx.status&MPD_Errors, status);
-                goto finish;
-            }
         }
         else {
+            /* sign(r) != sign(b) */
             _mpd_qadd(&q, r, b, &workctx, &workctx.status);
-            if (workctx.status&MPD_Errors) {
-                mpd_seterror(r, workctx.status&MPD_Errors, status);
-                goto finish;
-            }
-        }
-
-        cmp = mpd_cmp_total_mag(&q, r);
+        }
+
+        if (workctx.status&MPD_Errors) {
+            mpd_seterror(r, workctx.status&MPD_Errors, status);
+            goto finish;
+        }
+
+        cmp = _mpd_cmp_abs(&q, r);
         if (cmp < 0 || (cmp == 0 && isodd)) {
-            if (allnine && floordigits == ctx->prec) {
+            /* abs(r) > abs(b)/2 or abs(r) == abs(b)/2 and isodd(quotient) */
+            if (allnine && qdigits == ctx->prec) {
+                /* abs(quotient) + 1 == 10**prec */
                 mpd_seterror(r, MPD_Division_impossible, status);
                 goto finish;
             }
             mpd_qcopy(r, &q, status);
-            *status &= ~MPD_Rounded;
         }
     }
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list