added missing dependencies to fix failing unistr/ tests
[gnulib.git] / tests / test-frexp.c
1 /* Test of splitting a double into fraction and mantissa.
2    Copyright (C) 2007-2010 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 "signature.h"
24 SIGNATURE_CHECK (frexp, double, (double, int *));
25
26 #include <float.h>
27
28 #include "isnand-nolibm.h"
29 #include "nan.h"
30 #include "macros.h"
31
32 /* Avoid some warnings from "gcc -Wshadow".
33    This file doesn't use the exp() function.  */
34 #undef exp
35 #define exp exponent
36
37 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0.
38    So we use -zero instead.  */
39 double zero = 0.0;
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 = NaNd ();
66     mantissa = frexp (x, &exp);
67     ASSERT (isnand (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     ASSERT (!signbit (mantissa));
94   }
95
96   { /* Negative zero.  */
97     int exp = -9999;
98     double mantissa;
99     x = -zero;
100     mantissa = frexp (x, &exp);
101     ASSERT (exp == 0);
102     ASSERT (mantissa == x);
103     ASSERT (signbit (mantissa));
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 }