Mercurial > hg > octave-lojdl > gnulib-hg
annotate tests/test-frexp.c @ 9850:7f3ed6b093be
Guarantee a definition of NAN.
* lib/math.in.h (NAN): Define if missing.
* tests/test-math.c (main): Test it.
* doc/posix-headers/math.texi (math.h): Document this.
* lib/isnan.c (rpl_isnand): Use it.
* tests/test-ceilf1.c (NaN): Delete, and use NAN instead.
* tests/test-floorf1.c (NaN): Likewise.
* tests/test-frexp.c (NaN): Likewise.
* tests/test-isnand.c (NaN): Likewise.
* tests/test-isnanf.c (NaN): Likewise.
* tests/test-round1.c (NaN): Likewise.
* tests/test-roundf1.c (NaN): Likewise.
* tests/test-snprintf-posix.h (NaN): Likewise.
* tests/test-sprintf-posix.h (NaN): Likewise.
* tests/test-trunc1.c (NaN): Likewise.
* tests/test-truncf1.c (NaN): Likewise.
* tests/test-vasnprintf-posix.c (NaN): Likewise.
* tests/test-vasprintf-posix.c (NaN): Likewise.
* modules/isnand-nolibm (Depends-on): Add math.
* modules/isnanf-nolibm (Depends-on): Likewise.
* modules/isnanl (Depends-on): Likewise.
* modules/isnanl-nolibm (Depends-on): Likewise.
* modules/snprintf-posix-tests (Depends-on): Likewise.
* modules/sprintf-posix-tests (Depends-on): Likewise.
* modules/vsnprintf-posix-tests (Depends-on): Likewise.
* modules/vsprintf-posix-tests (Depends-on): Likewise.
* modules/vasnprintf-posix-tests (Depends-on): Likewise.
* modules/vasprintf-posix-tests (Depends-on): Likewise.
Signed-off-by: Eric Blake <ebb9@byu.net>
author | Eric Blake <ebb9@byu.net> |
---|---|
date | Mon, 31 Mar 2008 20:56:25 -0600 |
parents | a113e473cc98 |
children | baba3b346ab2 |
rev | line source |
---|---|
8492 | 1 /* Test of splitting a double into fraction and mantissa. |
9646
a113e473cc98
Rename isnan, applicable to 'double' only, to isnand.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
2 Copyright (C) 2007-2008 Free Software Foundation, Inc. |
8492 | 3 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8754
diff
changeset
|
4 This program is free software: you can redistribute it and/or modify |
8492 | 5 it under the terms of the GNU General Public License as published by |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8754
diff
changeset
|
6 the Free Software Foundation; either version 3 of the License, or |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8754
diff
changeset
|
7 (at your option) any later version. |
8492 | 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 | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8754
diff
changeset
|
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
8492 | 16 |
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */ | |
18 | |
19 #include <config.h> | |
20 | |
21 #include <math.h> | |
22 | |
23 #include <float.h> | |
8754 | 24 #include <stdio.h> |
8492 | 25 #include <stdlib.h> |
26 | |
9646
a113e473cc98
Rename isnan, applicable to 'double' only, to isnand.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
27 #include "isnand.h" |
8545
418f381347dc
Work around IRIX 6.5 cc compiler bug, which simplifies x != x to false.
Bruno Haible <bruno@clisp.org>
parents:
8543
diff
changeset
|
28 |
8754 | 29 #define ASSERT(expr) \ |
30 do \ | |
31 { \ | |
32 if (!(expr)) \ | |
33 { \ | |
34 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ | |
35 abort (); \ | |
36 } \ | |
37 } \ | |
38 while (0) | |
8492 | 39 |
40 static double | |
41 my_ldexp (double x, int d) | |
42 { | |
43 for (; d > 0; d--) | |
44 x *= 2.0; | |
45 for (; d < 0; d++) | |
46 x *= 0.5; | |
47 return x; | |
48 } | |
49 | |
50 int | |
51 main () | |
52 { | |
53 int i; | |
54 /* The use of 'volatile' guarantees that excess precision bits are dropped | |
55 when dealing with denormalized numbers. It is necessary on x86 systems | |
56 where double-floats are not IEEE compliant by default, to avoid that the | |
57 results become platform and compiler option dependent. 'volatile' is a | |
58 portable alternative to gcc's -ffloat-store option. */ | |
59 volatile double x; | |
60 | |
61 { /* NaN. */ | |
62 int exp = -9999; | |
63 double mantissa; | |
9850 | 64 x = NAN; |
8492 | 65 mantissa = frexp (x, &exp); |
9646
a113e473cc98
Rename isnan, applicable to 'double' only, to isnand.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
66 ASSERT (isnand (mantissa)); |
8492 | 67 } |
68 | |
69 { /* Positive infinity. */ | |
70 int exp = -9999; | |
71 double mantissa; | |
72 x = 1.0 / 0.0; | |
73 mantissa = frexp (x, &exp); | |
74 ASSERT (mantissa == x); | |
75 } | |
76 | |
77 { /* Negative infinity. */ | |
78 int exp = -9999; | |
79 double mantissa; | |
80 x = -1.0 / 0.0; | |
81 mantissa = frexp (x, &exp); | |
82 ASSERT (mantissa == x); | |
83 } | |
84 | |
85 { /* Positive zero. */ | |
86 int exp = -9999; | |
87 double mantissa; | |
88 x = 0.0; | |
89 mantissa = frexp (x, &exp); | |
90 ASSERT (exp == 0); | |
91 ASSERT (mantissa == x); | |
8654
a2bc90a7b7aa
Test also the sign bit of zero results.
Bruno Haible <bruno@clisp.org>
parents:
8545
diff
changeset
|
92 ASSERT (!signbit (mantissa)); |
8492 | 93 } |
94 | |
95 { /* Negative zero. */ | |
96 int exp = -9999; | |
97 double mantissa; | |
98 x = -0.0; | |
99 mantissa = frexp (x, &exp); | |
100 ASSERT (exp == 0); | |
101 ASSERT (mantissa == x); | |
8654
a2bc90a7b7aa
Test also the sign bit of zero results.
Bruno Haible <bruno@clisp.org>
parents:
8545
diff
changeset
|
102 ASSERT (signbit (mantissa)); |
8492 | 103 } |
104 | |
105 for (i = 1, x = 1.0; i <= DBL_MAX_EXP; i++, x *= 2.0) | |
106 { | |
107 int exp = -9999; | |
108 double mantissa = frexp (x, &exp); | |
109 ASSERT (exp == i); | |
110 ASSERT (mantissa == 0.5); | |
111 } | |
112 for (i = 1, x = 1.0; i >= DBL_MIN_EXP; i--, x *= 0.5) | |
113 { | |
114 int exp = -9999; | |
115 double mantissa = frexp (x, &exp); | |
116 ASSERT (exp == i); | |
117 ASSERT (mantissa == 0.5); | |
118 } | |
119 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5) | |
120 { | |
121 int exp = -9999; | |
122 double mantissa = frexp (x, &exp); | |
123 ASSERT (exp == i); | |
124 ASSERT (mantissa == 0.5); | |
125 } | |
126 | |
127 for (i = 1, x = -1.0; i <= DBL_MAX_EXP; i++, x *= 2.0) | |
128 { | |
129 int exp = -9999; | |
130 double mantissa = frexp (x, &exp); | |
131 ASSERT (exp == i); | |
132 ASSERT (mantissa == -0.5); | |
133 } | |
134 for (i = 1, x = -1.0; i >= DBL_MIN_EXP; i--, x *= 0.5) | |
135 { | |
136 int exp = -9999; | |
137 double mantissa = frexp (x, &exp); | |
138 ASSERT (exp == i); | |
139 ASSERT (mantissa == -0.5); | |
140 } | |
141 for (; i >= DBL_MIN_EXP - 100 && x < 0.0; i--, x *= 0.5) | |
142 { | |
143 int exp = -9999; | |
144 double mantissa = frexp (x, &exp); | |
145 ASSERT (exp == i); | |
146 ASSERT (mantissa == -0.5); | |
147 } | |
148 | |
149 for (i = 1, x = 1.01; i <= DBL_MAX_EXP; i++, x *= 2.0) | |
150 { | |
151 int exp = -9999; | |
152 double mantissa = frexp (x, &exp); | |
153 ASSERT (exp == i); | |
154 ASSERT (mantissa == 0.505); | |
155 } | |
156 for (i = 1, x = 1.01; i >= DBL_MIN_EXP; i--, x *= 0.5) | |
157 { | |
158 int exp = -9999; | |
159 double mantissa = frexp (x, &exp); | |
160 ASSERT (exp == i); | |
161 ASSERT (mantissa == 0.505); | |
162 } | |
163 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5) | |
164 { | |
165 int exp = -9999; | |
166 double mantissa = frexp (x, &exp); | |
167 ASSERT (exp == i); | |
168 ASSERT (mantissa >= 0.5); | |
169 ASSERT (mantissa < 1.0); | |
170 ASSERT (mantissa == my_ldexp (x, - exp)); | |
171 } | |
172 | |
173 for (i = 1, x = 1.73205; i <= DBL_MAX_EXP; i++, x *= 2.0) | |
174 { | |
175 int exp = -9999; | |
176 double mantissa = frexp (x, &exp); | |
177 ASSERT (exp == i); | |
178 ASSERT (mantissa == 0.866025); | |
179 } | |
180 for (i = 1, x = 1.73205; i >= DBL_MIN_EXP; i--, x *= 0.5) | |
181 { | |
182 int exp = -9999; | |
183 double mantissa = frexp (x, &exp); | |
184 ASSERT (exp == i); | |
185 ASSERT (mantissa == 0.866025); | |
186 } | |
187 for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5) | |
188 { | |
189 int exp = -9999; | |
190 double mantissa = frexp (x, &exp); | |
191 ASSERT (exp == i || exp == i + 1); | |
192 ASSERT (mantissa >= 0.5); | |
193 ASSERT (mantissa < 1.0); | |
194 ASSERT (mantissa == my_ldexp (x, - exp)); | |
195 } | |
196 | |
197 return 0; | |
198 } |