Use spaces for indentation, not tabs.
[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-nolibm.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 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0.
48    So we use -zero instead.  */
49 double zero = 0.0;
50
51 static double
52 my_ldexp (double x, int d)
53 {
54   for (; d > 0; d--)
55     x *= 2.0;
56   for (; d < 0; d++)
57     x *= 0.5;
58   return x;
59 }
60
61 int
62 main ()
63 {
64   int i;
65   /* The use of 'volatile' guarantees that excess precision bits are dropped
66      when dealing with denormalized numbers.  It is necessary on x86 systems
67      where double-floats are not IEEE compliant by default, to avoid that the
68      results become platform and compiler option dependent.  'volatile' is a
69      portable alternative to gcc's -ffloat-store option.  */
70   volatile double x;
71
72   { /* NaN.  */
73     int exp = -9999;
74     double mantissa;
75     x = NaNd ();
76     mantissa = frexp (x, &exp);
77     ASSERT (isnand (mantissa));
78   }
79
80   { /* Positive infinity.  */
81     int exp = -9999;
82     double mantissa;
83     x = 1.0 / 0.0;
84     mantissa = frexp (x, &exp);
85     ASSERT (mantissa == x);
86   }
87
88   { /* Negative infinity.  */
89     int exp = -9999;
90     double mantissa;
91     x = -1.0 / 0.0;
92     mantissa = frexp (x, &exp);
93     ASSERT (mantissa == x);
94   }
95
96   { /* Positive zero.  */
97     int exp = -9999;
98     double mantissa;
99     x = 0.0;
100     mantissa = frexp (x, &exp);
101     ASSERT (exp == 0);
102     ASSERT (mantissa == x);
103     ASSERT (!signbit (mantissa));
104   }
105
106   { /* Negative zero.  */
107     int exp = -9999;
108     double mantissa;
109     x = -zero;
110     mantissa = frexp (x, &exp);
111     ASSERT (exp == 0);
112     ASSERT (mantissa == x);
113     ASSERT (signbit (mantissa));
114   }
115
116   for (i = 1, x = 1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
117     {
118       int exp = -9999;
119       double mantissa = frexp (x, &exp);
120       ASSERT (exp == i);
121       ASSERT (mantissa == 0.5);
122     }
123   for (i = 1, x = 1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
124     {
125       int exp = -9999;
126       double mantissa = frexp (x, &exp);
127       ASSERT (exp == i);
128       ASSERT (mantissa == 0.5);
129     }
130   for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
131     {
132       int exp = -9999;
133       double mantissa = frexp (x, &exp);
134       ASSERT (exp == i);
135       ASSERT (mantissa == 0.5);
136     }
137
138   for (i = 1, x = -1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
139     {
140       int exp = -9999;
141       double mantissa = frexp (x, &exp);
142       ASSERT (exp == i);
143       ASSERT (mantissa == -0.5);
144     }
145   for (i = 1, x = -1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
146     {
147       int exp = -9999;
148       double mantissa = frexp (x, &exp);
149       ASSERT (exp == i);
150       ASSERT (mantissa == -0.5);
151     }
152   for (; i >= DBL_MIN_EXP - 100 && x < 0.0; i--, x *= 0.5)
153     {
154       int exp = -9999;
155       double mantissa = frexp (x, &exp);
156       ASSERT (exp == i);
157       ASSERT (mantissa == -0.5);
158     }
159
160   for (i = 1, x = 1.01; i <= DBL_MAX_EXP; i++, x *= 2.0)
161     {
162       int exp = -9999;
163       double mantissa = frexp (x, &exp);
164       ASSERT (exp == i);
165       ASSERT (mantissa == 0.505);
166     }
167   for (i = 1, x = 1.01; i >= DBL_MIN_EXP; i--, x *= 0.5)
168     {
169       int exp = -9999;
170       double mantissa = frexp (x, &exp);
171       ASSERT (exp == i);
172       ASSERT (mantissa == 0.505);
173     }
174   for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
175     {
176       int exp = -9999;
177       double mantissa = frexp (x, &exp);
178       ASSERT (exp == i);
179       ASSERT (mantissa >= 0.5);
180       ASSERT (mantissa < 1.0);
181       ASSERT (mantissa == my_ldexp (x, - exp));
182     }
183
184   for (i = 1, x = 1.73205; i <= DBL_MAX_EXP; i++, x *= 2.0)
185     {
186       int exp = -9999;
187       double mantissa = frexp (x, &exp);
188       ASSERT (exp == i);
189       ASSERT (mantissa == 0.866025);
190     }
191   for (i = 1, x = 1.73205; i >= DBL_MIN_EXP; i--, x *= 0.5)
192     {
193       int exp = -9999;
194       double mantissa = frexp (x, &exp);
195       ASSERT (exp == i);
196       ASSERT (mantissa == 0.866025);
197     }
198   for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
199     {
200       int exp = -9999;
201       double mantissa = frexp (x, &exp);
202       ASSERT (exp == i || exp == i + 1);
203       ASSERT (mantissa >= 0.5);
204       ASSERT (mantissa < 1.0);
205       ASSERT (mantissa == my_ldexp (x, - exp));
206     }
207
208   return 0;
209 }