1 /* Test of <float.h> substitute.
2 Copyright (C) 2011-2024 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 <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2011. */
23 /* Check that FLT_RADIX is a constant expression. */
24 int a
[] = { FLT_RADIX
};
26 /* ----------------------- Check macros for 'float' ----------------------- */
28 /* Check that the FLT_* macros expand to constant expressions. */
31 FLT_MANT_DIG
, FLT_MIN_EXP
, FLT_MAX_EXP
,
32 FLT_DIG
, FLT_MIN_10_EXP
, FLT_MAX_10_EXP
34 float fc
[] = { FLT_EPSILON
, FLT_MIN
, FLT_MAX
};
36 /* ----------------------- Check macros for 'double' ----------------------- */
38 /* Check that the DBL_* macros expand to constant expressions. */
41 DBL_MANT_DIG
, DBL_MIN_EXP
, DBL_MAX_EXP
,
42 DBL_DIG
, DBL_MIN_10_EXP
, DBL_MAX_10_EXP
44 double dc
[] = { DBL_EPSILON
, DBL_MIN
, DBL_MAX
};
46 /* -------------------- Check macros for 'long double' -------------------- */
48 /* Check that the LDBL_* macros expand to constant expressions. */
51 LDBL_MANT_DIG
, LDBL_MIN_EXP
, LDBL_MAX_EXP
,
52 LDBL_DIG
, LDBL_MIN_10_EXP
, LDBL_MAX_10_EXP
54 long double lc1
= LDBL_EPSILON
;
55 long double lc2
= LDBL_MIN
;
56 #if 0 /* LDBL_MAX is not a constant expression on some platforms. */
57 long double lc3
= LDBL_MAX
;
60 /* ------------------------------------------------------------------------- */
74 /* Invariant: 2^n == x * y^k. */
92 /* Now k == 0, hence x == 2^n. */
101 volatile double x
= 1;
102 volatile double y
= 2;
103 /* Invariant: 2^n == x * y^k. */
111 if (k
!= 2 * (k
/ 2))
121 /* Now k == 0, hence x == 2^n. */
130 volatile long double x
= 1;
131 volatile long double y
= 2;
132 /* Invariant: 2^n == x * y^k. */
140 if (k
!= 2 * (k
/ 2))
150 /* Now k == 0, hence x == 2^n. */
154 /* ----------------------- Check macros for 'float' ----------------------- */
159 /* Check that the value of FLT_MIN_EXP is well parenthesized. */
160 ASSERT ((FLT_MIN_EXP
% 101111) == (FLT_MIN_EXP
) % 101111);
162 /* Check that the value of DBL_MIN_10_EXP is well parenthesized. */
163 ASSERT ((FLT_MIN_10_EXP
% 101111) == (FLT_MIN_10_EXP
) % 101111);
165 /* Check that 'float' is as specified in IEEE 754. */
166 ASSERT (FLT_MANT_DIG
== 24);
167 ASSERT (FLT_MIN_EXP
== -125);
168 ASSERT (FLT_MAX_EXP
== 128);
170 /* Check the value of FLT_MIN_10_EXP. */
171 ASSERT (FLT_MIN_10_EXP
== - (int) (- (FLT_MIN_EXP
- 1) * 0.30103));
173 /* Check the value of FLT_DIG. */
174 ASSERT (FLT_DIG
== (int) ((FLT_MANT_DIG
- 1) * 0.30103));
176 /* Check the value of FLT_MIN_10_EXP. */
177 ASSERT (FLT_MIN_10_EXP
== - (int) (- (FLT_MIN_EXP
- 1) * 0.30103));
179 /* Check the value of FLT_MAX_10_EXP. */
180 ASSERT (FLT_MAX_10_EXP
== (int) (FLT_MAX_EXP
* 0.30103));
182 /* Check the value of FLT_MAX. */
184 volatile float m
= FLT_MAX
;
188 for (n
= 0; n
<= 2 * FLT_MANT_DIG
; n
++)
190 volatile float pow2_n
= pow2f (n
); /* 2^n */
191 volatile float x
= m
+ (m
/ pow2_n
);
195 ASSERT (!(x
+ x
== x
));
199 /* Check the value of FLT_MIN. */
201 volatile float m
= FLT_MIN
;
202 volatile float x
= pow2f (FLT_MIN_EXP
- 1);
206 /* Check the value of FLT_EPSILON. */
208 volatile float e
= FLT_EPSILON
;
214 ASSERT (me
- 1.0f
== e
);
215 for (n
= 0; n
<= 2 * FLT_MANT_DIG
; n
++)
217 volatile float half_n
= pow2f (- n
); /* 2^-n */
218 volatile float x
= me
- half_n
;
225 /* ----------------------- Check macros for 'double' ----------------------- */
230 /* Check that the value of DBL_MIN_EXP is well parenthesized. */
231 ASSERT ((DBL_MIN_EXP
% 101111) == (DBL_MIN_EXP
) % 101111);
233 /* Check that the value of DBL_MIN_10_EXP is well parenthesized. */
234 ASSERT ((DBL_MIN_10_EXP
% 101111) == (DBL_MIN_10_EXP
) % 101111);
236 /* Check that 'double' is as specified in IEEE 754. */
237 ASSERT (DBL_MANT_DIG
== 53);
238 ASSERT (DBL_MIN_EXP
== -1021);
239 ASSERT (DBL_MAX_EXP
== 1024);
241 /* Check the value of DBL_MIN_10_EXP. */
242 ASSERT (DBL_MIN_10_EXP
== - (int) (- (DBL_MIN_EXP
- 1) * 0.30103));
244 /* Check the value of DBL_DIG. */
245 ASSERT (DBL_DIG
== (int) ((DBL_MANT_DIG
- 1) * 0.30103));
247 /* Check the value of DBL_MIN_10_EXP. */
248 ASSERT (DBL_MIN_10_EXP
== - (int) (- (DBL_MIN_EXP
- 1) * 0.30103));
250 /* Check the value of DBL_MAX_10_EXP. */
251 ASSERT (DBL_MAX_10_EXP
== (int) (DBL_MAX_EXP
* 0.30103));
253 /* Check the value of DBL_MAX. */
255 volatile double m
= DBL_MAX
;
259 for (n
= 0; n
<= 2 * DBL_MANT_DIG
; n
++)
261 volatile double pow2_n
= pow2d (n
); /* 2^n */
262 volatile double x
= m
+ (m
/ pow2_n
);
266 ASSERT (!(x
+ x
== x
));
270 /* Check the value of DBL_MIN. */
272 volatile double m
= DBL_MIN
;
273 volatile double x
= pow2d (DBL_MIN_EXP
- 1);
277 /* Check the value of DBL_EPSILON. */
279 volatile double e
= DBL_EPSILON
;
285 ASSERT (me
- 1.0 == e
);
286 for (n
= 0; n
<= 2 * DBL_MANT_DIG
; n
++)
288 volatile double half_n
= pow2d (- n
); /* 2^-n */
289 volatile double x
= me
- half_n
;
296 /* -------------------- Check macros for 'long double' -------------------- */
299 test_long_double (void)
301 /* Check that the value of LDBL_MIN_EXP is well parenthesized. */
302 ASSERT ((LDBL_MIN_EXP
% 101111) == (LDBL_MIN_EXP
) % 101111);
304 /* Check that the value of LDBL_MIN_10_EXP is well parenthesized. */
305 ASSERT ((LDBL_MIN_10_EXP
% 101111) == (LDBL_MIN_10_EXP
) % 101111);
307 /* Check that 'long double' is at least as wide as 'double'. */
308 ASSERT (LDBL_MANT_DIG
>= DBL_MANT_DIG
);
309 ASSERT (LDBL_MIN_EXP
- LDBL_MANT_DIG
<= DBL_MIN_EXP
- DBL_MANT_DIG
);
310 ASSERT (LDBL_MAX_EXP
>= DBL_MAX_EXP
);
312 /* Check the value of LDBL_DIG. */
313 ASSERT (LDBL_DIG
== (int)((LDBL_MANT_DIG
- 1) * 0.30103));
315 /* Check the value of LDBL_MIN_10_EXP. */
316 ASSERT (LDBL_MIN_10_EXP
== - (int) (- (LDBL_MIN_EXP
- 1) * 0.30103));
318 /* Check the value of LDBL_MAX_10_EXP. */
319 ASSERT (LDBL_MAX_10_EXP
== (int) (LDBL_MAX_EXP
* 0.30103));
321 /* Check the value of LDBL_MAX. */
323 volatile long double m
= LDBL_MAX
;
327 for (n
= 0; n
<= 2 * LDBL_MANT_DIG
; n
++)
329 volatile long double pow2_n
= pow2l (n
); /* 2^n */
330 volatile long double x
= m
+ (m
/ pow2_n
);
334 ASSERT (!(x
+ x
== x
));
338 /* Check the value of LDBL_MIN. */
340 volatile long double m
= LDBL_MIN
;
341 volatile long double x
= pow2l (LDBL_MIN_EXP
- 1);
345 /* Check the value of LDBL_EPSILON. */
347 volatile long double e
= LDBL_EPSILON
;
348 volatile long double me
;
353 ASSERT (me
- 1.0L == e
);
354 for (n
= 0; n
<= 2 * LDBL_MANT_DIG
; n
++)
356 volatile long double half_n
= pow2l (- n
); /* 2^-n */
357 volatile long double x
= me
- half_n
;
371 DECL_LONG_DOUBLE_ROUNDING
373 BEGIN_LONG_DOUBLE_ROUNDING ();
377 END_LONG_DOUBLE_ROUNDING ();
380 return test_exit_status
;
388 fprintf (stderr
, "Skipping test: FLT_RADIX is not 2.\n");