1 /* Test mpf_get_d_2exp.
3 Copyright 2002, 2003 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
30 static const long data[] = {
31 -513, -512, -511, -65, -64, -63, -32, -1,
32 0, 1, 32, 53, 54, 64, 128, 256, 511, 512, 513
36 long got_exp, want_exp;
41 for (i = 0; i < numberof (data); i++)
45 mpf_mul_2exp (f, f, data[i]);
47 mpf_div_2exp (f, f, -data[i]);
49 want_exp = data[i] + 1;
51 got = mpf_get_d_2exp (&got_exp, f);
52 if (got != want || got_exp != want_exp)
54 printf ("mpf_get_d_2exp wrong on 2**%ld\n", data[i]);
56 d_trace (" want ", want);
57 d_trace (" got ", got);
58 printf (" want exp %ld\n", want_exp);
59 printf (" got exp %ld\n", got_exp);
66 /* Check that hardware rounding doesn't make mpf_get_d_2exp return a value
67 outside its defined range. */
71 static const unsigned long data[] = { 1, 32, 53, 54, 64, 128, 256, 512 };
75 int i, rnd_mode, old_rnd_mode;
78 old_rnd_mode = tests_hardware_getround ();
80 for (rnd_mode = 0; rnd_mode < 4; rnd_mode++)
82 tests_hardware_setround (rnd_mode);
84 for (i = 0; i < numberof (data); i++)
87 mpf_mul_2exp (f, f, data[i]);
88 mpf_sub_ui (f, f, 1L);
90 got = mpf_get_d_2exp (&got_exp, f);
91 if (got < 0.5 || got >= 1.0)
93 printf ("mpf_get_d_2exp bad on 2**%lu-1\n", data[i]);
94 printf ("result out of range, expect 0.5 <= got < 1.0\n");
95 printf (" rnd_mode = %d\n", rnd_mode);
96 printf (" data[i] = %lu\n", data[i]);
98 d_trace (" got ", got);
99 printf (" got exp %ld\n", got_exp);
106 tests_hardware_setround (old_rnd_mode);