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