Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2238501

Browse files
author
Stefan Krah
committed
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).
1 parent 42c9b04 commit 2238501

1 file changed

Lines changed: 15 additions & 23 deletions

File tree

Modules/_decimal/libmpdec/mpdecimal.c

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6679,7 +6679,7 @@ mpd_qrem_near(mpd_t *r, const mpd_t *a, const mpd_t *b,
66796679
mpd_context_t workctx;
66806680
MPD_NEW_STATIC(btmp,0,0,0,0);
66816681
MPD_NEW_STATIC(q,0,0,0,0);
6682-
mpd_ssize_t expdiff, floordigits;
6682+
mpd_ssize_t expdiff, qdigits;
66836683
int cmp, isodd, allnine;
66846684

66856685
if (mpd_isspecial(a) || mpd_isspecial(b)) {
@@ -6716,53 +6716,45 @@ mpd_qrem_near(mpd_t *r, const mpd_t *a, const mpd_t *b,
67166716
b = &btmp;
67176717
}
67186718

6719-
workctx = *ctx;
6720-
workctx.prec = a->digits;
6721-
workctx.prec = (workctx.prec > ctx->prec) ? workctx.prec : ctx->prec;
6722-
6723-
_mpd_qdivmod(&q, r, a, b, &workctx, status);
6724-
if (mpd_isnan(&q) || mpd_isnan(r) || q.digits > ctx->prec) {
6725-
mpd_seterror(r, MPD_Division_impossible, status);
6719+
_mpd_qdivmod(&q, r, a, b, ctx, status);
6720+
if (mpd_isnan(&q) || mpd_isnan(r)) {
67266721
goto finish;
67276722
}
67286723
if (mpd_iszerocoeff(r)) {
67296724
goto finish;
67306725
}
67316726

6732-
/* Deal with cases like rmnx078:
6733-
* remaindernear 999999999.5 1 -> NaN Division_impossible */
67346727
expdiff = mpd_adjexp(b) - mpd_adjexp(r);
67356728
if (-1 <= expdiff && expdiff <= 1) {
67366729

6737-
mpd_qtrunc(&q, &q, &workctx, &workctx.status);
67386730
allnine = mpd_coeff_isallnine(&q);
6739-
floordigits = q.digits;
6731+
qdigits = q.digits;
67406732
isodd = mpd_isodd(&q);
67416733

67426734
mpd_maxcontext(&workctx);
67436735
if (mpd_sign(a) == mpd_sign(b)) {
6736+
/* sign(r) == sign(b) */
67446737
_mpd_qsub(&q, r, b, &workctx, &workctx.status);
6745-
if (workctx.status&MPD_Errors) {
6746-
mpd_seterror(r, workctx.status&MPD_Errors, status);
6747-
goto finish;
6748-
}
67496738
}
67506739
else {
6740+
/* sign(r) != sign(b) */
67516741
_mpd_qadd(&q, r, b, &workctx, &workctx.status);
6752-
if (workctx.status&MPD_Errors) {
6753-
mpd_seterror(r, workctx.status&MPD_Errors, status);
6754-
goto finish;
6755-
}
67566742
}
67576743

6758-
cmp = mpd_cmp_total_mag(&q, r);
6744+
if (workctx.status&MPD_Errors) {
6745+
mpd_seterror(r, workctx.status&MPD_Errors, status);
6746+
goto finish;
6747+
}
6748+
6749+
cmp = _mpd_cmp_abs(&q, r);
67596750
if (cmp < 0 || (cmp == 0 && isodd)) {
6760-
if (allnine && floordigits == ctx->prec) {
6751+
/* abs(r) > abs(b)/2 or abs(r) == abs(b)/2 and isodd(quotient) */
6752+
if (allnine && qdigits == ctx->prec) {
6753+
/* abs(quotient) + 1 == 10**prec */
67616754
mpd_seterror(r, MPD_Division_impossible, status);
67626755
goto finish;
67636756
}
67646757
mpd_qcopy(r, &q, status);
6765-
*status &= ~MPD_Rounded;
67666758
}
67676759
}
67686760

0 commit comments

Comments
 (0)