Work around a DEC C compiler bug.
[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 #define ASSERT(expr) if (!(expr)) abort ();
28
29 /* The Compaq (ex-DEC) C 6.4 compiler chokes on the expression 0.0 / 0.0.  */
30 #ifdef __DECC
31 static double
32 NaN ()
33 {
34   static double zero = 0.0;
35   return zero / zero;
36 }
37 #else
38 # define NaN() (0.0 / 0.0)
39 #endif
40
41 static double
42 my_ldexp (double x, int d)
43 {
44   for (; d > 0; d--)
45     x *= 2.0;
46   for (; d < 0; d++)
47     x *= 0.5;
48   return x;
49 }
50
51 int
52 main ()
53 {
54   int i;
55   /* The use of 'volatile' guarantees that excess precision bits are dropped
56      when dealing with denormalized numbers.  It is necessary on x86 systems
57      where double-floats are not IEEE compliant by default, to avoid that the
58      results become platform and compiler option dependent.  'volatile' is a
59      portable alternative to gcc's -ffloat-store option.  */
60   volatile double x;
61
62   { /* NaN.  */
63     int exp = -9999;
64     double mantissa;
65     x = NaN ();
66     mantissa = frexp (x, &exp);
67     ASSERT (mantissa != mantissa);
68   }
69
70   { /* Positive infinity.  */
71     int exp = -9999;
72     double mantissa;
73     x = 1.0 / 0.0;
74     mantissa = frexp (x, &exp);
75     ASSERT (mantissa == x);
76   }
77
78   { /* Negative infinity.  */
79     int exp = -9999;
80     double mantissa;
81     x = -1.0 / 0.0;
82     mantissa = frexp (x, &exp);
83     ASSERT (mantissa == x);
84   }
85
86   { /* Positive zero.  */
87     int exp = -9999;
88     double mantissa;
89     x = 0.0;
90     mantissa = frexp (x, &exp);
91     ASSERT (exp == 0);
92     ASSERT (mantissa == x);
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   }
103
104   for (i = 1, x = 1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
105     {
106       int exp = -9999;
107       double mantissa = frexp (x, &exp);
108       ASSERT (exp == i);
109       ASSERT (mantissa == 0.5);
110     }
111   for (i = 1, x = 1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
112     {
113       int exp = -9999;
114       double mantissa = frexp (x, &exp);
115       ASSERT (exp == i);
116       ASSERT (mantissa == 0.5);
117     }
118   for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
119     {
120       int exp = -9999;
121       double mantissa = frexp (x, &exp);
122       ASSERT (exp == i);
123       ASSERT (mantissa == 0.5);
124     }
125
126   for (i = 1, x = -1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
127     {
128       int exp = -9999;
129       double mantissa = frexp (x, &exp);
130       ASSERT (exp == i);
131       ASSERT (mantissa == -0.5);
132     }
133   for (i = 1, x = -1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
134     {
135       int exp = -9999;
136       double mantissa = frexp (x, &exp);
137       ASSERT (exp == i);
138       ASSERT (mantissa == -0.5);
139     }
140   for (; i >= DBL_MIN_EXP - 100 && x < 0.0; i--, x *= 0.5)
141     {
142       int exp = -9999;
143       double mantissa = frexp (x, &exp);
144       ASSERT (exp == i);
145       ASSERT (mantissa == -0.5);
146     }
147
148   for (i = 1, x = 1.01; i <= DBL_MAX_EXP; i++, x *= 2.0)
149     {
150       int exp = -9999;
151       double mantissa = frexp (x, &exp);
152       ASSERT (exp == i);
153       ASSERT (mantissa == 0.505);
154     }
155   for (i = 1, x = 1.01; i >= DBL_MIN_EXP; i--, x *= 0.5)
156     {
157       int exp = -9999;
158       double mantissa = frexp (x, &exp);
159       ASSERT (exp == i);
160       ASSERT (mantissa == 0.505);
161     }
162   for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
163     {
164       int exp = -9999;
165       double mantissa = frexp (x, &exp);
166       ASSERT (exp == i);
167       ASSERT (mantissa >= 0.5);
168       ASSERT (mantissa < 1.0);
169       ASSERT (mantissa == my_ldexp (x, - exp));
170     }
171
172   for (i = 1, x = 1.73205; i <= DBL_MAX_EXP; i++, x *= 2.0)
173     {
174       int exp = -9999;
175       double mantissa = frexp (x, &exp);
176       ASSERT (exp == i);
177       ASSERT (mantissa == 0.866025);
178     }
179   for (i = 1, x = 1.73205; i >= DBL_MIN_EXP; i--, x *= 0.5)
180     {
181       int exp = -9999;
182       double mantissa = frexp (x, &exp);
183       ASSERT (exp == i);
184       ASSERT (mantissa == 0.866025);
185     }
186   for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
187     {
188       int exp = -9999;
189       double mantissa = frexp (x, &exp);
190       ASSERT (exp == i || exp == i + 1);
191       ASSERT (mantissa >= 0.5);
192       ASSERT (mantissa < 1.0);
193       ASSERT (mantissa == my_ldexp (x, - exp));
194     }
195
196   return 0;
197 }