diff options
-rw-r--r-- | ChangeLog | 11 | ||||
-rw-r--r-- | mpfr.c | 41 | ||||
-rw-r--r-- | test/ChangeLog | 6 | ||||
-rw-r--r-- | test/Makefile.am | 9 | ||||
-rw-r--r-- | test/Makefile.in | 9 | ||||
-rw-r--r-- | test/mpfrsqrt.awk | 82 | ||||
-rw-r--r-- | test/mpfrsqrt.ok | 2 |
7 files changed, 144 insertions, 16 deletions
@@ -1,3 +1,14 @@ +2014-08-05 Arnold D. Robbins <arnold@skeeve.com> + + Bug fix: For MPFR sqrt(), need to set precision of result to be + the same as that of the argument. Doesn't hurt other functions. + See test/mpfrsqrt.awk. + + * mpfr.c (do_mpfr_func): New function. Runs code for MPFR functions + while still enabling debugging. Add call here to mpfr_set_prec(). + Original code from SPEC_MATH macro. + (SPEC_MATH): Change macro to call do_mpfr_func(). + 2014-08-03 Arnold D. Robbins <arnold@skeeve.com> * builtin.c (format_tree): Don't need to check return value of @@ -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 */ diff --git a/test/ChangeLog b/test/ChangeLog index 4f72280b..afbccf5f 100644 --- a/test/ChangeLog +++ b/test/ChangeLog @@ -1,3 +1,9 @@ +2014-08-05 Arnold D. Robbins <arnold@skeeve.com> + + * Makefile.am (mpfrsqrt): New test. + * mpfrsqrt.awk, mpfrsqrt.ok: New files. + Test from Katie Wasserman <katie@wass.net>. + 2014-07-25 Arnold D. Robbins <arnold@skeeve.com> * printhuge.awk: Add a newline to output. diff --git a/test/Makefile.am b/test/Makefile.am index c822163a..8349a73e 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -533,6 +533,8 @@ EXTRA_DIST = \ mpfrrnd.ok \ mpfrsort.awk \ mpfrsort.ok \ + mpfrsqrt.awk \ + mpfrsqrt.ok \ mtchi18n.awk \ mtchi18n.in \ mtchi18n.ok \ @@ -1023,7 +1025,7 @@ INET_TESTS = inetdayu inetdayt inetechu inetecht MACHINE_TESTS = double1 double2 fmtspcl intformat MPFR_TESTS = mpfrnr mpfrnegzero mpfrrnd mpfrieee mpfrexprange \ - mpfrsort mpfrbigint + mpfrsort mpfrsqrt mpfrbigint LOCALE_CHARSET_TESTS = \ asort asorti backbigs1 backsmalls1 backsmalls2 \ @@ -1745,6 +1747,11 @@ mpfrbigint: @$(AWK) -M -f "$(srcdir)"/$@.awk > _$@ 2>&1 @-$(CMP) "$(srcdir)"/$@.ok _$@ && rm -f _$@ +mpfrsqrt: + @echo $@ + @$(AWK) -M -f "$(srcdir)"/$@.awk > _$@ 2>&1 + @-$(CMP) "$(srcdir)"/$@.ok _$@ && rm -f _$@ + jarebug:: @echo $@ @"$(srcdir)"/$@.sh "$(AWKPROG)" "$(srcdir)"/$@.awk "$(srcdir)"/$@.in "_$@" diff --git a/test/Makefile.in b/test/Makefile.in index 1660ab8f..e8fd8844 100644 --- a/test/Makefile.in +++ b/test/Makefile.in @@ -779,6 +779,8 @@ EXTRA_DIST = \ mpfrrnd.ok \ mpfrsort.awk \ mpfrsort.ok \ + mpfrsqrt.awk \ + mpfrsqrt.ok \ mtchi18n.awk \ mtchi18n.in \ mtchi18n.ok \ @@ -1265,7 +1267,7 @@ EXTRA_TESTS = inftest regtest INET_TESTS = inetdayu inetdayt inetechu inetecht MACHINE_TESTS = double1 double2 fmtspcl intformat MPFR_TESTS = mpfrnr mpfrnegzero mpfrrnd mpfrieee mpfrexprange \ - mpfrsort mpfrbigint + mpfrsort mpfrsqrt mpfrbigint LOCALE_CHARSET_TESTS = \ asort asorti backbigs1 backsmalls1 backsmalls2 \ @@ -2169,6 +2171,11 @@ mpfrbigint: @$(AWK) -M -f "$(srcdir)"/$@.awk > _$@ 2>&1 @-$(CMP) "$(srcdir)"/$@.ok _$@ && rm -f _$@ +mpfrsqrt: + @echo $@ + @$(AWK) -M -f "$(srcdir)"/$@.awk > _$@ 2>&1 + @-$(CMP) "$(srcdir)"/$@.ok _$@ && rm -f _$@ + jarebug:: @echo $@ @"$(srcdir)"/$@.sh "$(AWKPROG)" "$(srcdir)"/$@.awk "$(srcdir)"/$@.in "_$@" diff --git a/test/mpfrsqrt.awk b/test/mpfrsqrt.awk new file mode 100644 index 00000000..23a15c92 --- /dev/null +++ b/test/mpfrsqrt.awk @@ -0,0 +1,82 @@ +# Date: Sat, 02 Aug 2014 12:27:00 -0400 +# To: bug-gawk@gnu.org +# From: Katherine Wasserman <katie@wass.net> +# Message-ID: <E1XDc9F-0007BX-O1@eggs.gnu.org> +# Subject: [bug-gawk] GAWK 4.1 SQRT() bug +# +# In version 4.1.60 of GAWK the sqrt() function does not work correctly on bignums. +# Here's a demo of the problem along with, a function that does work correctly. +# +# Running this program (sqrt-bug.awk): +# -------------------------------------------------------------------- +BEGIN { +a=11111111111111111111111111111111111111111111111111111111111 +print sqrt(a^2) +#print sq_root(a^2) + +# ADR: Added for gawk-4.1-stable which doesn't have built-in div() function +if (PROCINFO["version"] < "4.1.60") + print sq_root2(a^2) +else + print sq_root(a^2) +} + + +function sq_root(x, temp,r,z) +{ temp=substr(x,1,length(x)/2) + 0 # a good first guess + z=0 + while (abs(z-temp)>1) + { z=temp + div(x,temp,r) + temp=r["quotient"] + temp + div(temp,2,r) + temp=r["quotient"] + } + return temp +} + +function sq_root2(x, temp,r,z) +{ temp=substr(x,1,length(x)/2) + 0 # a good first guess + z=0 + while (abs(z-temp)>1) + { z=temp + awk_div(x,temp,r) + temp=r["quotient"] + temp + awk_div(temp,2,r) + temp=r["quotient"] + } + return temp +} + +function abs(x) +{ return (x<0 ? -x : x) +} +# +# -------------------------------------------------------------------- +# gawk -M -f sqrt-bug.awk +# +# results in: +# 11111111111111111261130863809439559987542611609749437808640 +# 11111111111111111111111111111111111111111111111111111111111 +# +# Thanks, +# Katie +# + +# div --- do integer division + +function awk_div(numerator, denominator, result, i, save_PREC) +{ + save_PREC = PREC + PREC = 400 # good enough for this test + + split("", result) + + numerator = int(numerator) + denominator = int(denominator) + result["quotient"] = int(numerator / denominator) + result["remainder"] = int(numerator % denominator) + + PREC = save_PREC + return 0.0 +} diff --git a/test/mpfrsqrt.ok b/test/mpfrsqrt.ok new file mode 100644 index 00000000..16217c78 --- /dev/null +++ b/test/mpfrsqrt.ok @@ -0,0 +1,2 @@ +11111111111111111111111111111111111111111111111111111111111 +11111111111111111111111111111111111111111111111111111111111 |