Fix compilation failure on AIX with xlc.
[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 #include "nan.h"
29
30 /* Avoid some warnings from "gcc -Wshadow".
31    This file doesn't use the exp() function.  */
32 #undef exp
33 #define exp exponent
34
35 #define ASSERT(expr) \
36   do                                                                         \
37     {                                                                        \
38       if (!(expr))                                                           \
39         {                                                                    \
40           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
41           fflush (stderr);                                                   \
42           abort ();                                                          \
43         }                                                                    \
44     }                                                                        \
45   while (0)
46
47 static double
48 my_ldexp (double x, int d)
49 {
50   for (; d > 0; d--)
51     x *= 2.0;
52   for (; d < 0; d++)
53     x *= 0.5;
54   return x;
55 }
56
57 int
58 main ()
59 {
60   int i;
61   /* The use of 'volatile' guarantees that excess precision bits are dropped
62      when dealing with denormalized numbers.  It is necessary on x86 systems
63      where double-floats are not IEEE compliant by default, to avoid that the
64      results become platform and compiler option dependent.  'volatile' is a
65      portable alternative to gcc's -ffloat-store option.  */
66   volatile double x;
67
68   { /* NaN.  */
69     int exp = -9999;
70     double mantissa;
71     x = NaNd ();
72     mantissa = frexp (x, &exp);
73     ASSERT (isnand (mantissa));
74   }
75
76   { /* Positive infinity.  */
77     int exp = -9999;
78     double mantissa;
79     x = 1.0 / 0.0;
80     mantissa = frexp (x, &exp);
81     ASSERT (mantissa == x);
82   }
83
84   { /* Negative infinity.  */
85     int exp = -9999;
86     double mantissa;
87     x = -1.0 / 0.0;
88     mantissa = frexp (x, &exp);
89     ASSERT (mantissa == x);
90   }
91
92   { /* Positive zero.  */
93     int exp = -9999;
94     double mantissa;
95     x = 0.0;
96     mantissa = frexp (x, &exp);
97     ASSERT (exp == 0);
98     ASSERT (mantissa == x);
99     ASSERT (!signbit (mantissa));
100   }
101
102   { /* Negative zero.  */
103     int exp = -9999;
104     double mantissa;
105     x = -0.0;
106     mantissa = frexp (x, &exp);
107     ASSERT (exp == 0);
108     ASSERT (mantissa == x);
109     ASSERT (signbit (mantissa));
110   }
111
112   for (i = 1, x = 1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
113     {
114       int exp = -9999;
115       double mantissa = frexp (x, &exp);
116       ASSERT (exp == i);
117       ASSERT (mantissa == 0.5);
118     }
119   for (i = 1, x = 1.0; i >= DBL_MIN_EXP; 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   for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
127     {
128       int exp = -9999;
129       double mantissa = frexp (x, &exp);
130       ASSERT (exp == i);
131       ASSERT (mantissa == 0.5);
132     }
133
134   for (i = 1, x = -1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
135     {
136       int exp = -9999;
137       double mantissa = frexp (x, &exp);
138       ASSERT (exp == i);
139       ASSERT (mantissa == -0.5);
140     }
141   for (i = 1, x = -1.0; i >= DBL_MIN_EXP; 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   for (; i >= DBL_MIN_EXP - 100 && x < 0.0; i--, x *= 0.5)
149     {
150       int exp = -9999;
151       double mantissa = frexp (x, &exp);
152       ASSERT (exp == i);
153       ASSERT (mantissa == -0.5);
154     }
155
156   for (i = 1, x = 1.01; i <= DBL_MAX_EXP; i++, x *= 2.0)
157     {
158       int exp = -9999;
159       double mantissa = frexp (x, &exp);
160       ASSERT (exp == i);
161       ASSERT (mantissa == 0.505);
162     }
163   for (i = 1, x = 1.01; i >= DBL_MIN_EXP; i--, x *= 0.5)
164     {
165       int exp = -9999;
166       double mantissa = frexp (x, &exp);
167       ASSERT (exp == i);
168       ASSERT (mantissa == 0.505);
169     }
170   for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
171     {
172       int exp = -9999;
173       double mantissa = frexp (x, &exp);
174       ASSERT (exp == i);
175       ASSERT (mantissa >= 0.5);
176       ASSERT (mantissa < 1.0);
177       ASSERT (mantissa == my_ldexp (x, - exp));
178     }
179
180   for (i = 1, x = 1.73205; i <= DBL_MAX_EXP; i++, x *= 2.0)
181     {
182       int exp = -9999;
183       double mantissa = frexp (x, &exp);
184       ASSERT (exp == i);
185       ASSERT (mantissa == 0.866025);
186     }
187   for (i = 1, x = 1.73205; i >= DBL_MIN_EXP; i--, x *= 0.5)
188     {
189       int exp = -9999;
190       double mantissa = frexp (x, &exp);
191       ASSERT (exp == i);
192       ASSERT (mantissa == 0.866025);
193     }
194   for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
195     {
196       int exp = -9999;
197       double mantissa = frexp (x, &exp);
198       ASSERT (exp == i || exp == i + 1);
199       ASSERT (mantissa >= 0.5);
200       ASSERT (mantissa < 1.0);
201       ASSERT (mantissa == my_ldexp (x, - exp));
202     }
203
204   return 0;
205 }