Merge branch 'stable'
[gnulib.git] / tests / test-frexpf.c
1 /* Test of splitting a float into fraction and mantissa.
2    Copyright (C) 2007-2011 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 (frexpf, float, (float, int *));
25
26 #include <float.h>
27
28 #include "isnanf-nolibm.h"
29 #include "minus-zero.h"
30 #include "infinity.h"
31 #include "nan.h"
32 #include "macros.h"
33
34 /* Avoid some warnings from "gcc -Wshadow".
35    This file doesn't use the exp() function.  */
36 #undef exp
37 #define exp exponent
38
39 static float
40 my_ldexp (float x, int d)
41 {
42   for (; d > 0; d--)
43     x *= 2.0f;
44   for (; d < 0; d++)
45     x *= 0.5f;
46   return x;
47 }
48
49 int
50 main ()
51 {
52   int i;
53   volatile float x;
54
55   { /* NaN.  */
56     int exp = -9999;
57     float mantissa;
58     x = NaNf ();
59     mantissa = frexpf (x, &exp);
60     ASSERT (isnanf (mantissa));
61   }
62
63   { /* Positive infinity.  */
64     int exp = -9999;
65     float mantissa;
66     x = Infinityf ();
67     mantissa = frexpf (x, &exp);
68     ASSERT (mantissa == x);
69   }
70
71   { /* Negative infinity.  */
72     int exp = -9999;
73     float mantissa;
74     x = - Infinityf ();
75     mantissa = frexpf (x, &exp);
76     ASSERT (mantissa == x);
77   }
78
79   { /* Positive zero.  */
80     int exp = -9999;
81     float mantissa;
82     x = 0.0f;
83     mantissa = frexpf (x, &exp);
84     ASSERT (exp == 0);
85     ASSERT (mantissa == x);
86     ASSERT (!signbit (mantissa));
87   }
88
89   { /* Negative zero.  */
90     int exp = -9999;
91     float mantissa;
92     x = minus_zerof;
93     mantissa = frexpf (x, &exp);
94     ASSERT (exp == 0);
95     ASSERT (mantissa == x);
96     ASSERT (signbit (mantissa));
97   }
98
99   for (i = 1, x = 1.0f; i <= FLT_MAX_EXP; i++, x *= 2.0f)
100     {
101       int exp = -9999;
102       float mantissa = frexpf (x, &exp);
103       ASSERT (exp == i);
104       ASSERT (mantissa == 0.5f);
105     }
106   for (i = 1, x = 1.0f; i >= FLT_MIN_EXP; i--, x *= 0.5f)
107     {
108       int exp = -9999;
109       float mantissa = frexpf (x, &exp);
110       ASSERT (exp == i);
111       ASSERT (mantissa == 0.5f);
112     }
113   for (; i >= FLT_MIN_EXP - 100 && x > 0.0f; i--, x *= 0.5f)
114     {
115       int exp = -9999;
116       float mantissa = frexpf (x, &exp);
117       ASSERT (exp == i);
118       ASSERT (mantissa == 0.5f);
119     }
120
121   for (i = 1, x = -1.0f; i <= FLT_MAX_EXP; i++, x *= 2.0f)
122     {
123       int exp = -9999;
124       float mantissa = frexpf (x, &exp);
125       ASSERT (exp == i);
126       ASSERT (mantissa == -0.5f);
127     }
128   for (i = 1, x = -1.0f; i >= FLT_MIN_EXP; i--, x *= 0.5f)
129     {
130       int exp = -9999;
131       float mantissa = frexpf (x, &exp);
132       ASSERT (exp == i);
133       ASSERT (mantissa == -0.5f);
134     }
135   for (; i >= FLT_MIN_EXP - 100 && x < 0.0f; i--, x *= 0.5f)
136     {
137       int exp = -9999;
138       float mantissa = frexpf (x, &exp);
139       ASSERT (exp == i);
140       ASSERT (mantissa == -0.5f);
141     }
142
143   for (i = 1, x = 1.01f; i <= FLT_MAX_EXP; i++, x *= 2.0f)
144     {
145       int exp = -9999;
146       float mantissa = frexpf (x, &exp);
147       ASSERT (exp == i);
148       ASSERT (mantissa == 0.505f);
149     }
150   for (i = 1, x = 1.01f; i >= FLT_MIN_EXP; i--, x *= 0.5f)
151     {
152       int exp = -9999;
153       float mantissa = frexpf (x, &exp);
154       ASSERT (exp == i);
155       ASSERT (mantissa == 0.505f);
156     }
157   for (; i >= FLT_MIN_EXP - 100 && x > 0.0f; i--, x *= 0.5f)
158     {
159       int exp = -9999;
160       float mantissa = frexpf (x, &exp);
161       ASSERT (exp == i);
162       ASSERT (mantissa >= 0.5f);
163       ASSERT (mantissa < 1.0f);
164       ASSERT (mantissa == my_ldexp (x, - exp));
165     }
166
167   for (i = 1, x = 1.73205f; i <= FLT_MAX_EXP; i++, x *= 2.0f)
168     {
169       int exp = -9999;
170       float mantissa = frexpf (x, &exp);
171       ASSERT (exp == i);
172       ASSERT (mantissa == 0.866025f);
173     }
174   for (i = 1, x = 1.73205f; i >= FLT_MIN_EXP; i--, x *= 0.5f)
175     {
176       int exp = -9999;
177       float mantissa = frexpf (x, &exp);
178       ASSERT (exp == i);
179       ASSERT (mantissa == 0.866025f);
180     }
181   for (; i >= FLT_MIN_EXP - 100 && x > 0.0f; i--, x *= 0.5f)
182     {
183       int exp = -9999;
184       float mantissa = frexpf (x, &exp);
185       ASSERT (exp == i || exp == i + 1);
186       ASSERT (mantissa >= 0.5f);
187       ASSERT (mantissa < 1.0f);
188       ASSERT (mantissa == my_ldexp (x, - exp));
189     }
190
191   return 0;
192 }