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