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