finalise NEWS.stable
[gnulib.git] / tests / test-frexp.c
1 /* Test of splitting a double into fraction and mantissa.
2    Copyright (C) 2007-2011 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 "minus-zero.h"
30 #include "infinity.h"
31 #include "nan.h"
32 #include "macros.h"
33
34 /* Avoid some warnings from "gcc -Wshadow".
35    This file doesn't use the exp() function.  */
36 #undef exp
37 #define exp exponent
38
39 static double
40 my_ldexp (double x, int d)
41 {
42   for (; d > 0; d--)
43     x *= 2.0;
44   for (; d < 0; d++)
45     x *= 0.5;
46   return x;
47 }
48
49 int
50 main ()
51 {
52   int i;
53   /* The use of 'volatile' guarantees that excess precision bits are dropped
54      when dealing with denormalized numbers.  It is necessary on x86 systems
55      where double-floats are not IEEE compliant by default, to avoid that the
56      results become platform and compiler option dependent.  'volatile' is a
57      portable alternative to gcc's -ffloat-store option.  */
58   volatile double x;
59
60   { /* NaN.  */
61     int exp = -9999;
62     double mantissa;
63     x = NaNd ();
64     mantissa = frexp (x, &exp);
65     ASSERT (isnand (mantissa));
66   }
67
68   { /* Positive infinity.  */
69     int exp = -9999;
70     double mantissa;
71     x = Infinityd ();
72     mantissa = frexp (x, &exp);
73     ASSERT (mantissa == x);
74   }
75
76   { /* Negative infinity.  */
77     int exp = -9999;
78     double mantissa;
79     x = - Infinityd ();
80     mantissa = frexp (x, &exp);
81     ASSERT (mantissa == x);
82   }
83
84   { /* Positive zero.  */
85     int exp = -9999;
86     double mantissa;
87     x = 0.0;
88     mantissa = frexp (x, &exp);
89     ASSERT (exp == 0);
90     ASSERT (mantissa == x);
91     ASSERT (!signbit (mantissa));
92   }
93
94   { /* Negative zero.  */
95     int exp = -9999;
96     double mantissa;
97     x = minus_zerod;
98     mantissa = frexp (x, &exp);
99     ASSERT (exp == 0);
100     ASSERT (mantissa == x);
101     ASSERT (signbit (mantissa));
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 }