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