xref: /netbsd-src/external/lgpl3/gmp/dist/tests/mpf/t-get_d.c (revision 72c7faa4dbb41dbb0238d6b4a109da0d4b236dd4)
1 /* Test mpf_get_d and mpf_set_d.
2 
3 Copyright 1996, 1999-2001, 2009 Free Software Foundation, Inc.
4 
5 This file is part of the GNU MP Library test suite.
6 
7 The GNU MP Library test suite is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 3 of the License,
10 or (at your option) any later version.
11 
12 The GNU MP Library test suite is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
15 Public License for more details.
16 
17 You should have received a copy of the GNU General Public License along with
18 the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include "gmp-impl.h"
23 #include "tests.h"
24 
25 #if defined (__vax) || defined (__vax__)
26 #define LOW_BOUND 1e-38
27 #define HIGH_BOUND 8e37
28 #endif
29 
30 #if defined (_CRAY) && ! defined (_CRAYIEEE)
31 /* The range varies mysteriously between Cray version.  On an SV1,
32    the range seem to be 1e-600..1e603, but a cfp (non-ieee) T90
33    has a much smaller range of 1e-240..1e240.  */
34 #define LOW_BOUND 1e-240
35 #define HIGH_BOUND 1e240
36 #endif
37 
38 #if ! defined (LOW_BOUND)
39 #define LOW_BOUND 1e-300
40 #define HIGH_BOUND 1e300
41 #endif
42 
43 void
test_denorms(int prc)44 test_denorms (int prc)
45 {
46 #ifdef _GMP_IEEE_FLOATS
47   double d1, d2;
48   mpf_t f;
49   int i;
50 
51   mpf_set_default_prec (prc);
52 
53   mpf_init (f);
54 
55   d1 = 1.9;
56   for (i = 0; i < 820; i++)
57     {
58       mpf_set_d (f, d1);
59       d2 = mpf_get_d (f);
60       if (d1 != d2)
61         abort ();
62       d1 *= 0.4;
63     }
64 
65   mpf_clear (f);
66 #endif
67 }
68 
69 int
main(int argc,char ** argv)70 main (int argc, char **argv)
71 {
72   double d, e, r;
73   mpf_t u, v;
74 
75   tests_start ();
76   mpf_init (u);
77   mpf_init (v);
78 
79   mpf_set_d (u, LOW_BOUND);
80   for (d = 2.0 * LOW_BOUND; d < HIGH_BOUND; d *= 1.01)
81     {
82       mpf_set_d (v, d);
83       if (mpf_cmp (u, v) >= 0)
84 	abort ();
85       e = mpf_get_d (v);
86       r = e/d;
87       if (r < 0.99999999999999 || r > 1.00000000000001)
88 	{
89 	  fprintf (stderr, "should be one ulp from 1: %.16f\n", r);
90 	  abort ();
91 	}
92       mpf_set (u, v);
93     }
94 
95   mpf_clear (u);
96   mpf_clear (v);
97 
98   test_denorms (10);
99   test_denorms (32);
100   test_denorms (64);
101   test_denorms (100);
102   test_denorms (200);
103 
104   tests_end ();
105   exit (0);
106 }
107