1 /* Test mpfr_get_d_2exp.
2
3 Copyright 1999-2023 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramba projects, INRIA.
5
6 This file is part of the GNU MPFR Library.
7
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.
12
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.
17
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 https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22
23 #include <float.h>
24
25 #include "mpfr-test.h"
26
27
28 /* Check that hardware rounding doesn't make mpfr_get_d_2exp return a value
29 outside its defined range. */
30 static void
check_round(void)31 check_round (void)
32 {
33 static const unsigned long data[] = { 1, 32, 53, 54, 64, 128, 256, 512 };
34 mpfr_t f;
35 double got;
36 long got_exp;
37 int i, rnd_mode, neg;
38
39 mpfr_init2 (f, 1024L);
40
41 RND_LOOP (rnd_mode)
42 {
43 for (i = 0; i < (int) numberof (data); i++)
44 {
45 mpfr_set_ui (f, 1L, MPFR_RNDZ);
46 mpfr_mul_2ui (f, f, data[i], MPFR_RNDZ);
47 mpfr_sub_ui (f, f, 1L, MPFR_RNDZ);
48
49 for (neg = 0; neg <= 1; neg++)
50 {
51 got = mpfr_get_d_2exp (&got_exp, f, (mpfr_rnd_t) rnd_mode);
52
53 if (neg == 0
54 ? (got < 0.5 || got >= 1.0)
55 : (got <= -1.0 || got > -0.5))
56 {
57 printf ("mpfr_get_d_2exp wrong on 2**%lu-1\n", data[i]);
58 printf ("result out of range, expect 0.5 <= got < 1.0\n");
59 printf (" rnd_mode = %d\n", rnd_mode);
60 printf (" data[i] = %lu\n", data[i]);
61 printf (" f ");
62 mpfr_out_str (stdout, 2, 0, f, MPFR_RNDN);
63 printf ("\n");
64 d_trace (" got ", got);
65 printf (" got exp %ld\n", got_exp);
66 exit(1);
67 }
68
69 mpfr_neg (f, f, MPFR_RNDZ);
70 }
71 }
72 }
73
74 mpfr_clear (f);
75 }
76
77
78 static void
check_inf_nan(void)79 check_inf_nan (void)
80 {
81 #if !defined(MPFR_ERRDIVZERO)
82 mpfr_t x;
83 double d;
84 long exp;
85
86 mpfr_init2 (x, 123);
87
88 mpfr_set_inf (x, 1);
89 d = mpfr_get_d_2exp (&exp, x, MPFR_RNDZ);
90 MPFR_ASSERTN (d > 0);
91 MPFR_ASSERTN (DOUBLE_ISINF (d));
92
93 mpfr_set_inf (x, -1);
94 d = mpfr_get_d_2exp (&exp, x, MPFR_RNDZ);
95 MPFR_ASSERTN (d < 0);
96 MPFR_ASSERTN (DOUBLE_ISINF (d));
97
98 mpfr_set_nan (x);
99 d = mpfr_get_d_2exp (&exp, x, MPFR_RNDZ);
100 MPFR_ASSERTN (DOUBLE_ISNAN (d));
101
102 mpfr_clear (x);
103 #endif
104 }
105
106
107 int
main(void)108 main (void)
109 {
110 tests_start_mpfr ();
111 mpfr_test_init ();
112
113 check_round ();
114 check_inf_nan ();
115
116 tests_end_mpfr ();
117 return 0;
118 }
119