aboutsummaryrefslogtreecommitdiffstats
path: root/mpfr.c
diff options
context:
space:
mode:
authorArnold D. Robbins <arnold@skeeve.com>2014-08-05 17:38:37 +0300
committerArnold D. Robbins <arnold@skeeve.com>2014-08-05 17:38:37 +0300
commiteafb7b72c2b6095cba51a77f5f7a5240a658c55c (patch)
tree4b60eb71ac37bb58d3137d42442572e3e6288107 /mpfr.c
parent2aff045eb07d96c572242287487450f1d9acc5fe (diff)
downloadegawk-eafb7b72c2b6095cba51a77f5f7a5240a658c55c.tar.gz
egawk-eafb7b72c2b6095cba51a77f5f7a5240a658c55c.tar.bz2
egawk-eafb7b72c2b6095cba51a77f5f7a5240a658c55c.zip
Bug fix to MPFR mod operation for negative numerator.
Diffstat (limited to 'mpfr.c')
-rw-r--r--mpfr.c21
1 files changed, 20 insertions, 1 deletions
diff --git a/mpfr.c b/mpfr.c
index 37a12175..e53af616 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);