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