diff options
Diffstat (limited to 'mpfr.c')
-rw-r--r-- | mpfr.c | 62 |
1 files changed, 47 insertions, 15 deletions
@@ -697,22 +697,35 @@ do_mpfr_atan2(int nargs) return res; } +/* do_mpfr_func --- run an MPFR function - not inline, for debugging */ -#define SPEC_MATH(X) \ -NODE *t1, *res; \ -mpfr_ptr p1; \ -int tval; \ -t1 = POP_SCALAR(); \ -if (do_lint && (t1->flags & (NUMCUR|NUMBER)) == 0) \ - lintwarn(_("%s: received non-numeric argument"), #X); \ -force_number(t1); \ -p1 = MP_FLOAT(t1); \ -res = mpg_float(); \ -tval = mpfr_##X(res->mpg_numbr, p1, ROUND_MODE); \ -IEEE_FMT(res->mpg_numbr, tval); \ -DEREF(t1); \ -return res +static inline NODE * +do_mpfr_func(const char *name, + int (*mpfr_func)(), /* putting argument types just gets the compiler confused */ + int nargs) +{ + NODE *t1, *res; + mpfr_ptr p1; + int tval; + + t1 = POP_SCALAR(); + if (do_lint && (t1->flags & (NUMCUR|NUMBER)) == 0) + lintwarn(_("%s: received non-numeric argument"), name); + + force_number(t1); + p1 = MP_FLOAT(t1); + res = mpg_float(); + mpfr_set_prec(res->mpg_numbr, mpfr_get_prec(p1)); /* needed at least for sqrt() */ + tval = mpfr_func(res->mpg_numbr, p1, ROUND_MODE); + IEEE_FMT(res->mpg_numbr, tval); + DEREF(t1); + return res; +} +#define SPEC_MATH(X) \ +NODE *result; \ +result = do_mpfr_func(#X, mpfr_##X, nargs); \ +return result /* do_mpfr_sin --- do the sin function */ @@ -1452,8 +1465,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); |