2 * Copyright (c) 2008 David Schultz <das@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * Tests for corner cases in the inverse trigonometric functions. Some
29 * accuracy tests are included as well, but these are very basic
30 * sanity checks, not intended to be comprehensive.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
42 #include "test-utils.h"
44 #define LEN(a) (sizeof(a) / sizeof((a)[0]))
46 #pragma STDC FENV_ACCESS ON
49 * Test that a function returns the correct value and sets the
50 * exception flags correctly. A tolerance specifying the maximum
51 * relative error allowed may be specified. For the 'testall'
52 * functions, the tolerance is specified in ulps.
54 * These are macros instead of functions so that assert provides more
55 * meaningful error messages.
57 #define test_tol(func, x, result, tol, excepts) do { \
58 volatile long double _in = (x), _out = (result); \
59 assert(feclearexcept(FE_ALL_EXCEPT) == 0); \
60 assert(fpequal_tol(func(_in), _out, (tol), CS_BOTH)); \
61 assert(((void)func, fetestexcept(ALL_STD_EXCEPT) == (excepts))); \
63 #define test(func, x, result, excepts) \
64 test_tol(func, (x), (result), 0, (excepts))
66 #define testall_tol(prefix, x, result, tol, excepts) do { \
67 test_tol(prefix, (double)(x), (double)(result), \
68 (tol) * ldexp(1.0, 1 - DBL_MANT_DIG), (excepts)); \
69 test_tol(prefix##f, (float)(x), (float)(result), \
70 (tol) * ldexpf(1.0, 1 - FLT_MANT_DIG), (excepts)); \
71 test_tol(prefix##l, (x), (result), \
72 (tol) * ldexpl(1.0, 1 - LDBL_MANT_DIG), (excepts)); \
74 #define testall(prefix, x, result, excepts) \
75 testall_tol(prefix, (x), (result), 0, (excepts))
77 #define test2_tol(func, y, x, result, tol, excepts) do { \
78 volatile long double _iny = (y), _inx = (x), _out = (result); \
79 assert(feclearexcept(FE_ALL_EXCEPT) == 0); \
80 assert(fpequal_tol(func(_iny, _inx), _out, (tol), CS_BOTH)); \
81 assert(((void)func, fetestexcept(ALL_STD_EXCEPT) == (excepts))); \
83 #define test2(func, y, x, result, excepts) \
84 test2_tol(func, (y), (x), (result), 0, (excepts))
86 #define testall2_tol(prefix, y, x, result, tol, excepts) do { \
87 test2_tol(prefix, (double)(y), (double)(x), (double)(result), \
88 (tol) * ldexp(1.0, 1 - DBL_MANT_DIG), (excepts)); \
89 test2_tol(prefix##f, (float)(y), (float)(x), (float)(result), \
90 (tol) * ldexpf(1.0, 1 - FLT_MANT_DIG), (excepts)); \
91 test2_tol(prefix##l, (y), (x), (result), \
92 (tol) * ldexpl(1.0, 1 - LDBL_MANT_DIG), (excepts)); \
94 #define testall2(prefix, y, x, result, excepts) \
95 testall2_tol(prefix, (y), (x), (result), 0, (excepts))
98 pi = 3.14159265358979323846264338327950280e+00L,
99 pio3 = 1.04719755119659774615421446109316766e+00L,
100 c3pi = 9.42477796076937971538793014983850839e+00L,
101 c5pi = 1.57079632679489661923132169163975140e+01L,
102 c7pi = 2.19911485751285526692385036829565196e+01L,
103 c5pio3 = 5.23598775598298873077107230546583851e+00L,
104 sqrt2m1 = 4.14213562373095048801688724209698081e-01L;
108 * Test special case inputs in asin(), acos() and atan(): signed
109 * zeroes, infinities, and NaNs.
115 testall(asin, 0.0, 0.0, 0);
116 testall(acos, 0.0, pi / 2, FE_INEXACT);
117 testall(atan, 0.0, 0.0, 0);
118 testall(asin, -0.0, -0.0, 0);
119 testall(acos, -0.0, pi / 2, FE_INEXACT);
120 testall(atan, -0.0, -0.0, 0);
122 testall(asin, INFINITY, NAN, FE_INVALID);
123 testall(acos, INFINITY, NAN, FE_INVALID);
124 testall(atan, INFINITY, pi / 2, FE_INEXACT);
125 testall(asin, -INFINITY, NAN, FE_INVALID);
126 testall(acos, -INFINITY, NAN, FE_INVALID);
127 testall(atan, -INFINITY, -pi / 2, FE_INEXACT);
129 testall(asin, NAN, NAN, 0);
130 testall(acos, NAN, NAN, 0);
131 testall(atan, NAN, NAN, 0);
135 * Test special case inputs in atan2(), where the exact value of y/x is
136 * zero or non-finite.
139 test_special_atan2(void)
144 testall2(atan2, 0.0, -0.0, pi, FE_INEXACT);
145 testall2(atan2, -0.0, -0.0, -pi, FE_INEXACT);
146 testall2(atan2, 0.0, 0.0, 0.0, 0);
147 testall2(atan2, -0.0, 0.0, -0.0, 0);
149 testall2(atan2, INFINITY, -INFINITY, c3pi / 4, FE_INEXACT);
150 testall2(atan2, -INFINITY, -INFINITY, -c3pi / 4, FE_INEXACT);
151 testall2(atan2, INFINITY, INFINITY, pi / 4, FE_INEXACT);
152 testall2(atan2, -INFINITY, INFINITY, -pi / 4, FE_INEXACT);
154 /* Tests with one input in the range (0, Inf]. */
156 for (e = FLT_MIN_EXP - FLT_MANT_DIG; e <= FLT_MAX_EXP; e++) {
157 test2(atan2f, 0.0, ldexpf(z, e), 0.0, 0);
158 test2(atan2f, -0.0, ldexpf(z, e), -0.0, 0);
159 test2(atan2f, 0.0, ldexpf(-z, e), (float)pi, FE_INEXACT);
160 test2(atan2f, -0.0, ldexpf(-z, e), (float)-pi, FE_INEXACT);
161 test2(atan2f, ldexpf(z, e), 0.0, (float)pi / 2, FE_INEXACT);
162 test2(atan2f, ldexpf(z, e), -0.0, (float)pi / 2, FE_INEXACT);
163 test2(atan2f, ldexpf(-z, e), 0.0, (float)-pi / 2, FE_INEXACT);
164 test2(atan2f, ldexpf(-z, e), -0.0, (float)-pi / 2, FE_INEXACT);
166 for (e = DBL_MIN_EXP - DBL_MANT_DIG; e <= DBL_MAX_EXP; e++) {
167 test2(atan2, 0.0, ldexp(z, e), 0.0, 0);
168 test2(atan2, -0.0, ldexp(z, e), -0.0, 0);
169 test2(atan2, 0.0, ldexp(-z, e), (double)pi, FE_INEXACT);
170 test2(atan2, -0.0, ldexp(-z, e), (double)-pi, FE_INEXACT);
171 test2(atan2, ldexp(z, e), 0.0, (double)pi / 2, FE_INEXACT);
172 test2(atan2, ldexp(z, e), -0.0, (double)pi / 2, FE_INEXACT);
173 test2(atan2, ldexp(-z, e), 0.0, (double)-pi / 2, FE_INEXACT);
174 test2(atan2, ldexp(-z, e), -0.0, (double)-pi / 2, FE_INEXACT);
176 for (e = LDBL_MIN_EXP - LDBL_MANT_DIG; e <= LDBL_MAX_EXP; e++) {
177 test2(atan2l, 0.0, ldexpl(z, e), 0.0, 0);
178 test2(atan2l, -0.0, ldexpl(z, e), -0.0, 0);
179 test2(atan2l, 0.0, ldexpl(-z, e), pi, FE_INEXACT);
180 test2(atan2l, -0.0, ldexpl(-z, e), -pi, FE_INEXACT);
181 test2(atan2l, ldexpl(z, e), 0.0, pi / 2, FE_INEXACT);
182 test2(atan2l, ldexpl(z, e), -0.0, pi / 2, FE_INEXACT);
183 test2(atan2l, ldexpl(-z, e), 0.0, -pi / 2, FE_INEXACT);
184 test2(atan2l, ldexpl(-z, e), -0.0, -pi / 2, FE_INEXACT);
187 /* Tests with one input in the range (0, Inf). */
188 for (e = FLT_MIN_EXP - FLT_MANT_DIG; e <= FLT_MAX_EXP - 1; e++) {
189 test2(atan2f, ldexpf(z, e), INFINITY, 0.0, 0);
190 test2(atan2f, ldexpf(-z,e), INFINITY, -0.0, 0);
191 test2(atan2f, ldexpf(z, e), -INFINITY, (float)pi, FE_INEXACT);
192 test2(atan2f, ldexpf(-z,e), -INFINITY, (float)-pi, FE_INEXACT);
193 test2(atan2f, INFINITY, ldexpf(z,e), (float)pi/2, FE_INEXACT);
194 test2(atan2f, INFINITY, ldexpf(-z,e), (float)pi/2, FE_INEXACT);
195 test2(atan2f, -INFINITY, ldexpf(z,e), (float)-pi/2,FE_INEXACT);
196 test2(atan2f, -INFINITY, ldexpf(-z,e),(float)-pi/2,FE_INEXACT);
198 for (e = DBL_MIN_EXP - DBL_MANT_DIG; e <= DBL_MAX_EXP - 1; e++) {
199 test2(atan2, ldexp(z, e), INFINITY, 0.0, 0);
200 test2(atan2, ldexp(-z,e), INFINITY, -0.0, 0);
201 test2(atan2, ldexp(z, e), -INFINITY, (double)pi, FE_INEXACT);
202 test2(atan2, ldexp(-z,e), -INFINITY, (double)-pi, FE_INEXACT);
203 test2(atan2, INFINITY, ldexp(z,e), (double)pi/2, FE_INEXACT);
204 test2(atan2, INFINITY, ldexp(-z,e), (double)pi/2, FE_INEXACT);
205 test2(atan2, -INFINITY, ldexp(z,e), (double)-pi/2,FE_INEXACT);
206 test2(atan2, -INFINITY, ldexp(-z,e),(double)-pi/2,FE_INEXACT);
208 for (e = LDBL_MIN_EXP - LDBL_MANT_DIG; e <= LDBL_MAX_EXP - 1; e++) {
209 test2(atan2l, ldexpl(z, e), INFINITY, 0.0, 0);
210 test2(atan2l, ldexpl(-z,e), INFINITY, -0.0, 0);
211 test2(atan2l, ldexpl(z, e), -INFINITY, pi, FE_INEXACT);
212 test2(atan2l, ldexpl(-z,e), -INFINITY, -pi, FE_INEXACT);
213 test2(atan2l, INFINITY, ldexpl(z, e), pi / 2, FE_INEXACT);
214 test2(atan2l, INFINITY, ldexpl(-z, e), pi / 2, FE_INEXACT);
215 test2(atan2l, -INFINITY, ldexpl(z, e), -pi / 2, FE_INEXACT);
216 test2(atan2l, -INFINITY, ldexpl(-z, e), -pi / 2, FE_INEXACT);
221 * Test various inputs to asin(), acos() and atan() and verify that the
222 * results are accurate to within 1 ulp.
228 /* We expect correctly rounded results for these basic cases. */
229 testall(asin, 1.0, pi / 2, FE_INEXACT);
230 testall(acos, 1.0, 0, 0);
231 testall(atan, 1.0, pi / 4, FE_INEXACT);
232 testall(asin, -1.0, -pi / 2, FE_INEXACT);
233 testall(acos, -1.0, pi, FE_INEXACT);
234 testall(atan, -1.0, -pi / 4, FE_INEXACT);
237 * Here we expect answers to be within 1 ulp, although inexactness
238 * in the input, combined with double rounding, could cause larger
242 testall_tol(asin, sqrtl(2) / 2, pi / 4, 1, FE_INEXACT);
243 testall_tol(acos, sqrtl(2) / 2, pi / 4, 1, FE_INEXACT);
244 testall_tol(asin, -sqrtl(2) / 2, -pi / 4, 1, FE_INEXACT);
245 testall_tol(acos, -sqrtl(2) / 2, c3pi / 4, 1, FE_INEXACT);
247 testall_tol(asin, sqrtl(3) / 2, pio3, 1, FE_INEXACT);
248 testall_tol(acos, sqrtl(3) / 2, pio3 / 2, 1, FE_INEXACT);
249 testall_tol(atan, sqrtl(3), pio3, 1, FE_INEXACT);
250 testall_tol(asin, -sqrtl(3) / 2, -pio3, 1, FE_INEXACT);
251 testall_tol(acos, -sqrtl(3) / 2, c5pio3 / 2, 1, FE_INEXACT);
252 testall_tol(atan, -sqrtl(3), -pio3, 1, FE_INEXACT);
254 testall_tol(atan, sqrt2m1, pi / 8, 1, FE_INEXACT);
255 testall_tol(atan, -sqrt2m1, -pi / 8, 1, FE_INEXACT);
259 * Test inputs to atan2() where x is a power of 2. These are easy cases
260 * because y/x is exact.
266 testall2(atan2, 1.0, 1.0, pi / 4, FE_INEXACT);
267 testall2(atan2, 1.0, -1.0, c3pi / 4, FE_INEXACT);
268 testall2(atan2, -1.0, 1.0, -pi / 4, FE_INEXACT);
269 testall2(atan2, -1.0, -1.0, -c3pi / 4, FE_INEXACT);
271 testall2_tol(atan2, sqrt2m1 * 2, 2.0, pi / 8, 1, FE_INEXACT);
272 testall2_tol(atan2, sqrt2m1 * 2, -2.0, c7pi / 8, 1, FE_INEXACT);
273 testall2_tol(atan2, -sqrt2m1 * 2, 2.0, -pi / 8, 1, FE_INEXACT);
274 testall2_tol(atan2, -sqrt2m1 * 2, -2.0, -c7pi / 8, 1, FE_INEXACT);
276 testall2_tol(atan2, sqrtl(3) * 0.5, 0.5, pio3, 1, FE_INEXACT);
277 testall2_tol(atan2, sqrtl(3) * 0.5, -0.5, pio3 * 2, 1, FE_INEXACT);
278 testall2_tol(atan2, -sqrtl(3) * 0.5, 0.5, -pio3, 1, FE_INEXACT);
279 testall2_tol(atan2, -sqrtl(3) * 0.5, -0.5, -pio3 * 2, 1, FE_INEXACT);
283 * Test inputs very close to 0.
288 float tiny = 0x1.23456p-120f;
290 testall(asin, tiny, tiny, FE_INEXACT);
291 testall(acos, tiny, pi / 2, FE_INEXACT);
292 testall(atan, tiny, tiny, FE_INEXACT);
294 testall(asin, -tiny, -tiny, FE_INEXACT);
295 testall(acos, -tiny, pi / 2, FE_INEXACT);
296 testall(atan, -tiny, -tiny, FE_INEXACT);
298 /* Test inputs to atan2() that would cause y/x to underflow. */
299 test2(atan2f, 0x1.0p-100, 0x1.0p100, 0.0, FE_INEXACT | FE_UNDERFLOW);
300 test2(atan2, 0x1.0p-1000, 0x1.0p1000, 0.0, FE_INEXACT | FE_UNDERFLOW);
301 test2(atan2l, ldexpl(1.0, 100 - LDBL_MAX_EXP),
302 ldexpl(1.0, LDBL_MAX_EXP - 100), 0.0, FE_INEXACT | FE_UNDERFLOW);
303 test2(atan2f, -0x1.0p-100, 0x1.0p100, -0.0, FE_INEXACT | FE_UNDERFLOW);
304 test2(atan2, -0x1.0p-1000, 0x1.0p1000, -0.0, FE_INEXACT | FE_UNDERFLOW);
305 test2(atan2l, -ldexpl(1.0, 100 - LDBL_MAX_EXP),
306 ldexpl(1.0, LDBL_MAX_EXP - 100), -0.0, FE_INEXACT | FE_UNDERFLOW);
307 test2(atan2f, 0x1.0p-100, -0x1.0p100, (float)pi, FE_INEXACT);
308 test2(atan2, 0x1.0p-1000, -0x1.0p1000, (double)pi, FE_INEXACT);
309 test2(atan2l, ldexpl(1.0, 100 - LDBL_MAX_EXP),
310 -ldexpl(1.0, LDBL_MAX_EXP - 100), pi, FE_INEXACT);
311 test2(atan2f, -0x1.0p-100, -0x1.0p100, (float)-pi, FE_INEXACT);
312 test2(atan2, -0x1.0p-1000, -0x1.0p1000, (double)-pi, FE_INEXACT);
313 test2(atan2l, -ldexpl(1.0, 100 - LDBL_MAX_EXP),
314 -ldexpl(1.0, LDBL_MAX_EXP - 100), -pi, FE_INEXACT);
318 * Test very large inputs to atan().
323 float huge = 0x1.23456p120;
325 testall(atan, huge, pi / 2, FE_INEXACT);
326 testall(atan, -huge, -pi / 2, FE_INEXACT);
328 /* Test inputs to atan2() that would cause y/x to overflow. */
329 test2(atan2f, 0x1.0p100, 0x1.0p-100, (float)pi / 2, FE_INEXACT);
330 test2(atan2, 0x1.0p1000, 0x1.0p-1000, (double)pi / 2, FE_INEXACT);
331 test2(atan2l, ldexpl(1.0, LDBL_MAX_EXP - 100),
332 ldexpl(1.0, 100 - LDBL_MAX_EXP), pi / 2, FE_INEXACT);
333 test2(atan2f, -0x1.0p100, 0x1.0p-100, (float)-pi / 2, FE_INEXACT);
334 test2(atan2, -0x1.0p1000, 0x1.0p-1000, (double)-pi / 2, FE_INEXACT);
335 test2(atan2l, -ldexpl(1.0, LDBL_MAX_EXP - 100),
336 ldexpl(1.0, 100 - LDBL_MAX_EXP), -pi / 2, FE_INEXACT);
338 test2(atan2f, 0x1.0p100, -0x1.0p-100, (float)pi / 2, FE_INEXACT);
339 test2(atan2, 0x1.0p1000, -0x1.0p-1000, (double)pi / 2, FE_INEXACT);
340 test2(atan2l, ldexpl(1.0, LDBL_MAX_EXP - 100),
341 -ldexpl(1.0, 100 - LDBL_MAX_EXP), pi / 2, FE_INEXACT);
342 test2(atan2f, -0x1.0p100, -0x1.0p-100, (float)-pi / 2, FE_INEXACT);
343 test2(atan2, -0x1.0p1000, -0x1.0p-1000, (double)-pi / 2, FE_INEXACT);
344 test2(atan2l, -ldexpl(1.0, LDBL_MAX_EXP - 100),
345 -ldexpl(1.0, 100 - LDBL_MAX_EXP), -pi / 2, FE_INEXACT);
349 * Test that sin(asin(x)) == x, and similarly for acos() and atan().
350 * You need to have a working sinl(), cosl(), and tanl() for these
357 return (sinl(asinf(x)));
364 return (sinl(asin(x)));
368 sinasinl(long double x)
371 return (sinl(asinl(x)));
378 return (cosl(acosf(x)));
385 return (cosl(acos(x)));
389 cosacosl(long double x)
392 return (cosl(acosl(x)));
399 return (tanl(atanf(x)));
406 return (tanl(atan(x)));
410 tanatanl(long double x)
413 return (tanl(atanl(x)));
421 for (i = -1; i <= 1; i += 0x1.0p-12f) {
422 testall_tol(sinasin, i, i, 2, i == 0 ? 0 : FE_INEXACT);
423 /* The relative error for cosacos is very large near x=0. */
424 if (fabsf(i) > 0x1.0p-4f)
425 testall_tol(cosacos, i, i, 16, i == 1 ? 0 : FE_INEXACT);
426 testall_tol(tanatan, i, i, 2, i == 0 ? 0 : FE_INEXACT);
431 main(int argc, char *argv[])
437 printf("ok 1 - special\n");
439 test_special_atan2();
440 printf("ok 2 - atan2 special\n");
443 printf("ok 3 - accuracy\n");
446 printf("ok 4 - atan2 p2x\n");
449 printf("ok 5 - tiny inputs\n");
452 printf("ok 6 - atan huge inputs\n");
455 printf("ok 7 - inverse\n");