Work around IRIX 6.5 cc compiler bug, which simplifies x != x to false.
[pspp] / 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   }
96
97   { /* Negative zero.  */
98     int exp = -9999;
99     double mantissa;
100     x = -0.0;
101     mantissa = frexp (x, &exp);
102     ASSERT (exp == 0);
103     ASSERT (mantissa == x);
104   }
105
106   for (i = 1, x = 1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
107     {
108       int exp = -9999;
109       double mantissa = frexp (x, &exp);
110       ASSERT (exp == i);
111       ASSERT (mantissa == 0.5);
112     }
113   for (i = 1, x = 1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
114     {
115       int exp = -9999;
116       double mantissa = frexp (x, &exp);
117       ASSERT (exp == i);
118       ASSERT (mantissa == 0.5);
119     }
120   for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
121     {
122       int exp = -9999;
123       double mantissa = frexp (x, &exp);
124       ASSERT (exp == i);
125       ASSERT (mantissa == 0.5);
126     }
127
128   for (i = 1, x = -1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
129     {
130       int exp = -9999;
131       double mantissa = frexp (x, &exp);
132       ASSERT (exp == i);
133       ASSERT (mantissa == -0.5);
134     }
135   for (i = 1, x = -1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
136     {
137       int exp = -9999;
138       double mantissa = frexp (x, &exp);
139       ASSERT (exp == i);
140       ASSERT (mantissa == -0.5);
141     }
142   for (; i >= DBL_MIN_EXP - 100 && x < 0.0; i--, x *= 0.5)
143     {
144       int exp = -9999;
145       double mantissa = frexp (x, &exp);
146       ASSERT (exp == i);
147       ASSERT (mantissa == -0.5);
148     }
149
150   for (i = 1, x = 1.01; i <= DBL_MAX_EXP; i++, x *= 2.0)
151     {
152       int exp = -9999;
153       double mantissa = frexp (x, &exp);
154       ASSERT (exp == i);
155       ASSERT (mantissa == 0.505);
156     }
157   for (i = 1, x = 1.01; i >= DBL_MIN_EXP; i--, x *= 0.5)
158     {
159       int exp = -9999;
160       double mantissa = frexp (x, &exp);
161       ASSERT (exp == i);
162       ASSERT (mantissa == 0.505);
163     }
164   for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
165     {
166       int exp = -9999;
167       double mantissa = frexp (x, &exp);
168       ASSERT (exp == i);
169       ASSERT (mantissa >= 0.5);
170       ASSERT (mantissa < 1.0);
171       ASSERT (mantissa == my_ldexp (x, - exp));
172     }
173
174   for (i = 1, x = 1.73205; i <= DBL_MAX_EXP; i++, x *= 2.0)
175     {
176       int exp = -9999;
177       double mantissa = frexp (x, &exp);
178       ASSERT (exp == i);
179       ASSERT (mantissa == 0.866025);
180     }
181   for (i = 1, x = 1.73205; i >= DBL_MIN_EXP; i--, x *= 0.5)
182     {
183       int exp = -9999;
184       double mantissa = frexp (x, &exp);
185       ASSERT (exp == i);
186       ASSERT (mantissa == 0.866025);
187     }
188   for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
189     {
190       int exp = -9999;
191       double mantissa = frexp (x, &exp);
192       ASSERT (exp == i || exp == i + 1);
193       ASSERT (mantissa >= 0.5);
194       ASSERT (mantissa < 1.0);
195       ASSERT (mantissa == my_ldexp (x, - exp));
196     }
197
198   return 0;
199 }