1 /* Test of <float.h> substitute.
2 Copyright (C) 2011-2017 Free Software Foundation, Inc.
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.
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.
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/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2011. */
26 /* Check that FLT_RADIX is a constant expression. */
27 int a
[] = { FLT_RADIX
};
38 /* Invariant: 2^n == x * y^k. */
56 /* Now k == 0, hence x == 2^n. */
65 volatile double x
= 1;
66 volatile double y
= 2;
67 /* Invariant: 2^n == x * y^k. */
85 /* Now k == 0, hence x == 2^n. */
94 volatile long double x
= 1;
95 volatile long double y
= 2;
96 /* Invariant: 2^n == x * y^k. */
104 if (k
!= 2 * (k
/ 2))
114 /* Now k == 0, hence x == 2^n. */
118 /* ----------------------- Check macros for 'float' ----------------------- */
120 /* Check that the FLT_* macros expand to constant expressions. */
123 FLT_MANT_DIG
, FLT_MIN_EXP
, FLT_MAX_EXP
,
124 FLT_DIG
, FLT_MIN_10_EXP
, FLT_MAX_10_EXP
126 float fc
[] = { FLT_EPSILON
, FLT_MIN
, FLT_MAX
};
131 /* Check that the value of FLT_MIN_EXP is well parenthesized. */
132 ASSERT ((FLT_MIN_EXP
% 101111) == (FLT_MIN_EXP
) % 101111);
134 /* Check that the value of DBL_MIN_10_EXP is well parenthesized. */
135 ASSERT ((FLT_MIN_10_EXP
% 101111) == (FLT_MIN_10_EXP
) % 101111);
137 /* Check that 'float' is as specified in IEEE 754. */
138 ASSERT (FLT_MANT_DIG
== 24);
139 ASSERT (FLT_MIN_EXP
== -125);
140 ASSERT (FLT_MAX_EXP
== 128);
142 /* Check the value of FLT_MIN_10_EXP. */
143 ASSERT (FLT_MIN_10_EXP
== - (int) (- (FLT_MIN_EXP
- 1) * 0.30103));
145 /* Check the value of FLT_DIG. */
146 ASSERT (FLT_DIG
== (int) ((FLT_MANT_DIG
- 1) * 0.30103));
148 /* Check the value of FLT_MIN_10_EXP. */
149 ASSERT (FLT_MIN_10_EXP
== - (int) (- (FLT_MIN_EXP
- 1) * 0.30103));
151 /* Check the value of FLT_MAX_10_EXP. */
152 ASSERT (FLT_MAX_10_EXP
== (int) (FLT_MAX_EXP
* 0.30103));
154 /* Check the value of FLT_MAX. */
156 volatile float m
= FLT_MAX
;
160 for (n
= 0; n
<= 2 * FLT_MANT_DIG
; n
++)
162 volatile float pow2_n
= pow2f (n
); /* 2^n */
163 volatile float x
= m
+ (m
/ pow2_n
);
167 ASSERT (!(x
+ x
== x
));
171 /* Check the value of FLT_MIN. */
173 volatile float m
= FLT_MIN
;
174 volatile float x
= pow2f (FLT_MIN_EXP
- 1);
178 /* Check the value of FLT_EPSILON. */
180 volatile float e
= FLT_EPSILON
;
186 ASSERT (me
- 1.0f
== e
);
187 for (n
= 0; n
<= 2 * FLT_MANT_DIG
; n
++)
189 volatile float half_n
= pow2f (- n
); /* 2^-n */
190 volatile float x
= me
- half_n
;
197 /* ----------------------- Check macros for 'double' ----------------------- */
199 /* Check that the DBL_* macros expand to constant expressions. */
202 DBL_MANT_DIG
, DBL_MIN_EXP
, DBL_MAX_EXP
,
203 DBL_DIG
, DBL_MIN_10_EXP
, DBL_MAX_10_EXP
205 double dc
[] = { DBL_EPSILON
, DBL_MIN
, DBL_MAX
};
210 /* Check that the value of DBL_MIN_EXP is well parenthesized. */
211 ASSERT ((DBL_MIN_EXP
% 101111) == (DBL_MIN_EXP
) % 101111);
213 /* Check that the value of DBL_MIN_10_EXP is well parenthesized. */
214 ASSERT ((DBL_MIN_10_EXP
% 101111) == (DBL_MIN_10_EXP
) % 101111);
216 /* Check that 'double' is as specified in IEEE 754. */
217 ASSERT (DBL_MANT_DIG
== 53);
218 ASSERT (DBL_MIN_EXP
== -1021);
219 ASSERT (DBL_MAX_EXP
== 1024);
221 /* Check the value of DBL_MIN_10_EXP. */
222 ASSERT (DBL_MIN_10_EXP
== - (int) (- (DBL_MIN_EXP
- 1) * 0.30103));
224 /* Check the value of DBL_DIG. */
225 ASSERT (DBL_DIG
== (int) ((DBL_MANT_DIG
- 1) * 0.30103));
227 /* Check the value of DBL_MIN_10_EXP. */
228 ASSERT (DBL_MIN_10_EXP
== - (int) (- (DBL_MIN_EXP
- 1) * 0.30103));
230 /* Check the value of DBL_MAX_10_EXP. */
231 ASSERT (DBL_MAX_10_EXP
== (int) (DBL_MAX_EXP
* 0.30103));
233 /* Check the value of DBL_MAX. */
235 volatile double m
= DBL_MAX
;
239 for (n
= 0; n
<= 2 * DBL_MANT_DIG
; n
++)
241 volatile double pow2_n
= pow2d (n
); /* 2^n */
242 volatile double x
= m
+ (m
/ pow2_n
);
246 ASSERT (!(x
+ x
== x
));
250 /* Check the value of DBL_MIN. */
252 volatile double m
= DBL_MIN
;
253 volatile double x
= pow2d (DBL_MIN_EXP
- 1);
257 /* Check the value of DBL_EPSILON. */
259 volatile double e
= DBL_EPSILON
;
265 ASSERT (me
- 1.0 == e
);
266 for (n
= 0; n
<= 2 * DBL_MANT_DIG
; n
++)
268 volatile double half_n
= pow2d (- n
); /* 2^-n */
269 volatile double x
= me
- half_n
;
276 /* -------------------- Check macros for 'long double' -------------------- */
278 /* Check that the LDBL_* macros expand to constant expressions. */
281 LDBL_MANT_DIG
, LDBL_MIN_EXP
, LDBL_MAX_EXP
,
282 LDBL_DIG
, LDBL_MIN_10_EXP
, LDBL_MAX_10_EXP
284 long double lc1
= LDBL_EPSILON
;
285 long double lc2
= LDBL_MIN
;
286 #if 0 /* LDBL_MAX is not a constant expression on some platforms. */
287 long double lc3
= LDBL_MAX
;
291 test_long_double (void)
293 /* Check that the value of LDBL_MIN_EXP is well parenthesized. */
294 ASSERT ((LDBL_MIN_EXP
% 101111) == (LDBL_MIN_EXP
) % 101111);
296 /* Check that the value of LDBL_MIN_10_EXP is well parenthesized. */
297 ASSERT ((LDBL_MIN_10_EXP
% 101111) == (LDBL_MIN_10_EXP
) % 101111);
299 /* Check that 'long double' is at least as wide as 'double'. */
300 ASSERT (LDBL_MANT_DIG
>= DBL_MANT_DIG
);
301 ASSERT (LDBL_MIN_EXP
- LDBL_MANT_DIG
<= DBL_MIN_EXP
- DBL_MANT_DIG
);
302 ASSERT (LDBL_MAX_EXP
>= DBL_MAX_EXP
);
304 /* Check the value of LDBL_DIG. */
305 ASSERT (LDBL_DIG
== (int)((LDBL_MANT_DIG
- 1) * 0.30103));
307 /* Check the value of LDBL_MIN_10_EXP. */
308 ASSERT (LDBL_MIN_10_EXP
== - (int) (- (LDBL_MIN_EXP
- 1) * 0.30103));
310 /* Check the value of LDBL_MAX_10_EXP. */
311 ASSERT (LDBL_MAX_10_EXP
== (int) (LDBL_MAX_EXP
* 0.30103));
313 /* Check the value of LDBL_MAX. */
315 volatile long double m
= LDBL_MAX
;
319 for (n
= 0; n
<= 2 * LDBL_MANT_DIG
; n
++)
321 volatile long double pow2_n
= pow2l (n
); /* 2^n */
322 volatile long double x
= m
+ (m
/ pow2_n
);
326 ASSERT (!(x
+ x
== x
));
330 /* Check the value of LDBL_MIN. */
332 volatile long double m
= LDBL_MIN
;
333 volatile long double x
= pow2l (LDBL_MIN_EXP
- 1);
337 /* Check the value of LDBL_EPSILON. */
339 volatile long double e
= LDBL_EPSILON
;
340 volatile long double me
;
345 ASSERT (me
- 1.0L == e
);
346 for (n
= 0; n
<= 2 * LDBL_MANT_DIG
; n
++)
348 volatile long double half_n
= pow2l (- n
); /* 2^-n */
349 volatile long double x
= me
- half_n
;
363 DECL_LONG_DOUBLE_ROUNDING
365 BEGIN_LONG_DOUBLE_ROUNDING ();
369 END_LONG_DOUBLE_ROUNDING ();
380 fprintf (stderr
, "Skipping test: FLT_RADIX is not 2.\n");