* doc/headers/assert.texi (assert.h): Document assert module use.
[gnulib.git] / tests / test-frexp.c
1 /* Test of splitting a double into fraction and mantissa.
2    Copyright (C) 2007 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 2, or (at your option)
7    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, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
19
20 #include <config.h>
21
22 #include <math.h>
23
24 #include <float.h>
25 #include <stdlib.h>
26
27 #include "isnan.h"
28
29 #define ASSERT(expr) if (!(expr)) abort ();
30
31 /* The Compaq (ex-DEC) C 6.4 compiler chokes on the expression 0.0 / 0.0.  */
32 #ifdef __DECC
33 static double
34 NaN ()
35 {
36   static double zero = 0.0;
37   return zero / zero;
38 }
39 #else
40 # define NaN() (0.0 / 0.0)
41 #endif
42
43 static double
44 my_ldexp (double x, int d)
45 {
46   for (; d > 0; d--)
47     x *= 2.0;
48   for (; d < 0; d++)
49     x *= 0.5;
50   return x;
51 }
52
53 int
54 main ()
55 {
56   int i;
57   /* The use of 'volatile' guarantees that excess precision bits are dropped
58      when dealing with denormalized numbers.  It is necessary on x86 systems
59      where double-floats are not IEEE compliant by default, to avoid that the
60      results become platform and compiler option dependent.  'volatile' is a
61      portable alternative to gcc's -ffloat-store option.  */
62   volatile double x;
63
64   { /* NaN.  */
65     int exp = -9999;
66     double mantissa;
67     x = NaN ();
68     mantissa = frexp (x, &exp);
69     ASSERT (isnan (mantissa));
70   }
71
72   { /* Positive infinity.  */
73     int exp = -9999;
74     double mantissa;
75     x = 1.0 / 0.0;
76     mantissa = frexp (x, &exp);
77     ASSERT (mantissa == x);
78   }
79
80   { /* Negative 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   { /* Positive zero.  */
89     int exp = -9999;
90     double mantissa;
91     x = 0.0;
92     mantissa = frexp (x, &exp);
93     ASSERT (exp == 0);
94     ASSERT (mantissa == x);
95     ASSERT (!signbit (mantissa));
96   }
97
98   { /* Negative zero.  */
99     int exp = -9999;
100     double mantissa;
101     x = -0.0;
102     mantissa = frexp (x, &exp);
103     ASSERT (exp == 0);
104     ASSERT (mantissa == x);
105     ASSERT (signbit (mantissa));
106   }
107
108   for (i = 1, x = 1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
109     {
110       int exp = -9999;
111       double mantissa = frexp (x, &exp);
112       ASSERT (exp == i);
113       ASSERT (mantissa == 0.5);
114     }
115   for (i = 1, x = 1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
116     {
117       int exp = -9999;
118       double mantissa = frexp (x, &exp);
119       ASSERT (exp == i);
120       ASSERT (mantissa == 0.5);
121     }
122   for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
123     {
124       int exp = -9999;
125       double mantissa = frexp (x, &exp);
126       ASSERT (exp == i);
127       ASSERT (mantissa == 0.5);
128     }
129
130   for (i = 1, x = -1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
131     {
132       int exp = -9999;
133       double mantissa = frexp (x, &exp);
134       ASSERT (exp == i);
135       ASSERT (mantissa == -0.5);
136     }
137   for (i = 1, x = -1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
138     {
139       int exp = -9999;
140       double mantissa = frexp (x, &exp);
141       ASSERT (exp == i);
142       ASSERT (mantissa == -0.5);
143     }
144   for (; i >= DBL_MIN_EXP - 100 && x < 0.0; i--, x *= 0.5)
145     {
146       int exp = -9999;
147       double mantissa = frexp (x, &exp);
148       ASSERT (exp == i);
149       ASSERT (mantissa == -0.5);
150     }
151
152   for (i = 1, x = 1.01; i <= DBL_MAX_EXP; i++, x *= 2.0)
153     {
154       int exp = -9999;
155       double mantissa = frexp (x, &exp);
156       ASSERT (exp == i);
157       ASSERT (mantissa == 0.505);
158     }
159   for (i = 1, x = 1.01; i >= DBL_MIN_EXP; i--, x *= 0.5)
160     {
161       int exp = -9999;
162       double mantissa = frexp (x, &exp);
163       ASSERT (exp == i);
164       ASSERT (mantissa == 0.505);
165     }
166   for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
167     {
168       int exp = -9999;
169       double mantissa = frexp (x, &exp);
170       ASSERT (exp == i);
171       ASSERT (mantissa >= 0.5);
172       ASSERT (mantissa < 1.0);
173       ASSERT (mantissa == my_ldexp (x, - exp));
174     }
175
176   for (i = 1, x = 1.73205; i <= DBL_MAX_EXP; i++, x *= 2.0)
177     {
178       int exp = -9999;
179       double mantissa = frexp (x, &exp);
180       ASSERT (exp == i);
181       ASSERT (mantissa == 0.866025);
182     }
183   for (i = 1, x = 1.73205; i >= DBL_MIN_EXP; i--, x *= 0.5)
184     {
185       int exp = -9999;
186       double mantissa = frexp (x, &exp);
187       ASSERT (exp == i);
188       ASSERT (mantissa == 0.866025);
189     }
190   for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
191     {
192       int exp = -9999;
193       double mantissa = frexp (x, &exp);
194       ASSERT (exp == i || exp == i + 1);
195       ASSERT (mantissa >= 0.5);
196       ASSERT (mantissa < 1.0);
197       ASSERT (mantissa == my_ldexp (x, - exp));
198     }
199
200   return 0;
201 }