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