* doc/headers/assert.texi (assert.h): Document assert module use.
[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 2, or (at your option)
7    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, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
19
20 #include <config.h>
21
22 #include <math.h>
23
24 #include <float.h>
25 #include <stdlib.h>
26
27 #include "fpucw.h"
28 #include "isnanl-nolibm.h"
29
30 #define ASSERT(expr) if (!(expr)) abort ();
31
32 /* On MIPS IRIX machines, LDBL_MIN_EXP is -1021, but the smallest reliable
33    exponent for 'long double' is -964.  For exponents below that, the
34    precision may be truncated to the precision used for 'double'.  */
35 #ifdef __sgi
36 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 57)
37 #else
38 # define MIN_NORMAL_EXP LDBL_MIN_EXP
39 #endif
40
41 static long double
42 my_ldexp (long double x, int d)
43 {
44   for (; d > 0; d--)
45     x *= 2.0L;
46   for (; d < 0; d++)
47     x *= 0.5L;
48   return x;
49 }
50
51 int
52 main ()
53 {
54   int i;
55   long double x;
56   DECL_LONG_DOUBLE_ROUNDING
57
58   BEGIN_LONG_DOUBLE_ROUNDING ();
59
60   { /* NaN.  */
61     int exp = -9999;
62     long double mantissa;
63     x = 0.0L / 0.0L;
64     mantissa = frexpl (x, &exp);
65     ASSERT (isnanl (mantissa));
66   }
67
68   { /* Positive infinity.  */
69     int exp = -9999;
70     long double mantissa;
71     x = 1.0L / 0.0L;
72     mantissa = frexpl (x, &exp);
73     ASSERT (mantissa == x);
74   }
75
76   { /* Negative infinity.  */
77     int exp = -9999;
78     long double mantissa;
79     x = -1.0L / 0.0L;
80     mantissa = frexpl (x, &exp);
81     ASSERT (mantissa == x);
82   }
83
84   { /* Positive zero.  */
85     int exp = -9999;
86     long double mantissa;
87     x = 0.0L;
88     mantissa = frexpl (x, &exp);
89     ASSERT (exp == 0);
90     ASSERT (mantissa == x);
91     ASSERT (!signbit (mantissa));
92   }
93
94   { /* Negative zero.  */
95     int exp = -9999;
96     long double mantissa;
97     x = -0.0L;
98     mantissa = frexpl (x, &exp);
99     ASSERT (exp == 0);
100     ASSERT (mantissa == x);
101     ASSERT (signbit (mantissa));
102   }
103
104   for (i = 1, x = 1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
105     {
106       int exp = -9999;
107       long double mantissa = frexpl (x, &exp);
108       ASSERT (exp == i);
109       ASSERT (mantissa == 0.5L);
110     }
111   for (i = 1, x = 1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
112     {
113       int exp = -9999;
114       long double mantissa = frexpl (x, &exp);
115       ASSERT (exp == i);
116       ASSERT (mantissa == 0.5L);
117     }
118   for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
119     {
120       int exp = -9999;
121       long double mantissa = frexpl (x, &exp);
122       ASSERT (exp == i);
123       ASSERT (mantissa == 0.5L);
124     }
125
126   for (i = 1, x = -1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
127     {
128       int exp = -9999;
129       long double mantissa = frexpl (x, &exp);
130       ASSERT (exp == i);
131       ASSERT (mantissa == -0.5L);
132     }
133   for (i = 1, x = -1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
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 >= LDBL_MIN_EXP - 100 && x < 0.0L; 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
148   for (i = 1, x = 1.01L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
149     {
150       int exp = -9999;
151       long double mantissa = frexpl (x, &exp);
152       ASSERT (exp == i);
153       ASSERT (mantissa == 0.505L);
154     }
155   for (i = 1, x = 1.01L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
156     {
157       int exp = -9999;
158       long double mantissa = frexpl (x, &exp);
159       ASSERT (exp == i);
160       ASSERT (mantissa == 0.505L);
161     }
162   for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; 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       ASSERT (mantissa < 1.0L);
169       ASSERT (mantissa == my_ldexp (x, - exp));
170     }
171
172   for (i = 1, x = 1.73205L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
173     {
174       int exp = -9999;
175       long double mantissa = frexpl (x, &exp);
176       ASSERT (exp == i);
177       ASSERT (mantissa == 0.866025L);
178     }
179   for (i = 1, x = 1.73205L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
180     {
181       int exp = -9999;
182       long double mantissa = frexpl (x, &exp);
183       ASSERT (exp == i);
184       ASSERT (mantissa == 0.866025L);
185     }
186   for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
187     {
188       int exp = -9999;
189       long double mantissa = frexpl (x, &exp);
190       ASSERT (exp == i || exp == i + 1);
191       ASSERT (mantissa >= 0.5L);
192       ASSERT (mantissa < 1.0L);
193       ASSERT (mantissa == my_ldexp (x, - exp));
194     }
195
196   return 0;
197 }