1/* Test mpfr_get_ld_2exp. 2 3Copyright 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. 4Contributed by the Arenaire and Cacao projects, INRIA. 5 6This file is part of the GNU MPFR Library. 7Contributed by Damien Stehle. 8 9The GNU MPFR Library is free software; you can redistribute it and/or modify 10it under the terms of the GNU Lesser General Public License as published by 11the Free Software Foundation; either version 3 of the License, or (at your 12option) any later version. 13 14The GNU MPFR Library is distributed in the hope that it will be useful, but 15WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 17License for more details. 18 19You should have received a copy of the GNU Lesser General Public License 20along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see 21http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc., 2251 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */ 23 24#include <stdio.h> 25#include <stdlib.h> 26#include <float.h> 27 28#include "mpfr-test.h" 29 30/* Check that hardware rounding doesn't make mpfr_get_ld_2exp return a value 31 outside its defined range. */ 32static void 33check_round (void) 34{ 35 static const unsigned long data[] = {1, 32, 53, 54, 63, 64, 65, 127, 128, 256, 512 }; 36 mpfr_t f; 37 long double got; 38 long got_exp; 39 int i, rnd_mode, neg; 40 41 mpfr_init2 (f, 1024L); 42 43 for (rnd_mode = 0; rnd_mode < MPFR_RND_MAX ; rnd_mode++) 44 { 45 for (i = 0; i < (int) numberof (data); i++) 46 { 47 mpfr_set_ui (f, 1L, MPFR_RNDZ); 48 mpfr_mul_2exp (f, f, data[i], MPFR_RNDZ); 49 mpfr_sub_ui (f, f, 1L, MPFR_RNDZ); 50 51 for (neg = 0; neg <= 1; neg++) 52 { 53 got = mpfr_get_ld_2exp (&got_exp, f, (mpfr_rnd_t) rnd_mode); 54 55 if (neg == 0 56 ? (got < 0.5 || got >= 1.0) 57 : (got <= -1.0 || got > -0.5)) 58 { 59 printf ("mpfr_get_ld_2exp wrong on 2**%lu-1\n", data[i]); 60 printf ("result out of range, expect 0.5 <= got < 1.0\n"); 61 printf (" rnd_mode = %d\n", rnd_mode); 62 printf (" data[i] = %lu\n", data[i]); 63 printf (" f "); 64 mpfr_out_str (stdout, 2, 0, f, MPFR_RNDN); 65 printf ("\n"); 66 d_trace (" got ", got); 67 printf (" got exp %ld\n", got_exp); 68 exit(1); 69 } 70 71 mpfr_neg (f, f, MPFR_RNDZ); 72 } 73 } 74 } 75 76 mpfr_clear (f); 77} 78 79 80static void 81check_inf_nan (void) 82{ 83 /* only if nans and infs are available */ 84#if _GMP_IEEE_FLOATS 85 mpfr_t x; 86 double d; 87 long exp; 88 89 mpfr_init2 (x, 123); 90 91 mpfr_set_inf (x, 1); 92 d = (double) mpfr_get_ld_2exp (&exp, x, MPFR_RNDZ); 93 ASSERT_ALWAYS (d > 0); 94 ASSERT_ALWAYS (DOUBLE_ISINF (d)); 95 96 mpfr_set_inf (x, -1); 97 d = (double) mpfr_get_ld_2exp (&exp, x, MPFR_RNDZ); 98 ASSERT_ALWAYS (d < 0); 99 ASSERT_ALWAYS (DOUBLE_ISINF (d)); 100 101 mpfr_set_nan (x); 102 d = (double) mpfr_get_ld_2exp (&exp, x, MPFR_RNDZ); 103 ASSERT_ALWAYS (DOUBLE_ISNAN (d)); 104 105 mpfr_clear (x); 106#endif 107} 108 109static void 110bug20090520 (void) 111{ 112 mpfr_t x; 113 long double d, e; 114 int i; 115 116 mpfr_init (x); 117 mpfr_set_ui (x, 1, MPFR_RNDN); 118 d = 1.0; 119 mpfr_div_2exp (x, x, 16383, MPFR_RNDN); 120 for (i = 0; i < 16383; i++) 121 d *= 0.5; 122 e = mpfr_get_ld (x, MPFR_RNDN); 123 if (e != d) 124 { 125 printf ("mpfr_get_ld(1e-16383) failed\n"); 126 printf ("expected %.20Le\n", d); 127 printf ("got %.20Le\n", e); 128 exit (1); 129 } 130 mpfr_clear (x); 131} 132 133int 134main (void) 135{ 136 tests_start_mpfr (); 137 mpfr_test_init (); 138 139 bug20090520 (); 140 141 check_round (); 142 check_inf_nan (); 143 144 tests_end_mpfr (); 145 return 0; 146} 147