1 /* Test file for mpfr_get_decimal64 and mpfr_set_decimal64.
3 Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012 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. */
23 #ifdef MPFR_WANT_DECIMAL_FLOATS
25 #include <stdlib.h> /* for exit */
26 #include "mpfr-test.h"
31 print_decimal64 (_Decimal64 d)
33 union ieee_double_extract x;
34 union ieee_double_decimal64 y;
40 printf ("|%d%d%d%d%d%d", x.s.sig, Gh >> 4, (Gh >> 3) & 1,
41 (Gh >> 2) & 1, (Gh >> 1) & 1, Gh & 1);
42 printf ("%d%d%d%d%d%d", (x.s.exp >> 5) & 1, (x.s.exp >> 4) & 1,
43 (x.s.exp >> 3) & 1, (x.s.exp >> 2) & 1, (x.s.exp >> 1) & 1,
45 for (i = 20; i > 0; i--)
46 printf ("%d", (x.s.manh >> (i - 1)) & 1);
47 for (i = 32; i > 0; i--)
48 printf ("%d", (x.s.manl >> (i - 1)) & 1);
62 d = mpfr_get_decimal64 (x, MPFR_RNDZ);
63 mpfr_set_ui (x, 1, MPFR_RNDZ);
64 mpfr_set_decimal64 (x, d, MPFR_RNDZ);
65 ASSERT_ALWAYS (mpfr_nan_p (x));
68 d = mpfr_get_decimal64 (x, MPFR_RNDZ);
69 mpfr_set_ui (x, 1, MPFR_RNDZ);
70 mpfr_set_decimal64 (x, d, MPFR_RNDZ);
71 ASSERT_ALWAYS (mpfr_inf_p (x) && mpfr_sgn (x) > 0);
74 d = mpfr_get_decimal64 (x, MPFR_RNDZ);
75 mpfr_set_ui (x, 1, MPFR_RNDZ);
76 mpfr_set_decimal64 (x, d, MPFR_RNDZ);
77 ASSERT_ALWAYS (mpfr_inf_p (x) && mpfr_sgn (x) < 0);
79 mpfr_set_ui (x, 0, MPFR_RNDZ);
80 d = mpfr_get_decimal64 (x, MPFR_RNDZ);
81 mpfr_set_ui (x, 1, MPFR_RNDZ);
82 mpfr_set_decimal64 (x, d, MPFR_RNDZ);
83 ASSERT_ALWAYS (mpfr_cmp_ui (x, 0) == 0 && MPFR_SIGN (x) > 0);
85 mpfr_set_ui (x, 0, MPFR_RNDZ);
86 mpfr_neg (x, x, MPFR_RNDZ);
87 d = mpfr_get_decimal64 (x, MPFR_RNDZ);
88 mpfr_set_ui (x, 1, MPFR_RNDZ);
89 mpfr_set_decimal64 (x, d, MPFR_RNDZ);
90 ASSERT_ALWAYS (mpfr_cmp_ui (x, 0) == 0 && MPFR_SIGN (x) < 0);
92 mpfr_set_ui (x, 1, MPFR_RNDZ);
93 d = mpfr_get_decimal64 (x, MPFR_RNDZ);
94 mpfr_set_ui (x, 0, MPFR_RNDZ);
95 mpfr_set_decimal64 (x, d, MPFR_RNDZ);
96 ASSERT_ALWAYS (mpfr_cmp_ui (x, 1) == 0);
98 mpfr_set_si (x, -1, MPFR_RNDZ);
99 d = mpfr_get_decimal64 (x, MPFR_RNDZ);
100 mpfr_set_ui (x, 0, MPFR_RNDZ);
101 mpfr_set_decimal64 (x, d, MPFR_RNDZ);
102 ASSERT_ALWAYS (mpfr_cmp_si (x, -1) == 0);
104 mpfr_set_ui (x, 2, MPFR_RNDZ);
105 d = mpfr_get_decimal64 (x, MPFR_RNDZ);
106 mpfr_set_ui (x, 0, MPFR_RNDZ);
107 mpfr_set_decimal64 (x, d, MPFR_RNDZ);
108 ASSERT_ALWAYS (mpfr_cmp_ui (x, 2) == 0);
110 mpfr_set_ui (x, 99, MPFR_RNDZ);
111 d = mpfr_get_decimal64 (x, MPFR_RNDZ);
112 mpfr_set_ui (x, 0, MPFR_RNDZ);
113 mpfr_set_decimal64 (x, d, MPFR_RNDZ);
114 ASSERT_ALWAYS (mpfr_cmp_ui (x, 99) == 0);
116 mpfr_set_str (x, "9999999999999999", 10, MPFR_RNDZ);
117 mpfr_set (y, x, MPFR_RNDZ);
118 d = mpfr_get_decimal64 (x, MPFR_RNDZ);
119 mpfr_set_ui (x, 0, MPFR_RNDZ);
120 mpfr_set_decimal64 (x, d, MPFR_RNDZ);
121 ASSERT_ALWAYS (mpfr_cmp (x, y) == 0);
123 /* smallest normal number */
124 mpfr_set_str (x, "1E-383", 10, MPFR_RNDU);
125 mpfr_set (y, x, MPFR_RNDZ);
126 d = mpfr_get_decimal64 (x, MPFR_RNDZ);
127 mpfr_set_ui (x, 0, MPFR_RNDZ);
128 mpfr_set_decimal64 (x, d, MPFR_RNDU);
129 ASSERT_ALWAYS (mpfr_cmp (x, y) == 0);
131 /* smallest subnormal number */
132 mpfr_set_str (x, "1E-398", 10, MPFR_RNDU);
133 mpfr_set (y, x, MPFR_RNDZ);
134 d = mpfr_get_decimal64 (x, MPFR_RNDZ);
135 mpfr_set_ui (x, 0, MPFR_RNDZ);
136 mpfr_set_decimal64 (x, d, MPFR_RNDU);
137 ASSERT_ALWAYS (mpfr_cmp (x, y) == 0);
139 /* subnormal number with exponent change when we round back
140 from 16 digits to 1 digit */
141 mpfr_set_str (x, "9.9E-398", 10, MPFR_RNDN);
142 d = mpfr_get_decimal64 (x, MPFR_RNDU); /* should be 1E-397 */
143 mpfr_set_ui (x, 0, MPFR_RNDZ);
144 mpfr_set_decimal64 (x, d, MPFR_RNDD);
145 mpfr_set_str (y, "1E-397", 10, MPFR_RNDN);
146 ASSERT_ALWAYS (mpfr_cmp (x, y) == 0);
149 mpfr_set_str (x, "9.999999999999999E384", 10, MPFR_RNDZ);
150 mpfr_set (y, x, MPFR_RNDZ);
151 d = mpfr_get_decimal64 (x, MPFR_RNDU);
152 mpfr_set_ui (x, 0, MPFR_RNDZ);
153 mpfr_set_decimal64 (x, d, MPFR_RNDZ);
154 ASSERT_ALWAYS (mpfr_cmp (x, y) == 0);
156 mpfr_set_prec (x, 53);
157 mpfr_set_prec (y, 53);
160 mpfr_set_str (x, "9.999999999999999E384", 10, MPFR_RNDZ);
161 d = mpfr_get_decimal64 (x, MPFR_RNDZ);
162 mpfr_set_decimal64 (y, d, MPFR_RNDU);
163 ASSERT_ALWAYS (mpfr_cmp (x, y) == 0);
179 for (i = 0; i < 100000; i++)
181 mpfr_urandomb (x, RANDS); /* 0 <= x < 1 */
182 /* the normal decimal64 range contains [2^(-1272), 2^1278] */
183 mpfr_mul_2si (x, x, (i % 2550) - 1272, MPFR_RNDN);
184 if (mpfr_get_exp (x) <= -1272)
185 mpfr_mul_2exp (x, x, -1271 - mpfr_get_exp (x), MPFR_RNDN);
186 d = mpfr_get_decimal64 (x, MPFR_RNDN);
187 mpfr_set_decimal64 (y, d, MPFR_RNDN);
188 if (mpfr_cmp (x, y) != 0)
190 printf ("x="); mpfr_dump (x);
191 printf ("d="); print_decimal64 (d);
192 printf ("y="); mpfr_dump (y);
201 /* check with native decimal formats */
210 /* check important constants are correctly converted */
211 mpfr_set_ui (x, 17, MPFR_RNDN);
212 d = mpfr_get_decimal64 (x, MPFR_RNDN);
213 MPFR_ASSERTN(d == 17.0dd);
215 mpfr_set_ui (x, 42, MPFR_RNDN);
216 d = mpfr_get_decimal64 (x, MPFR_RNDN);
217 MPFR_ASSERTN(d == 42.0dd);
219 mpfr_set_decimal64 (x, 17.0dd, MPFR_RNDN);
220 MPFR_ASSERTN(mpfr_cmp_ui (x, 17) == 0);
222 mpfr_set_decimal64 (x, 42.0dd, MPFR_RNDN);
223 MPFR_ASSERTN(mpfr_cmp_ui (x, 42) == 0);
236 printf ("Using DPD format\n");
238 printf ("Using BID format\n");
249 #else /* MPFR_WANT_DECIMAL_FLOATS */
257 #endif /* MPFR_WANT_DECIMAL_FLOATS */