1 /* Test mpfr_get_d_2exp.
3 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramel projects, INRIA.
6 This file is part of the GNU MPFR Library.
8 The GNU MPFR Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
13 The GNU MPFR Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
20 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
27 #include "mpfr-test.h"
30 /* Check that hardware rounding doesn't make mpfr_get_d_2exp return a value
31 outside its defined range. */
35 static const unsigned long data[] = { 1, 32, 53, 54, 64, 128, 256, 512 };
41 mpfr_init2 (f, 1024L);
43 for (rnd_mode = 0; rnd_mode < MPFR_RND_MAX ; rnd_mode++)
45 for (i = 0; i < (int) numberof (data); i++)
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);
51 for (neg = 0; neg <= 1; neg++)
53 got = mpfr_get_d_2exp (&got_exp, f, (mpfr_rnd_t) rnd_mode);
56 ? (got < 0.5 || got >= 1.0)
57 : (got <= -1.0 || got > -0.5))
59 printf ("mpfr_get_d_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]);
64 mpfr_out_str (stdout, 2, 0, f, MPFR_RNDN);
66 d_trace (" got ", got);
67 printf (" got exp %ld\n", got_exp);
71 mpfr_neg (f, f, MPFR_RNDZ);
83 /* only if nans and infs are available */
92 d = mpfr_get_d_2exp (&exp, x, MPFR_RNDZ);
93 ASSERT_ALWAYS (d > 0);
94 ASSERT_ALWAYS (DOUBLE_ISINF (d));
97 d = mpfr_get_d_2exp (&exp, x, MPFR_RNDZ);
98 ASSERT_ALWAYS (d < 0);
99 ASSERT_ALWAYS (DOUBLE_ISINF (d));
102 d = mpfr_get_d_2exp (&exp, x, MPFR_RNDZ);
103 ASSERT_ALWAYS (DOUBLE_ISNAN (d));