Work around IRIX 6.5 cc compiler bug, which simplifies x != x to false.
[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 static long double
33 my_ldexp (long double x, int d)
34 {
35   for (; d > 0; d--)
36     x *= 2.0L;
37   for (; d < 0; d++)
38     x *= 0.5L;
39   return x;
40 }
41
42 int
43 main ()
44 {
45   int i;
46   long double x;
47   DECL_LONG_DOUBLE_ROUNDING
48
49   BEGIN_LONG_DOUBLE_ROUNDING ();
50
51   { /* NaN.  */
52     int exp = -9999;
53     long double mantissa;
54     x = 0.0L / 0.0L;
55     mantissa = frexpl (x, &exp);
56     ASSERT (isnanl (mantissa));
57   }
58
59   { /* Positive infinity.  */
60     int exp = -9999;
61     long double mantissa;
62     x = 1.0L / 0.0L;
63     mantissa = frexpl (x, &exp);
64     ASSERT (mantissa == x);
65   }
66
67   { /* Negative infinity.  */
68     int exp = -9999;
69     long double mantissa;
70     x = -1.0L / 0.0L;
71     mantissa = frexpl (x, &exp);
72     ASSERT (mantissa == x);
73   }
74
75   { /* Positive zero.  */
76     int exp = -9999;
77     long double mantissa;
78     x = 0.0L;
79     mantissa = frexpl (x, &exp);
80     ASSERT (exp == 0);
81     ASSERT (mantissa == x);
82   }
83
84   { /* Negative 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   }
92
93   for (i = 1, x = 1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
94     {
95       int exp = -9999;
96       long double mantissa = frexpl (x, &exp);
97       ASSERT (exp == i);
98       ASSERT (mantissa == 0.5L);
99     }
100   for (i = 1, x = 1.0L; i >= LDBL_MIN_EXP; i--, x *= 0.5L)
101     {
102       int exp = -9999;
103       long double mantissa = frexpl (x, &exp);
104       ASSERT (exp == i);
105       ASSERT (mantissa == 0.5L);
106     }
107   for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
108     {
109       int exp = -9999;
110       long double mantissa = frexpl (x, &exp);
111       ASSERT (exp == i);
112       ASSERT (mantissa == 0.5L);
113     }
114
115   for (i = 1, x = -1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
116     {
117       int exp = -9999;
118       long double mantissa = frexpl (x, &exp);
119       ASSERT (exp == i);
120       ASSERT (mantissa == -0.5L);
121     }
122   for (i = 1, x = -1.0L; i >= LDBL_MIN_EXP; i--, x *= 0.5L)
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 >= LDBL_MIN_EXP - 100 && x < 0.0L; 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
137   for (i = 1, x = 1.01L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
138     {
139       int exp = -9999;
140       long double mantissa = frexpl (x, &exp);
141       ASSERT (exp == i);
142       ASSERT (mantissa == 0.505L);
143     }
144   for (i = 1, x = 1.01L; i >= LDBL_MIN_EXP; i--, x *= 0.5L)
145     {
146       int exp = -9999;
147       long double mantissa = frexpl (x, &exp);
148       ASSERT (exp == i);
149       ASSERT (mantissa == 0.505L);
150     }
151   for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; 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       ASSERT (mantissa < 1.0L);
158       ASSERT (mantissa == my_ldexp (x, - exp));
159     }
160
161   for (i = 1, x = 1.73205L; 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.866025L);
167     }
168   for (i = 1, x = 1.73205L; i >= LDBL_MIN_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.866025L);
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 || exp == i + 1);
180       ASSERT (mantissa >= 0.5L);
181       ASSERT (mantissa < 1.0L);
182       ASSERT (mantissa == my_ldexp (x, - exp));
183     }
184
185   return 0;
186 }