maint: update all copyright year number ranges
[gnulib.git] / tests / test-frexpl.c
1 /* Test of splitting a 'long double' into fraction and mantissa.
2    Copyright (C) 2007-2012 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 (frexpl, long double, (long double, int *));
25
26 #include <float.h>
27
28 #include "fpucw.h"
29 #include "isnanl-nolibm.h"
30 #include "minus-zero.h"
31 #include "infinity.h"
32 #include "nan.h"
33 #include "macros.h"
34
35 /* Avoid some warnings from "gcc -Wshadow".
36    This file doesn't use the exp() function.  */
37 #undef exp
38 #define exp exponent
39
40 /* On MIPS IRIX machines, LDBL_MIN_EXP is -1021, but the smallest reliable
41    exponent for 'long double' is -964.  Similarly, on PowerPC machines,
42    LDBL_MIN_EXP is -1021, but the smallest reliable exponent for 'long double'
43    is -968.  For exponents below that, the precision may be truncated to the
44    precision used for 'double'.  */
45 #ifdef __sgi
46 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 57)
47 #elif defined __ppc || defined __ppc__ || defined __powerpc || defined __powerpc__
48 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 53)
49 #else
50 # define MIN_NORMAL_EXP LDBL_MIN_EXP
51 #endif
52
53 static long double
54 my_ldexp (long double x, int d)
55 {
56   for (; d > 0; d--)
57     x *= 2.0L;
58   for (; d < 0; d++)
59     x *= 0.5L;
60   return x;
61 }
62
63 int
64 main ()
65 {
66   int i;
67   long double x;
68   DECL_LONG_DOUBLE_ROUNDING
69
70   BEGIN_LONG_DOUBLE_ROUNDING ();
71
72   { /* NaN.  */
73     int exp = -9999;
74     long double mantissa;
75     x = NaNl ();
76     mantissa = frexpl (x, &exp);
77     ASSERT (isnanl (mantissa));
78   }
79
80   { /* Positive infinity.  */
81     int exp = -9999;
82     long double mantissa;
83     x = Infinityl ();
84     mantissa = frexpl (x, &exp);
85     ASSERT (mantissa == x);
86   }
87
88   { /* Negative infinity.  */
89     int exp = -9999;
90     long double mantissa;
91     x = - Infinityl ();
92     mantissa = frexpl (x, &exp);
93     ASSERT (mantissa == x);
94   }
95
96   { /* Positive zero.  */
97     int exp = -9999;
98     long double mantissa;
99     x = 0.0L;
100     mantissa = frexpl (x, &exp);
101     ASSERT (exp == 0);
102     ASSERT (mantissa == x);
103     ASSERT (!signbit (mantissa));
104   }
105
106   { /* Negative zero.  */
107     int exp = -9999;
108     long double mantissa;
109     x = minus_zerol;
110     mantissa = frexpl (x, &exp);
111     ASSERT (exp == 0);
112     ASSERT (mantissa == x);
113     ASSERT (signbit (mantissa));
114   }
115
116   for (i = 1, x = 1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
117     {
118       int exp = -9999;
119       long double mantissa = frexpl (x, &exp);
120       ASSERT (exp == i);
121       ASSERT (mantissa == 0.5L);
122     }
123   for (i = 1, x = 1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
124     {
125       int exp = -9999;
126       long double mantissa = frexpl (x, &exp);
127       ASSERT (exp == i);
128       ASSERT (mantissa == 0.5L);
129     }
130   for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
131     {
132       int exp = -9999;
133       long double mantissa = frexpl (x, &exp);
134       ASSERT (exp == i);
135       ASSERT (mantissa == 0.5L);
136     }
137
138   for (i = 1, x = -1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
139     {
140       int exp = -9999;
141       long double mantissa = frexpl (x, &exp);
142       ASSERT (exp == i);
143       ASSERT (mantissa == -0.5L);
144     }
145   for (i = 1, x = -1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
146     {
147       int exp = -9999;
148       long double mantissa = frexpl (x, &exp);
149       ASSERT (exp == i);
150       ASSERT (mantissa == -0.5L);
151     }
152   for (; i >= LDBL_MIN_EXP - 100 && x < 0.0L; i--, x *= 0.5L)
153     {
154       int exp = -9999;
155       long double mantissa = frexpl (x, &exp);
156       ASSERT (exp == i);
157       ASSERT (mantissa == -0.5L);
158     }
159
160   for (i = 1, x = 1.01L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
161     {
162       int exp = -9999;
163       long double mantissa = frexpl (x, &exp);
164       ASSERT (exp == i);
165       ASSERT (mantissa == 0.505L);
166     }
167   for (i = 1, x = 1.01L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
168     {
169       int exp = -9999;
170       long double mantissa = frexpl (x, &exp);
171       ASSERT (exp == i);
172       ASSERT (mantissa == 0.505L);
173     }
174   for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
175     {
176       int exp = -9999;
177       long double mantissa = frexpl (x, &exp);
178       ASSERT (exp == i);
179       ASSERT (mantissa >= 0.5L);
180       ASSERT (mantissa < 1.0L);
181       ASSERT (mantissa == my_ldexp (x, - exp));
182     }
183
184   for (i = 1, x = 1.73205L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
185     {
186       int exp = -9999;
187       long double mantissa = frexpl (x, &exp);
188       ASSERT (exp == i);
189       ASSERT (mantissa == 0.866025L);
190     }
191   for (i = 1, x = 1.73205L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
192     {
193       int exp = -9999;
194       long double mantissa = frexpl (x, &exp);
195       ASSERT (exp == i);
196       ASSERT (mantissa == 0.866025L);
197     }
198   for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
199     {
200       int exp = -9999;
201       long double mantissa = frexpl (x, &exp);
202       ASSERT (exp == i || exp == i + 1);
203       ASSERT (mantissa >= 0.5L);
204       ASSERT (mantissa < 1.0L);
205       ASSERT (mantissa == my_ldexp (x, - exp));
206     }
207
208   return 0;
209 }