Guarantee a definition of NAN.
[gnulib.git] / tests / test-frexp.c
1 /* Test of splitting a double into fraction and mantissa.
2    Copyright (C) 2007-2008 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
18
19 #include <config.h>
20
21 #include <math.h>
22
23 #include <float.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #include "isnand.h"
28
29 #define ASSERT(expr) \
30   do                                                                         \
31     {                                                                        \
32       if (!(expr))                                                           \
33         {                                                                    \
34           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
35           abort ();                                                          \
36         }                                                                    \
37     }                                                                        \
38   while (0)
39
40 static double
41 my_ldexp (double x, int d)
42 {
43   for (; d > 0; d--)
44     x *= 2.0;
45   for (; d < 0; d++)
46     x *= 0.5;
47   return x;
48 }
49
50 int
51 main ()
52 {
53   int i;
54   /* The use of 'volatile' guarantees that excess precision bits are dropped
55      when dealing with denormalized numbers.  It is necessary on x86 systems
56      where double-floats are not IEEE compliant by default, to avoid that the
57      results become platform and compiler option dependent.  'volatile' is a
58      portable alternative to gcc's -ffloat-store option.  */
59   volatile double x;
60
61   { /* NaN.  */
62     int exp = -9999;
63     double mantissa;
64     x = NAN;
65     mantissa = frexp (x, &exp);
66     ASSERT (isnand (mantissa));
67   }
68
69   { /* Positive infinity.  */
70     int exp = -9999;
71     double mantissa;
72     x = 1.0 / 0.0;
73     mantissa = frexp (x, &exp);
74     ASSERT (mantissa == x);
75   }
76
77   { /* Negative infinity.  */
78     int exp = -9999;
79     double mantissa;
80     x = -1.0 / 0.0;
81     mantissa = frexp (x, &exp);
82     ASSERT (mantissa == x);
83   }
84
85   { /* Positive zero.  */
86     int exp = -9999;
87     double mantissa;
88     x = 0.0;
89     mantissa = frexp (x, &exp);
90     ASSERT (exp == 0);
91     ASSERT (mantissa == x);
92     ASSERT (!signbit (mantissa));
93   }
94
95   { /* Negative zero.  */
96     int exp = -9999;
97     double mantissa;
98     x = -0.0;
99     mantissa = frexp (x, &exp);
100     ASSERT (exp == 0);
101     ASSERT (mantissa == x);
102     ASSERT (signbit (mantissa));
103   }
104
105   for (i = 1, x = 1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
106     {
107       int exp = -9999;
108       double mantissa = frexp (x, &exp);
109       ASSERT (exp == i);
110       ASSERT (mantissa == 0.5);
111     }
112   for (i = 1, x = 1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
113     {
114       int exp = -9999;
115       double mantissa = frexp (x, &exp);
116       ASSERT (exp == i);
117       ASSERT (mantissa == 0.5);
118     }
119   for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
120     {
121       int exp = -9999;
122       double mantissa = frexp (x, &exp);
123       ASSERT (exp == i);
124       ASSERT (mantissa == 0.5);
125     }
126
127   for (i = 1, x = -1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
128     {
129       int exp = -9999;
130       double mantissa = frexp (x, &exp);
131       ASSERT (exp == i);
132       ASSERT (mantissa == -0.5);
133     }
134   for (i = 1, x = -1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
135     {
136       int exp = -9999;
137       double mantissa = frexp (x, &exp);
138       ASSERT (exp == i);
139       ASSERT (mantissa == -0.5);
140     }
141   for (; i >= DBL_MIN_EXP - 100 && x < 0.0; i--, x *= 0.5)
142     {
143       int exp = -9999;
144       double mantissa = frexp (x, &exp);
145       ASSERT (exp == i);
146       ASSERT (mantissa == -0.5);
147     }
148
149   for (i = 1, x = 1.01; i <= DBL_MAX_EXP; i++, x *= 2.0)
150     {
151       int exp = -9999;
152       double mantissa = frexp (x, &exp);
153       ASSERT (exp == i);
154       ASSERT (mantissa == 0.505);
155     }
156   for (i = 1, x = 1.01; i >= DBL_MIN_EXP; i--, x *= 0.5)
157     {
158       int exp = -9999;
159       double mantissa = frexp (x, &exp);
160       ASSERT (exp == i);
161       ASSERT (mantissa == 0.505);
162     }
163   for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
164     {
165       int exp = -9999;
166       double mantissa = frexp (x, &exp);
167       ASSERT (exp == i);
168       ASSERT (mantissa >= 0.5);
169       ASSERT (mantissa < 1.0);
170       ASSERT (mantissa == my_ldexp (x, - exp));
171     }
172
173   for (i = 1, x = 1.73205; i <= DBL_MAX_EXP; i++, x *= 2.0)
174     {
175       int exp = -9999;
176       double mantissa = frexp (x, &exp);
177       ASSERT (exp == i);
178       ASSERT (mantissa == 0.866025);
179     }
180   for (i = 1, x = 1.73205; i >= DBL_MIN_EXP; i--, x *= 0.5)
181     {
182       int exp = -9999;
183       double mantissa = frexp (x, &exp);
184       ASSERT (exp == i);
185       ASSERT (mantissa == 0.866025);
186     }
187   for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
188     {
189       int exp = -9999;
190       double mantissa = frexp (x, &exp);
191       ASSERT (exp == i || exp == i + 1);
192       ASSERT (mantissa >= 0.5);
193       ASSERT (mantissa < 1.0);
194       ASSERT (mantissa == my_ldexp (x, - exp));
195     }
196
197   return 0;
198 }