tests: add signature checks
[pspp] / tests / test-frexp.c
1 /* Test of splitting a double into fraction and mantissa.
2    Copyright (C) 2007-2009 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 "signature.h"
24 SIGNATURE_CHECK (frexp, double, (double, int *));
25
26 #include <float.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #include "isnand-nolibm.h"
31 #include "nan.h"
32
33 /* Avoid some warnings from "gcc -Wshadow".
34    This file doesn't use the exp() function.  */
35 #undef exp
36 #define exp exponent
37
38 #define ASSERT(expr) \
39   do                                                                         \
40     {                                                                        \
41       if (!(expr))                                                           \
42         {                                                                    \
43           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
44           fflush (stderr);                                                   \
45           abort ();                                                          \
46         }                                                                    \
47     }                                                                        \
48   while (0)
49
50 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0.
51    So we use -zero instead.  */
52 double zero = 0.0;
53
54 static double
55 my_ldexp (double x, int d)
56 {
57   for (; d > 0; d--)
58     x *= 2.0;
59   for (; d < 0; d++)
60     x *= 0.5;
61   return x;
62 }
63
64 int
65 main ()
66 {
67   int i;
68   /* The use of 'volatile' guarantees that excess precision bits are dropped
69      when dealing with denormalized numbers.  It is necessary on x86 systems
70      where double-floats are not IEEE compliant by default, to avoid that the
71      results become platform and compiler option dependent.  'volatile' is a
72      portable alternative to gcc's -ffloat-store option.  */
73   volatile double x;
74
75   { /* NaN.  */
76     int exp = -9999;
77     double mantissa;
78     x = NaNd ();
79     mantissa = frexp (x, &exp);
80     ASSERT (isnand (mantissa));
81   }
82
83   { /* Positive infinity.  */
84     int exp = -9999;
85     double mantissa;
86     x = 1.0 / 0.0;
87     mantissa = frexp (x, &exp);
88     ASSERT (mantissa == x);
89   }
90
91   { /* Negative infinity.  */
92     int exp = -9999;
93     double mantissa;
94     x = -1.0 / 0.0;
95     mantissa = frexp (x, &exp);
96     ASSERT (mantissa == x);
97   }
98
99   { /* Positive zero.  */
100     int exp = -9999;
101     double mantissa;
102     x = 0.0;
103     mantissa = frexp (x, &exp);
104     ASSERT (exp == 0);
105     ASSERT (mantissa == x);
106     ASSERT (!signbit (mantissa));
107   }
108
109   { /* Negative zero.  */
110     int exp = -9999;
111     double mantissa;
112     x = -zero;
113     mantissa = frexp (x, &exp);
114     ASSERT (exp == 0);
115     ASSERT (mantissa == x);
116     ASSERT (signbit (mantissa));
117   }
118
119   for (i = 1, x = 1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
120     {
121       int exp = -9999;
122       double mantissa = frexp (x, &exp);
123       ASSERT (exp == i);
124       ASSERT (mantissa == 0.5);
125     }
126   for (i = 1, x = 1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
127     {
128       int exp = -9999;
129       double mantissa = frexp (x, &exp);
130       ASSERT (exp == i);
131       ASSERT (mantissa == 0.5);
132     }
133   for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
134     {
135       int exp = -9999;
136       double mantissa = frexp (x, &exp);
137       ASSERT (exp == i);
138       ASSERT (mantissa == 0.5);
139     }
140
141   for (i = 1, x = -1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
142     {
143       int exp = -9999;
144       double mantissa = frexp (x, &exp);
145       ASSERT (exp == i);
146       ASSERT (mantissa == -0.5);
147     }
148   for (i = 1, x = -1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
149     {
150       int exp = -9999;
151       double mantissa = frexp (x, &exp);
152       ASSERT (exp == i);
153       ASSERT (mantissa == -0.5);
154     }
155   for (; i >= DBL_MIN_EXP - 100 && x < 0.0; i--, x *= 0.5)
156     {
157       int exp = -9999;
158       double mantissa = frexp (x, &exp);
159       ASSERT (exp == i);
160       ASSERT (mantissa == -0.5);
161     }
162
163   for (i = 1, x = 1.01; i <= DBL_MAX_EXP; i++, x *= 2.0)
164     {
165       int exp = -9999;
166       double mantissa = frexp (x, &exp);
167       ASSERT (exp == i);
168       ASSERT (mantissa == 0.505);
169     }
170   for (i = 1, x = 1.01; i >= DBL_MIN_EXP; i--, x *= 0.5)
171     {
172       int exp = -9999;
173       double mantissa = frexp (x, &exp);
174       ASSERT (exp == i);
175       ASSERT (mantissa == 0.505);
176     }
177   for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
178     {
179       int exp = -9999;
180       double mantissa = frexp (x, &exp);
181       ASSERT (exp == i);
182       ASSERT (mantissa >= 0.5);
183       ASSERT (mantissa < 1.0);
184       ASSERT (mantissa == my_ldexp (x, - exp));
185     }
186
187   for (i = 1, x = 1.73205; i <= DBL_MAX_EXP; i++, x *= 2.0)
188     {
189       int exp = -9999;
190       double mantissa = frexp (x, &exp);
191       ASSERT (exp == i);
192       ASSERT (mantissa == 0.866025);
193     }
194   for (i = 1, x = 1.73205; i >= DBL_MIN_EXP; i--, x *= 0.5)
195     {
196       int exp = -9999;
197       double mantissa = frexp (x, &exp);
198       ASSERT (exp == i);
199       ASSERT (mantissa == 0.866025);
200     }
201   for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
202     {
203       int exp = -9999;
204       double mantissa = frexp (x, &exp);
205       ASSERT (exp == i || exp == i + 1);
206       ASSERT (mantissa >= 0.5);
207       ASSERT (mantissa < 1.0);
208       ASSERT (mantissa == my_ldexp (x, - exp));
209     }
210
211   return 0;
212 }