Tests for module 'frexpl'.
[pspp] / 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 #define ASSERT(expr) if (!(expr)) abort ();
28
29 static long double
30 my_ldexp (long double x, int d)
31 {
32   for (; d > 0; d--)
33     x *= 2.0L;
34   for (; d < 0; d++)
35     x *= 0.5L;
36   return x;
37 }
38
39 int
40 main ()
41 {
42   int i;
43   long double x;
44
45   { /* NaN.  */
46     int exp = -9999;
47     long double mantissa;
48     x = 0.0L / 0.0L;
49     mantissa = frexpl (x, &exp);
50     ASSERT (mantissa != mantissa);
51   }
52
53   { /* Positive infinity.  */
54     int exp = -9999;
55     long double mantissa;
56     x = 1.0L / 0.0L;
57     mantissa = frexpl (x, &exp);
58     ASSERT (mantissa == x);
59   }
60
61   { /* Negative infinity.  */
62     int exp = -9999;
63     long double mantissa;
64     x = -1.0L / 0.0L;
65     mantissa = frexpl (x, &exp);
66     ASSERT (mantissa == x);
67   }
68
69   { /* Positive zero.  */
70     int exp = -9999;
71     long double mantissa;
72     x = 0.0L;
73     mantissa = frexpl (x, &exp);
74     ASSERT (exp == 0);
75     ASSERT (mantissa == x);
76   }
77
78   { /* Negative zero.  */
79     int exp = -9999;
80     long double mantissa;
81     x = -0.0L;
82     mantissa = frexpl (x, &exp);
83     ASSERT (exp == 0);
84     ASSERT (mantissa == x);
85   }
86
87   for (i = 1, x = 1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
88     {
89       int exp = -9999;
90       long double mantissa = frexpl (x, &exp);
91       ASSERT (exp == i);
92       ASSERT (mantissa == 0.5L);
93     }
94   for (i = 1, x = 1.0L; i >= LDBL_MIN_EXP; i--, x *= 0.5L)
95     {
96       int exp = -9999;
97       long double mantissa = frexpl (x, &exp);
98       ASSERT (exp == i);
99       ASSERT (mantissa == 0.5L);
100     }
101   for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
102     {
103       int exp = -9999;
104       long double mantissa = frexpl (x, &exp);
105       ASSERT (exp == i);
106       ASSERT (mantissa == 0.5L);
107     }
108
109   for (i = 1, x = -1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
110     {
111       int exp = -9999;
112       long double mantissa = frexpl (x, &exp);
113       ASSERT (exp == i);
114       ASSERT (mantissa == -0.5L);
115     }
116   for (i = 1, x = -1.0L; i >= LDBL_MIN_EXP; i--, x *= 0.5L)
117     {
118       int exp = -9999;
119       long double mantissa = frexpl (x, &exp);
120       ASSERT (exp == i);
121       ASSERT (mantissa == -0.5L);
122     }
123   for (; i >= LDBL_MIN_EXP - 100 && x < 0.0L; i--, x *= 0.5L)
124     {
125       int exp = -9999;
126       long double mantissa = frexpl (x, &exp);
127       ASSERT (exp == i);
128       ASSERT (mantissa == -0.5L);
129     }
130
131   for (i = 1, x = 1.01L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
132     {
133       int exp = -9999;
134       long double mantissa = frexpl (x, &exp);
135       ASSERT (exp == i);
136       ASSERT (mantissa == 0.505L);
137     }
138   for (i = 1, x = 1.01L; i >= LDBL_MIN_EXP; i--, x *= 0.5L)
139     {
140       int exp = -9999;
141       long double mantissa = frexpl (x, &exp);
142       ASSERT (exp == i);
143       ASSERT (mantissa == 0.505L);
144     }
145   for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
146     {
147       int exp = -9999;
148       long double mantissa = frexpl (x, &exp);
149       ASSERT (exp == i);
150       ASSERT (mantissa >= 0.5L);
151       ASSERT (mantissa < 1.0L);
152       ASSERT (mantissa == my_ldexp (x, - exp));
153     }
154
155   for (i = 1, x = 1.73205L; 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.866025L);
161     }
162   for (i = 1, x = 1.73205L; i >= LDBL_MIN_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.866025L);
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 || exp == i + 1);
174       ASSERT (mantissa >= 0.5L);
175       ASSERT (mantissa < 1.0L);
176       ASSERT (mantissa == my_ldexp (x, - exp));
177     }
178
179   return 0;
180 }