annotate tests/test-float.c @ 40231:9b3c79fdfe0b

strtod: fix clash with strtold Problem reported for RHEL 5 by Jesse Caldwell (Bug#34817). * lib/strtod.c (compute_minus_zero, minus_zero): Simplify by remving the macro / external variable, and having just a function. User changed. This avoids the need for an external variable that might clash.
author Paul Eggert <eggert@cs.ucla.edu>
date Mon, 11 Mar 2019 16:40:29 -0700
parents b06060465f09
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15300
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Test of <float.h> substitute.
40057
b06060465f09 maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents: 19484
diff changeset
2 Copyright (C) 2011-2019 Free Software Foundation, Inc.
15300
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4 This program is free software: you can redistribute it and/or modify
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 (at your option) any later version.
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
19190
9759915b2aca all: prefer https: URLs
Paul Eggert <eggert@cs.ucla.edu>
parents: 18626
diff changeset
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
15300
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 /* Written by Bruno Haible <bruno@clisp.org>, 2011. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 #include <config.h>
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 #include <float.h>
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 #include "fpucw.h"
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 #include "macros.h"
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 /* Check that FLT_RADIX is a constant expression. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 int a[] = { FLT_RADIX };
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 #if FLT_RADIX == 2
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 /* Return 2^n. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 static float
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 pow2f (int n)
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 {
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 int k = n;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 volatile float x = 1;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 volatile float y = 2;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 /* Invariant: 2^n == x * y^k. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 if (k < 0)
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 {
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 y = 0.5f;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 k = - k;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 }
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 while (k > 0)
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 {
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 if (k != 2 * (k / 2))
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 {
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 x = x * y;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 k = k - 1;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 }
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 if (k == 0)
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 break;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 y = y * y;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 k = k / 2;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 }
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 /* Now k == 0, hence x == 2^n. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 return x;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 }
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 /* Return 2^n. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 static double
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 pow2d (int n)
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 {
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 int k = n;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 volatile double x = 1;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 volatile double y = 2;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 /* Invariant: 2^n == x * y^k. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 if (k < 0)
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 {
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 y = 0.5;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 k = - k;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 }
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 while (k > 0)
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 {
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 if (k != 2 * (k / 2))
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 {
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 x = x * y;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 k = k - 1;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 }
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 if (k == 0)
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 break;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 y = y * y;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 k = k / 2;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 }
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 /* Now k == 0, hence x == 2^n. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 return x;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 }
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 /* Return 2^n. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 static long double
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 pow2l (int n)
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92 {
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 int k = n;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 volatile long double x = 1;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 volatile long double y = 2;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 /* Invariant: 2^n == x * y^k. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 if (k < 0)
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 {
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99 y = 0.5L;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 k = - k;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101 }
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102 while (k > 0)
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103 {
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 if (k != 2 * (k / 2))
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 {
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106 x = x * y;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 k = k - 1;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108 }
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109 if (k == 0)
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110 break;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
111 y = y * y;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112 k = k / 2;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113 }
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114 /* Now k == 0, hence x == 2^n. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115 return x;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
116 }
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
117
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
118 /* ----------------------- Check macros for 'float' ----------------------- */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
119
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
120 /* Check that the FLT_* macros expand to constant expressions. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
121 int fb[] =
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
122 {
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
123 FLT_MANT_DIG, FLT_MIN_EXP, FLT_MAX_EXP,
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
124 FLT_DIG, FLT_MIN_10_EXP, FLT_MAX_10_EXP
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
125 };
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
126 float fc[] = { FLT_EPSILON, FLT_MIN, FLT_MAX };
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
127
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
128 static void
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
129 test_float (void)
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
130 {
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
131 /* Check that the value of FLT_MIN_EXP is well parenthesized. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
132 ASSERT ((FLT_MIN_EXP % 101111) == (FLT_MIN_EXP) % 101111);
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
133
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
134 /* Check that the value of DBL_MIN_10_EXP is well parenthesized. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
135 ASSERT ((FLT_MIN_10_EXP % 101111) == (FLT_MIN_10_EXP) % 101111);
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
136
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
137 /* Check that 'float' is as specified in IEEE 754. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
138 ASSERT (FLT_MANT_DIG == 24);
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
139 ASSERT (FLT_MIN_EXP == -125);
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
140 ASSERT (FLT_MAX_EXP == 128);
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
141
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
142 /* Check the value of FLT_MIN_10_EXP. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
143 ASSERT (FLT_MIN_10_EXP == - (int) (- (FLT_MIN_EXP - 1) * 0.30103));
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
144
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
145 /* Check the value of FLT_DIG. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
146 ASSERT (FLT_DIG == (int) ((FLT_MANT_DIG - 1) * 0.30103));
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
147
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
148 /* Check the value of FLT_MIN_10_EXP. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
149 ASSERT (FLT_MIN_10_EXP == - (int) (- (FLT_MIN_EXP - 1) * 0.30103));
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
150
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
151 /* Check the value of FLT_MAX_10_EXP. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
152 ASSERT (FLT_MAX_10_EXP == (int) (FLT_MAX_EXP * 0.30103));
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
153
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
154 /* Check the value of FLT_MAX. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
155 {
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
156 volatile float m = FLT_MAX;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
157 int n;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
158
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
159 ASSERT (m + m > m);
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
160 for (n = 0; n <= 2 * FLT_MANT_DIG; n++)
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
161 {
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
162 volatile float pow2_n = pow2f (n); /* 2^n */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
163 volatile float x = m + (m / pow2_n);
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
164 if (x > m)
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
165 ASSERT (x + x == x);
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
166 else
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
167 ASSERT (!(x + x == x));
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
168 }
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
169 }
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
170
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
171 /* Check the value of FLT_MIN. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
172 {
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
173 volatile float m = FLT_MIN;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
174 volatile float x = pow2f (FLT_MIN_EXP - 1);
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
175 ASSERT (m == x);
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
176 }
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
177
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
178 /* Check the value of FLT_EPSILON. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
179 {
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
180 volatile float e = FLT_EPSILON;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
181 volatile float me;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
182 int n;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
183
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
184 me = 1.0f + e;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
185 ASSERT (me > 1.0f);
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
186 ASSERT (me - 1.0f == e);
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
187 for (n = 0; n <= 2 * FLT_MANT_DIG; n++)
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
188 {
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
189 volatile float half_n = pow2f (- n); /* 2^-n */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
190 volatile float x = me - half_n;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
191 if (x < me)
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
192 ASSERT (x <= 1.0f);
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
193 }
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
194 }
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
195 }
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
196
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
197 /* ----------------------- Check macros for 'double' ----------------------- */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
198
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
199 /* Check that the DBL_* macros expand to constant expressions. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
200 int db[] =
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
201 {
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
202 DBL_MANT_DIG, DBL_MIN_EXP, DBL_MAX_EXP,
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
203 DBL_DIG, DBL_MIN_10_EXP, DBL_MAX_10_EXP
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
204 };
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
205 double dc[] = { DBL_EPSILON, DBL_MIN, DBL_MAX };
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
206
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
207 static void
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
208 test_double (void)
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
209 {
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
210 /* Check that the value of DBL_MIN_EXP is well parenthesized. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
211 ASSERT ((DBL_MIN_EXP % 101111) == (DBL_MIN_EXP) % 101111);
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
212
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
213 /* Check that the value of DBL_MIN_10_EXP is well parenthesized. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
214 ASSERT ((DBL_MIN_10_EXP % 101111) == (DBL_MIN_10_EXP) % 101111);
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
215
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
216 /* Check that 'double' is as specified in IEEE 754. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
217 ASSERT (DBL_MANT_DIG == 53);
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
218 ASSERT (DBL_MIN_EXP == -1021);
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
219 ASSERT (DBL_MAX_EXP == 1024);
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
220
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
221 /* Check the value of DBL_MIN_10_EXP. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
222 ASSERT (DBL_MIN_10_EXP == - (int) (- (DBL_MIN_EXP - 1) * 0.30103));
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
223
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
224 /* Check the value of DBL_DIG. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
225 ASSERT (DBL_DIG == (int) ((DBL_MANT_DIG - 1) * 0.30103));
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
226
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
227 /* Check the value of DBL_MIN_10_EXP. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
228 ASSERT (DBL_MIN_10_EXP == - (int) (- (DBL_MIN_EXP - 1) * 0.30103));
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
229
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
230 /* Check the value of DBL_MAX_10_EXP. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
231 ASSERT (DBL_MAX_10_EXP == (int) (DBL_MAX_EXP * 0.30103));
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
232
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
233 /* Check the value of DBL_MAX. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
234 {
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
235 volatile double m = DBL_MAX;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
236 int n;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
237
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
238 ASSERT (m + m > m);
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
239 for (n = 0; n <= 2 * DBL_MANT_DIG; n++)
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
240 {
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
241 volatile double pow2_n = pow2d (n); /* 2^n */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
242 volatile double x = m + (m / pow2_n);
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
243 if (x > m)
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
244 ASSERT (x + x == x);
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
245 else
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
246 ASSERT (!(x + x == x));
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
247 }
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
248 }
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
249
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
250 /* Check the value of DBL_MIN. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
251 {
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
252 volatile double m = DBL_MIN;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
253 volatile double x = pow2d (DBL_MIN_EXP - 1);
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
254 ASSERT (m == x);
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
255 }
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
256
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
257 /* Check the value of DBL_EPSILON. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
258 {
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
259 volatile double e = DBL_EPSILON;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
260 volatile double me;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
261 int n;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
262
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
263 me = 1.0 + e;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
264 ASSERT (me > 1.0);
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
265 ASSERT (me - 1.0 == e);
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
266 for (n = 0; n <= 2 * DBL_MANT_DIG; n++)
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
267 {
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
268 volatile double half_n = pow2d (- n); /* 2^-n */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
269 volatile double x = me - half_n;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
270 if (x < me)
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
271 ASSERT (x <= 1.0);
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
272 }
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
273 }
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
274 }
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
275
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
276 /* -------------------- Check macros for 'long double' -------------------- */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
277
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
278 /* Check that the LDBL_* macros expand to constant expressions. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
279 int lb[] =
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
280 {
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
281 LDBL_MANT_DIG, LDBL_MIN_EXP, LDBL_MAX_EXP,
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
282 LDBL_DIG, LDBL_MIN_10_EXP, LDBL_MAX_10_EXP
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
283 };
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
284 long double lc1 = LDBL_EPSILON;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
285 long double lc2 = LDBL_MIN;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
286 #if 0 /* LDBL_MAX is not a constant expression on some platforms. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
287 long double lc3 = LDBL_MAX;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
288 #endif
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
289
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
290 static void
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
291 test_long_double (void)
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
292 {
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
293 /* Check that the value of LDBL_MIN_EXP is well parenthesized. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
294 ASSERT ((LDBL_MIN_EXP % 101111) == (LDBL_MIN_EXP) % 101111);
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
295
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
296 /* Check that the value of LDBL_MIN_10_EXP is well parenthesized. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
297 ASSERT ((LDBL_MIN_10_EXP % 101111) == (LDBL_MIN_10_EXP) % 101111);
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
298
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
299 /* Check that 'long double' is at least as wide as 'double'. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
300 ASSERT (LDBL_MANT_DIG >= DBL_MANT_DIG);
16136
fd0796dd37e2 float tests: Correct and re-enable assertion about LDBL_MIN_EXP.
Paolo Bonzini <bonzini@gnu.org>
parents: 15545
diff changeset
301 ASSERT (LDBL_MIN_EXP - LDBL_MANT_DIG <= DBL_MIN_EXP - DBL_MANT_DIG);
15300
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
302 ASSERT (LDBL_MAX_EXP >= DBL_MAX_EXP);
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
303
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
304 /* Check the value of LDBL_DIG. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
305 ASSERT (LDBL_DIG == (int)((LDBL_MANT_DIG - 1) * 0.30103));
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
306
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
307 /* Check the value of LDBL_MIN_10_EXP. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
308 ASSERT (LDBL_MIN_10_EXP == - (int) (- (LDBL_MIN_EXP - 1) * 0.30103));
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
309
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
310 /* Check the value of LDBL_MAX_10_EXP. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
311 ASSERT (LDBL_MAX_10_EXP == (int) (LDBL_MAX_EXP * 0.30103));
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
312
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
313 /* Check the value of LDBL_MAX. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
314 {
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
315 volatile long double m = LDBL_MAX;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
316 int n;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
317
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
318 ASSERT (m + m > m);
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
319 for (n = 0; n <= 2 * LDBL_MANT_DIG; n++)
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
320 {
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
321 volatile long double pow2_n = pow2l (n); /* 2^n */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
322 volatile long double x = m + (m / pow2_n);
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
323 if (x > m)
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
324 ASSERT (x + x == x);
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
325 else
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
326 ASSERT (!(x + x == x));
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
327 }
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
328 }
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
329
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
330 /* Check the value of LDBL_MIN. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
331 {
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
332 volatile long double m = LDBL_MIN;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
333 volatile long double x = pow2l (LDBL_MIN_EXP - 1);
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
334 ASSERT (m == x);
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
335 }
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
336
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
337 /* Check the value of LDBL_EPSILON. */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
338 {
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
339 volatile long double e = LDBL_EPSILON;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
340 volatile long double me;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
341 int n;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
342
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
343 me = 1.0L + e;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
344 ASSERT (me > 1.0L);
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
345 ASSERT (me - 1.0L == e);
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
346 for (n = 0; n <= 2 * LDBL_MANT_DIG; n++)
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
347 {
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
348 volatile long double half_n = pow2l (- n); /* 2^-n */
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
349 volatile long double x = me - half_n;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
350 if (x < me)
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
351 ASSERT (x <= 1.0L);
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
352 }
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
353 }
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
354 }
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
355
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
356 int
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
357 main ()
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
358 {
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
359 test_float ();
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
360 test_double ();
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
361
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
362 {
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
363 DECL_LONG_DOUBLE_ROUNDING
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
364
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
365 BEGIN_LONG_DOUBLE_ROUNDING ();
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
366
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
367 test_long_double ();
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
368
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
369 END_LONG_DOUBLE_ROUNDING ();
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
370 }
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
371
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
372 return 0;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
373 }
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
374
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
375 #else
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
376
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
377 int
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
378 main ()
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
379 {
15361
cb88770da803 float tests: Tweak.
Bruno Haible <bruno@clisp.org>
parents: 15300
diff changeset
380 fprintf (stderr, "Skipping test: FLT_RADIX is not 2.\n");
15300
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
381 return 77;
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
382 }
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
383
64ce149fbae3 Tests for module 'float'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
384 #endif