diff options
author | vlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4> | 2016-01-21 00:52:24 +0000 |
---|---|---|
committer | vlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4> | 2016-01-21 00:52:24 +0000 |
commit | 78c797d8acf10f9811624bbdd005e426daaec2e8 (patch) | |
tree | 960738c19c2aafa46f689219dbf340e2c04228d5 | |
parent | 6c94f4e1e21d6c5d2eee5082c53fecc774c18740 (diff) | |
download | mpfr-78c797d8acf10f9811624bbdd005e426daaec2e8.tar.gz |
Fixed bug in mpfr_sqrt reported by Fredrik Johansson, occurring when
the result has to be rounded up to a power of 2.
(merged changesets r9857-9859 from the trunk)
git-svn-id: svn://scm.gforge.inria.fr/svn/mpfr/branches/3.1@9860 280ebfd0-de03-0410-8827-d642c229c3f4
-rw-r--r-- | src/sqrt.c | 3 | ||||
-rw-r--r-- | tests/tsqrt.c | 26 |
2 files changed, 28 insertions, 1 deletions
diff --git a/src/sqrt.c b/src/sqrt.c index ca71e6627..afae0b689 100644 --- a/src/sqrt.c +++ b/src/sqrt.c @@ -211,10 +211,11 @@ mpfr_sqrt (mpfr_ptr r, mpfr_srcptr u, mpfr_rnd_t rnd_mode) rsize --; sh = 0; } + /* now rsize = MPFR_LIMB_SIZE(r) */ if (mpn_add_1 (rp0, rp, rsize, MPFR_LIMB_ONE << sh)) { expr ++; - rp[rsize - 1] = MPFR_LIMB_HIGHBIT; + rp0[rsize - 1] = MPFR_LIMB_HIGHBIT; } goto end; diff --git a/tests/tsqrt.c b/tests/tsqrt.c index bec47d9f8..1a3f59a89 100644 --- a/tests/tsqrt.c +++ b/tests/tsqrt.c @@ -569,6 +569,30 @@ test_property2 (mpfr_prec_t p, mpfr_rnd_t r) mpfr_clear (y); } +/* bug reported by Fredrik Johansson */ +static void +bug20160120 (void) +{ + mpfr_t x, y; + + mpfr_init2 (x, 361); + mpfr_init2 (y, 64); + + mpfr_set_ui (x, 1, MPFR_RNDN); + mpfr_nextbelow (x); + mpfr_sqrt (y, x, MPFR_RNDN); + MPFR_ASSERTN(mpfr_check (y)); + MPFR_ASSERTN(mpfr_cmp_ui (y, 1) == 0); + + mpfr_set_prec (y, 128); + mpfr_sqrt (y, x, MPFR_RNDN); + MPFR_ASSERTN(mpfr_check (y)); + MPFR_ASSERTN(mpfr_cmp_ui (y, 1) == 0); + + mpfr_clear(x); + mpfr_clear(y); +} + #define TEST_FUNCTION test_sqrt #define TEST_RANDOM_POS 8 #include "tgeneric.c" @@ -704,6 +728,8 @@ main (void) data_check ("data/sqrt", mpfr_sqrt, "mpfr_sqrt"); bad_cases (mpfr_sqrt, mpfr_sqr, "mpfr_sqrt", 8, -256, 255, 4, 128, 800, 50); + bug20160120 (); + tests_end_mpfr (); return 0; } |