diff options
-rw-r--r-- | tests/Makefile.am | 2 | ||||
-rw-r--r-- | tests/tget_ld_2exp.c | 120 |
2 files changed, 121 insertions, 1 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am index d055c5741..205ad9a74 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,6 +1,6 @@ AUTOMAKE_OPTIONS = gnu $(top_builddir)/ansi2knr -check_PROGRAMS = tversion tinternals tinits tsgn tcheck tisnan texceptions tset_exp tset tabs tset_d tset_f tset_q tset_si tset_str tset_z tset_ld tset_sj tswap tcopysign tcmp tcmp2 tcmpabs tcmp_d tcmp_ld tcomparisons teq tadd tsub tmul tdiv tsub1sp tadd1sp tadd_ui tsub_ui tcmp_ui tdiv_ui tmul_ui tsqrt_ui tui_div tui_sub tgmpop tsi_op tmul_2exp tfma tsum tdim tminmax tnext tfits tget_d tget_d_2exp tget_z tget_str tget_sj tout_str tinp_str toutimpl tcan_round tround_prec tsqrt tconst_log2 tconst_pi tconst_euler trandom ttrunc trint tfrac texp texp2 texpm1 tlog tlog2 tlog10 tlog1p tpow tui_pow tpow3 tcosh tsinh ttanh tacosh tasinh tatanh thyperbolic tasin tacos tcos tatan tsin ttan tsin_cos tagm thypot tfactorial tgamma terf tcbrt tzeta mpf_compat mpfr_compat reuse tsqr tstrtofr tpow_z tget_f tconst_catalan troot tsec tcsc tcot teint tcoth tcsch tsech tstckintc tsubnormal tlngamma tzeta_ui +check_PROGRAMS = tversion tinternals tinits tsgn tcheck tisnan texceptions tset_exp tset tabs tset_d tset_f tset_q tset_si tset_str tset_z tset_ld tset_sj tswap tcopysign tcmp tcmp2 tcmpabs tcmp_d tcmp_ld tcomparisons teq tadd tsub tmul tdiv tsub1sp tadd1sp tadd_ui tsub_ui tcmp_ui tdiv_ui tmul_ui tsqrt_ui tui_div tui_sub tgmpop tsi_op tmul_2exp tfma tsum tdim tminmax tnext tfits tget_d tget_d_2exp tget_z tget_str tget_sj tout_str tinp_str toutimpl tcan_round tround_prec tsqrt tconst_log2 tconst_pi tconst_euler trandom ttrunc trint tfrac texp texp2 texpm1 tlog tlog2 tlog10 tlog1p tpow tui_pow tpow3 tcosh tsinh ttanh tacosh tasinh tatanh thyperbolic tasin tacos tcos tatan tsin ttan tsin_cos tagm thypot tfactorial tgamma terf tcbrt tzeta mpf_compat mpfr_compat reuse tsqr tstrtofr tpow_z tget_f tconst_catalan troot tsec tcsc tcot teint tcoth tcsch tsech tstckintc tsubnormal tlngamma tzeta_ui tget_ld_2exp EXTRA_DIST = tgeneric.c tgeneric_ui.c mpf_compat.h inp_str.data diff --git a/tests/tget_ld_2exp.c b/tests/tget_ld_2exp.c new file mode 100644 index 000000000..87de61e9c --- /dev/null +++ b/tests/tget_ld_2exp.c @@ -0,0 +1,120 @@ +/* Test mpfr_get_ld_2exp. + +Copyright 2006 Free Software Foundation, Inc. + +This file is part of the MPFR Library. +Contributed by Damien Stehle. + +The MPFR Library is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or (at your +option) any later version. + +The MPFR Library is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public +License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with the MPFR Library; see the file COPYING.LIB. If not, write to +the Free Software Foundation, Inc., 51 Franklin Place, Fifth Floor, Boston, +MA 02110-1301, USA. */ + +#include <stdio.h> +#include <stdlib.h> +#include <float.h> + +#include "mpfr-test.h" + +/* Check that hardware rounding doesn't make mpfr_get_ld_2exp return a value + outside its defined range. */ +static void +check_round (void) +{ + static const unsigned long data[] = {1, 32, 53, 54, 63, 64, 65, 127, 128, 256, 512 }; + mpfr_t f; + long double got; + long got_exp; + int i, rnd_mode, neg; + + mpfr_init2 (f, 1024L); + + for (rnd_mode = 0; rnd_mode < GMP_RND_MAX ; rnd_mode++) + { + for (i = 0; i < (int) numberof (data); i++) + { + mpfr_set_ui (f, 1L, GMP_RNDZ); + mpfr_mul_2exp (f, f, data[i], GMP_RNDZ); + mpfr_sub_ui (f, f, 1L, GMP_RNDZ); + + for (neg = 0; neg <= 1; neg++) + { + got = mpfr_get_ld_2exp (&got_exp, f, (mp_rnd_t) rnd_mode); + + if (neg == 0 + ? (got < 0.5 || got >= 1.0) + : (got <= -1.0 || got > -0.5)) + { + printf ("mpfr_get_ld_2exp wrong on 2**%lu-1\n", data[i]); + printf ("result out of range, expect 0.5 <= got < 1.0\n"); + printf (" rnd_mode = %d\n", rnd_mode); + printf (" data[i] = %lu\n", data[i]); + printf (" f "); + mpfr_out_str (stdout, 2, 0, f, GMP_RNDN); + printf ("\n"); + d_trace (" got ", got); + printf (" got exp %ld\n", got_exp); + exit(1); + } + + mpfr_neg (f, f, GMP_RNDZ); + } + } + } + + mpfr_clear (f); +} + + +static void +check_inf_nan () +{ + /* only if nans and infs are available */ +#if _GMP_IEEE_FLOATS + mpfr_t x; + double d; + long exp; + + mpfr_init2 (x, 123); + + mpfr_set_inf (x, 1); + d = (double) mpfr_get_ld_2exp (&exp, x, GMP_RNDZ); + ASSERT_ALWAYS (d > 0); + ASSERT_ALWAYS (DOUBLE_ISINF (d)); + + mpfr_set_inf (x, -1); + d = (double) mpfr_get_ld_2exp (&exp, x, GMP_RNDZ); + ASSERT_ALWAYS (d < 0); + ASSERT_ALWAYS (DOUBLE_ISINF (d)); + + mpfr_set_nan (x); + d = (double) mpfr_get_ld_2exp (&exp, x, GMP_RNDZ); + ASSERT_ALWAYS (DOUBLE_ISNAN (d)); + + mpfr_clear (x); +#endif +} + + +int +main (void) +{ + tests_start_mpfr (); + mpfr_test_init (); + + check_round (); + check_inf_nan (); + + tests_end_mpfr (); + return 0; +} |