[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [bug-gawk] GAWK 4.1.60 DIV() Remainder Bug
From: |
Aharon Robbins |
Subject: |
Re: [bug-gawk] GAWK 4.1.60 DIV() Remainder Bug |
Date: |
Tue, 05 Aug 2014 17:32:09 +0300 |
User-agent: |
Heirloom mailx 12.5 6/20/10 |
Hello again.
> From: Aharon Robbins <address@hidden>
> Date: Tue, 05 Aug 2014 16:25:23 +0300
> To: address@hidden, address@hidden
> Cc: address@hidden
> Subject: Re: [bug-gawk] GAWK 4.1.60 DIV() Remainder Bug
>
> Hi Andy & Katie.
>
> Some quick experimentation convinced me that MPFR was getting this wrong
> also. Andy, your analysis down to mpz_mod is great and has saved me some
> time. Now we have to figure out WHY mpz_mod isn't doing what it is
> supposed to be doing.
>
> In the worst case, I'll just switch out to use the div_mod function that
> div() uses, but I'd like to try for a bit to understand why mpz_mod
> isn't working.
So, after web searching and reading, I'm stumped. mpz_mod() should be doing
the trick, but it's not. So I have punted and moved to mpz_tdiv_qr() which
produces the right result, and I just throw away the quotient.
Here is the patch that I will be pushing shortly.
Katie - feel free to keep poking. You may be the first user to seriously
exercise the MPFR code. I think this is like the 3rd or 4th bug fix in
the space of 2 weeks!
Thanks!
Arnold
-------------------------------------------------------------
diff --git a/mpfr.c b/mpfr.c
index 37a1217..e53af61 100644
--- a/mpfr.c
+++ b/mpfr.c
@@ -1376,8 +1376,27 @@ mpg_mod(NODE *t1, NODE *t2)
int tval;
if (is_mpg_integer(t1) && is_mpg_integer(t2)) {
+ /*
+ * 8/2014: Originally, this was just
+ *
+ * r = mpg_integer();
+ * mpz_mod(r->mpg_i, t1->mpg_i, t2->mpg_i);
+ *
+ * But that gave very strange results with negative numerator:
+ *
+ * $ ./gawk -M 'BEGIN { print -15 % 7 }'
+ * 6
+ *
+ * So instead we use mpz_tdiv_qr() to get the correct result
+ * and just throw away the quotient. We could not find any
+ * reason why mpz_mod() wasn't working correctly.
+ */
+ NODE *dummy_quotient;
+
r = mpg_integer();
- mpz_mod(r->mpg_i, t1->mpg_i, t2->mpg_i);
+ dummy_quotient = mpg_integer();
+ mpz_tdiv_qr(dummy_quotient->mpg_i, r->mpg_i, t1->mpg_i,
t2->mpg_i);
+ unref(dummy_quotient);
} else {
mpfr_ptr p1, p2;
p1 = MP_FLOAT(t1);