Fix bug in error handling code.
[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 static long double
61 my_ldexp (long double x, int d)
62 {
63   for (; d > 0; d--)
64     x *= 2.0L;
65   for (; d < 0; d++)
66     x *= 0.5L;
67   return x;
68 }
69
70 int
71 main ()
72 {
73   int i;
74   long double x;
75   DECL_LONG_DOUBLE_ROUNDING
76
77   BEGIN_LONG_DOUBLE_ROUNDING ();
78
79   { /* NaN.  */
80     int exp = -9999;
81     long double mantissa;
82     x = 0.0L / 0.0L;
83     mantissa = frexpl (x, &exp);
84     ASSERT (isnanl (mantissa));
85   }
86
87   { /* Positive infinity.  */
88     int exp = -9999;
89     long double mantissa;
90     x = 1.0L / 0.0L;
91     mantissa = frexpl (x, &exp);
92     ASSERT (mantissa == x);
93   }
94
95   { /* Negative infinity.  */
96     int exp = -9999;
97     long double mantissa;
98     x = -1.0L / 0.0L;
99     mantissa = frexpl (x, &exp);
100     ASSERT (mantissa == x);
101   }
102
103   { /* Positive zero.  */
104     int exp = -9999;
105     long double mantissa;
106     x = 0.0L;
107     mantissa = frexpl (x, &exp);
108     ASSERT (exp == 0);
109     ASSERT (mantissa == x);
110     ASSERT (!signbit (mantissa));
111   }
112
113   { /* Negative 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   for (i = 1, x = 1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
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 = 1, x = 1.0L; i >= MIN_NORMAL_EXP; 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   for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
138     {
139       int exp = -9999;
140       long double mantissa = frexpl (x, &exp);
141       ASSERT (exp == i);
142       ASSERT (mantissa == 0.5L);
143     }
144
145   for (i = 1, x = -1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
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 = 1, x = -1.0L; i >= MIN_NORMAL_EXP; 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   for (; i >= LDBL_MIN_EXP - 100 && x < 0.0L; i--, x *= 0.5L)
160     {
161       int exp = -9999;
162       long double mantissa = frexpl (x, &exp);
163       ASSERT (exp == i);
164       ASSERT (mantissa == -0.5L);
165     }
166
167   for (i = 1, x = 1.01L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
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 = 1, x = 1.01L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
175     {
176       int exp = -9999;
177       long double mantissa = frexpl (x, &exp);
178       ASSERT (exp == i);
179       ASSERT (mantissa == 0.505L);
180     }
181   for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
182     {
183       int exp = -9999;
184       long double mantissa = frexpl (x, &exp);
185       ASSERT (exp == i);
186       ASSERT (mantissa >= 0.5L);
187       ASSERT (mantissa < 1.0L);
188       ASSERT (mantissa == my_ldexp (x, - exp));
189     }
190
191   for (i = 1, x = 1.73205L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
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 = 1, x = 1.73205L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
199     {
200       int exp = -9999;
201       long double mantissa = frexpl (x, &exp);
202       ASSERT (exp == i);
203       ASSERT (mantissa == 0.866025L);
204     }
205   for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
206     {
207       int exp = -9999;
208       long double mantissa = frexpl (x, &exp);
209       ASSERT (exp == i || exp == i + 1);
210       ASSERT (mantissa >= 0.5L);
211       ASSERT (mantissa < 1.0L);
212       ASSERT (mantissa == my_ldexp (x, - exp));
213     }
214
215   return 0;
216 }