2 * Copyright (c) 2008-2011 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 csin[h](), ccos[h](), and ctan[h]().
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
41 #define ALL_STD_EXCEPT (FE_DIVBYZERO | FE_INEXACT | FE_INVALID | \
42 FE_OVERFLOW | FE_UNDERFLOW)
43 #define OPT_INVALID (ALL_STD_EXCEPT & ~FE_INVALID)
44 #define OPT_INEXACT (ALL_STD_EXCEPT & ~FE_INEXACT)
45 #define FLT_ULP() ldexpl(1.0, 1 - FLT_MANT_DIG)
46 #define DBL_ULP() ldexpl(1.0, 1 - DBL_MANT_DIG)
47 #define LDBL_ULP() ldexpl(1.0, 1 - LDBL_MANT_DIG)
49 #pragma STDC FENV_ACCESS ON
50 #pragma STDC CX_LIMITED_RANGE OFF
53 * XXX gcc implements complex multiplication incorrectly. In
54 * particular, it implements it as if the CX_LIMITED_RANGE pragma
55 * were ON. Consequently, we need this function to form numbers
56 * such as x + INFINITY * I, since gcc evalutes INFINITY * I as
59 static inline long double complex
60 cpackl(long double x, long double y)
62 long double complex z;
69 /* Flags that determine whether to check the signs of the result. */
72 #define CS_BOTH (CS_REAL | CS_IMAG)
75 #define debug(...) printf(__VA_ARGS__)
77 #define debug(...) (void)0
81 * Test that a function returns the correct value and sets the
82 * exception flags correctly. The exceptmask specifies which
83 * exceptions we should check. We need to be lenient for several
84 * reasons, but mainly because on some architectures it's impossible
85 * to raise FE_OVERFLOW without raising FE_INEXACT.
87 * These are macros instead of functions so that assert provides more
88 * meaningful error messages.
90 * XXX The volatile here is to avoid gcc's bogus constant folding and work
91 * around the lack of support for the FENV_ACCESS pragma.
93 #define test_p(func, z, result, exceptmask, excepts, checksign) do { \
94 volatile long double complex _d = z; \
95 debug(" testing %s(%Lg + %Lg I) == %Lg + %Lg I\n", #func, \
96 creall(_d), cimagl(_d), creall(result), cimagl(result)); \
97 assert(feclearexcept(FE_ALL_EXCEPT) == 0); \
98 assert(cfpequal((func)(_d), (result), (checksign))); \
99 assert(((func), fetestexcept(exceptmask) == (excepts))); \
103 * Test within a given tolerance. The tolerance indicates relative error
104 * in ulps. If result is 0, however, it measures absolute error in units
105 * of <format>_EPSILON.
107 #define test_p_tol(func, z, result, tol) do { \
108 volatile long double complex _d = z; \
109 debug(" testing %s(%Lg + %Lg I) ~= %Lg + %Lg I\n", #func, \
110 creall(_d), cimagl(_d), creall(result), cimagl(result)); \
111 assert(cfpequal_tol((func)(_d), (result), (tol))); \
114 /* These wrappers apply the identities f(conj(z)) = conj(f(z)). */
115 #define test(func, z, result, exceptmask, excepts, checksign) do { \
116 test_p(func, z, result, exceptmask, excepts, checksign); \
117 test_p(func, conjl(z), conjl(result), exceptmask, excepts, checksign); \
119 #define test_tol(func, z, result, tol) do { \
120 test_p_tol(func, z, result, tol); \
121 test_p_tol(func, conjl(z), conjl(result), tol); \
124 /* Test the given function in all precisions. */
125 #define testall(func, x, result, exceptmask, excepts, checksign) do { \
126 test(func, x, result, exceptmask, excepts, checksign); \
127 test(func##f, x, result, exceptmask, excepts, checksign); \
129 #define testall_odd(func, x, result, exceptmask, excepts, checksign) do { \
130 testall(func, x, result, exceptmask, excepts, checksign); \
131 testall(func, -x, -result, exceptmask, excepts, checksign); \
133 #define testall_even(func, x, result, exceptmask, excepts, checksign) do { \
134 testall(func, x, result, exceptmask, excepts, checksign); \
135 testall(func, -x, result, exceptmask, excepts, checksign); \
139 * Test the given function in all precisions, within a given tolerance.
140 * The tolerance is specified in ulps.
142 #define testall_tol(func, x, result, tol) do { \
143 test_tol(func, x, result, tol * DBL_ULP()); \
144 test_tol(func##f, x, result, tol * FLT_ULP()); \
146 #define testall_odd_tol(func, x, result, tol) do { \
147 test_tol(func, x, result, tol * DBL_ULP()); \
148 test_tol(func, -x, -result, tol * DBL_ULP()); \
150 #define testall_even_tol(func, x, result, tol) do { \
151 test_tol(func, x, result, tol * DBL_ULP()); \
152 test_tol(func, -x, result, tol * DBL_ULP()); \
156 * Determine whether x and y are equal, with two special rules:
159 * If checksign is 0, we compare the absolute values instead.
162 fpequal(long double x, long double y, int checksign)
164 if (isnan(x) && isnan(y))
167 return (x == y && !signbit(x) == !signbit(y));
169 return (fabsl(x) == fabsl(y));
173 fpequal_tol(long double x, long double y, long double tol)
178 if (isnan(x) && isnan(y))
180 if (!signbit(x) != !signbit(y) && tol == 0)
187 /* Hard case: need to check the tolerance. */
190 * For our purposes here, if y=0, we interpret tol as an absolute
191 * tolerance. This is to account for roundoff in the input, e.g.,
195 ret = fabsl(x - y) <= fabsl(tol);
197 ret = fabsl(x - y) <= fabsl(y * tol);
203 cfpequal(long double complex x, long double complex y, int checksign)
205 return (fpequal(creal(x), creal(y), checksign & CS_REAL)
206 && fpequal(cimag(x), cimag(y), checksign & CS_IMAG));
210 cfpequal_tol(long double complex x, long double complex y, long double tol)
212 return (fpequal_tol(creal(x), creal(y), tol)
213 && fpequal_tol(cimag(x), cimag(y), tol));
221 long double complex zero = cpackl(0.0, 0.0);
223 /* csinh(0) = ctanh(0) = 0; ccosh(0) = 1 (no exceptions raised) */
224 testall_odd(csinh, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
225 testall_odd(csin, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
226 testall_even(ccosh, zero, 1.0, ALL_STD_EXCEPT, 0, CS_BOTH);
227 testall_even(ccos, zero, cpackl(1.0, -0.0), ALL_STD_EXCEPT, 0, CS_BOTH);
228 testall_odd(ctanh, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
229 testall_odd(ctan, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH);
233 * Tests for NaN inputs.
238 long double complex nan_nan = cpackl(NAN, NAN);
239 long double complex z;
242 * IN CSINH CCOSH CTANH
243 * NaN,NaN NaN,NaN NaN,NaN NaN,NaN
244 * finite,NaN NaN,NaN [inval] NaN,NaN [inval] NaN,NaN [inval]
245 * NaN,finite NaN,NaN [inval] NaN,NaN [inval] NaN,NaN [inval]
246 * NaN,Inf NaN,NaN [inval] NaN,NaN [inval] NaN,NaN [inval]
247 * Inf,NaN +-Inf,NaN Inf,NaN 1,+-0
248 * 0,NaN +-0,NaN NaN,+-0 NaN,NaN [inval]
249 * NaN,0 NaN,0 NaN,+-0 NaN,0
252 testall_odd(csinh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
253 testall_even(ccosh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
254 testall_odd(ctanh, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
255 testall_odd(csin, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
256 testall_even(ccos, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
257 testall_odd(ctan, z, nan_nan, ALL_STD_EXCEPT, 0, 0);
260 testall_odd(csinh, z, nan_nan, OPT_INVALID, 0, 0);
261 testall_even(ccosh, z, nan_nan, OPT_INVALID, 0, 0);
262 /* XXX We allow a spurious inexact exception here. */
263 testall_odd(ctanh, z, nan_nan, OPT_INVALID & ~FE_INEXACT, 0, 0);
264 testall_odd(csin, z, nan_nan, OPT_INVALID, 0, 0);
265 testall_even(ccos, z, nan_nan, OPT_INVALID, 0, 0);
266 testall_odd(ctan, z, nan_nan, OPT_INVALID, 0, 0);
269 testall_odd(csinh, z, nan_nan, OPT_INVALID, 0, 0);
270 testall_even(ccosh, z, nan_nan, OPT_INVALID, 0, 0);
271 testall_odd(ctanh, z, nan_nan, OPT_INVALID, 0, 0);
272 testall_odd(csin, z, nan_nan, OPT_INVALID, 0, 0);
273 testall_even(ccos, z, nan_nan, OPT_INVALID, 0, 0);
274 /* XXX We allow a spurious inexact exception here. */
275 testall_odd(ctan, z, nan_nan, OPT_INVALID & ~FE_INEXACT, 0, 0);
277 z = cpackl(NAN, INFINITY);
278 testall_odd(csinh, z, nan_nan, OPT_INVALID, 0, 0);
279 testall_even(ccosh, z, nan_nan, OPT_INVALID, 0, 0);
280 testall_odd(ctanh, z, nan_nan, OPT_INVALID, 0, 0);
281 testall_odd(csin, z, cpackl(NAN, INFINITY), ALL_STD_EXCEPT, 0, 0);
282 testall_even(ccos, z, cpackl(INFINITY, NAN), ALL_STD_EXCEPT, 0,
284 testall_odd(ctan, z, cpackl(0, 1), ALL_STD_EXCEPT, 0, CS_IMAG);
286 z = cpackl(INFINITY, NAN);
287 testall_odd(csinh, z, cpackl(INFINITY, NAN), ALL_STD_EXCEPT, 0, 0);
288 testall_even(ccosh, z, cpackl(INFINITY, NAN), ALL_STD_EXCEPT, 0,
290 testall_odd(ctanh, z, cpackl(1, 0), ALL_STD_EXCEPT, 0, CS_REAL);
291 testall_odd(csin, z, nan_nan, OPT_INVALID, 0, 0);
292 testall_even(ccos, z, nan_nan, OPT_INVALID, 0, 0);
293 testall_odd(ctan, z, nan_nan, OPT_INVALID, 0, 0);
296 testall_odd(csinh, z, cpackl(0, NAN), ALL_STD_EXCEPT, 0, 0);
297 testall_even(ccosh, z, cpackl(NAN, 0), ALL_STD_EXCEPT, 0, 0);
298 testall_odd(ctanh, z, nan_nan, OPT_INVALID, 0, 0);
299 testall_odd(csin, z, cpackl(0, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
300 testall_even(ccos, z, cpackl(NAN, 0), ALL_STD_EXCEPT, 0, 0);
301 testall_odd(ctan, z, cpackl(0, NAN), ALL_STD_EXCEPT, 0, CS_REAL);
304 testall_odd(csinh, z, cpackl(NAN, 0), ALL_STD_EXCEPT, 0, CS_IMAG);
305 testall_even(ccosh, z, cpackl(NAN, 0), ALL_STD_EXCEPT, 0, 0);
306 testall_odd(ctanh, z, cpackl(NAN, 0), ALL_STD_EXCEPT, 0, CS_IMAG);
307 testall_odd(csin, z, cpackl(NAN, 0), ALL_STD_EXCEPT, 0, 0);
308 testall_even(ccos, z, cpackl(NAN, 0), ALL_STD_EXCEPT, 0, 0);
309 testall_odd(ctan, z, nan_nan, OPT_INVALID, 0, 0);
315 static const long double finites[] = {
316 0, M_PI / 4, 3 * M_PI / 4, 5 * M_PI / 4,
318 long double complex z, c, s;
322 * IN CSINH CCOSH CTANH
323 * Inf,Inf +-Inf,NaN inval +-Inf,NaN inval 1,+-0
324 * Inf,finite Inf cis(finite) Inf cis(finite) 1,0 sin(2 finite)
325 * 0,Inf +-0,NaN inval NaN,+-0 inval NaN,NaN inval
326 * finite,Inf NaN,NaN inval NaN,NaN inval NaN,NaN inval
328 z = cpackl(INFINITY, INFINITY);
329 testall_odd(csinh, z, cpackl(INFINITY, NAN),
330 ALL_STD_EXCEPT, FE_INVALID, 0);
331 testall_even(ccosh, z, cpackl(INFINITY, NAN),
332 ALL_STD_EXCEPT, FE_INVALID, 0);
333 testall_odd(ctanh, z, cpackl(1, 0), ALL_STD_EXCEPT, 0, CS_REAL);
334 testall_odd(csin, z, cpackl(NAN, INFINITY),
335 ALL_STD_EXCEPT, FE_INVALID, 0);
336 testall_even(ccos, z, cpackl(INFINITY, NAN),
337 ALL_STD_EXCEPT, FE_INVALID, 0);
338 testall_odd(ctan, z, cpackl(0, 1), ALL_STD_EXCEPT, 0, CS_REAL);
340 /* XXX We allow spurious inexact exceptions here (hard to avoid). */
341 for (i = 0; i < sizeof(finites) / sizeof(finites[0]); i++) {
342 z = cpackl(INFINITY, finites[i]);
343 c = INFINITY * cosl(finites[i]);
344 s = finites[i] == 0 ? finites[i] : INFINITY * sinl(finites[i]);
345 testall_odd(csinh, z, cpackl(c, s), OPT_INEXACT, 0, CS_BOTH);
346 testall_even(ccosh, z, cpackl(c, s), OPT_INEXACT, 0, CS_BOTH);
347 testall_odd(ctanh, z, cpackl(1, 0 * sin(finites[i] * 2)),
348 OPT_INEXACT, 0, CS_BOTH);
349 z = cpackl(finites[i], INFINITY);
350 testall_odd(csin, z, cpackl(s, c), OPT_INEXACT, 0, CS_BOTH);
351 testall_even(ccos, z, cpackl(c, -s), OPT_INEXACT, 0, CS_BOTH);
352 testall_odd(ctan, z, cpackl(0 * sin(finites[i] * 2), 1),
353 OPT_INEXACT, 0, CS_BOTH);
356 z = cpackl(0, INFINITY);
357 testall_odd(csinh, z, cpackl(0, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
358 testall_even(ccosh, z, cpackl(NAN, 0), ALL_STD_EXCEPT, FE_INVALID, 0);
359 testall_odd(ctanh, z, cpackl(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
360 z = cpackl(INFINITY, 0);
361 testall_odd(csin, z, cpackl(NAN, 0), ALL_STD_EXCEPT, FE_INVALID, 0);
362 testall_even(ccos, z, cpackl(NAN, 0), ALL_STD_EXCEPT, FE_INVALID, 0);
363 testall_odd(ctan, z, cpackl(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
365 z = cpackl(42, INFINITY);
366 testall_odd(csinh, z, cpackl(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
367 testall_even(ccosh, z, cpackl(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
368 /* XXX We allow a spurious inexact exception here. */
369 testall_odd(ctanh, z, cpackl(NAN, NAN), OPT_INEXACT, FE_INVALID, 0);
370 z = cpackl(INFINITY, 42);
371 testall_odd(csin, z, cpackl(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
372 testall_even(ccos, z, cpackl(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0);
373 /* XXX We allow a spurious inexact exception here. */
374 testall_odd(ctan, z, cpackl(NAN, NAN), OPT_INEXACT, FE_INVALID, 0);
377 /* Tests along the real and imaginary axes. */
381 static const long double nums[] = {
382 M_PI / 4, M_PI / 2, 3 * M_PI / 4,
383 5 * M_PI / 4, 3 * M_PI / 2, 7 * M_PI / 4,
385 long double complex z;
388 for (i = 0; i < sizeof(nums) / sizeof(nums[0]); i++) {
390 z = cpackl(nums[i], 0.0);
391 testall_odd_tol(csinh, z, cpackl(sinh(nums[i]), 0), 0);
392 testall_even_tol(ccosh, z, cpackl(cosh(nums[i]), 0), 0);
393 testall_odd_tol(ctanh, z, cpackl(tanh(nums[i]), 0), 1);
394 testall_odd_tol(csin, z, cpackl(sin(nums[i]),
395 copysign(0, cos(nums[i]))), 0);
396 testall_even_tol(ccos, z, cpackl(cos(nums[i]),
397 -copysign(0, sin(nums[i]))), 0);
398 testall_odd_tol(ctan, z, cpackl(tan(nums[i]), 0), 1);
401 z = cpackl(0.0, nums[i]);
402 testall_odd_tol(csinh, z, cpackl(copysign(0, cos(nums[i])),
404 testall_even_tol(ccosh, z, cpackl(cos(nums[i]),
405 copysign(0, sin(nums[i]))), 0);
406 testall_odd_tol(ctanh, z, cpackl(0, tan(nums[i])), 1);
407 testall_odd_tol(csin, z, cpackl(0, sinh(nums[i])), 0);
408 testall_even_tol(ccos, z, cpackl(cosh(nums[i]), -0.0), 0);
409 testall_odd_tol(ctan, z, cpackl(0, tanh(nums[i])), 1);
418 * sinh(z) = (sinh(0.5) + i cosh(0.5)) * sqrt(2)/2
419 * cosh(z) = (cosh(0.5) + i sinh(0.5)) * sqrt(2)/2
420 * tanh(z) = (2cosh(0.5)sinh(0.5) + i) / (2 cosh(0.5)**2 - 1)
422 * sinh(z) = cosh(0.5)
423 * cosh(z) = -i sinh(0.5)
424 * tanh(z) = -coth(0.5)
426 * sinh(z) = (-sinh(1) + i cosh(1)) * sqrt(2)/2
427 * cosh(z) = (-cosh(1) + i sinh(1)) * sqrt(2)/2
428 * tanh(z) = (2cosh(1)sinh(1) - i) / (2cosh(1)**2 - 1)
430 static const struct {
432 long double sinh_a, sinh_b;
433 long double cosh_a, cosh_b;
434 long double tanh_a, tanh_b;
437 0.78539816339744830961566084581987572L,
438 0.36847002415910435172083660522240710L,
439 0.79735196663945774996093142586179334L,
440 0.79735196663945774996093142586179334L,
441 0.36847002415910435172083660522240710L,
442 0.76159415595576488811945828260479359L,
443 0.64805427366388539957497735322615032L },
445 1.57079632679489661923132169163975144L,
447 1.12762596520638078522622516140267201L,
449 -0.52109530549374736162242562641149156L,
450 -2.16395341373865284877000401021802312L,
453 2.35619449019234492884698253745962716L,
454 -0.83099273328405698212637979852748608L,
455 1.09112278079550143030545602018565236L,
456 -1.09112278079550143030545602018565236L,
457 0.83099273328405698212637979852748609L,
458 0.96402758007581688394641372410092315L,
459 -0.26580222883407969212086273981988897L }
461 long double complex z;
464 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
465 z = cpackl(tests[i].a, tests[i].b);
466 testall_odd_tol(csinh, z,
467 cpackl(tests[i].sinh_a, tests[i].sinh_b), 1.1);
468 testall_even_tol(ccosh, z,
469 cpackl(tests[i].cosh_a, tests[i].cosh_b), 1.1);
470 testall_odd_tol(ctanh, z,
471 cpackl(tests[i].tanh_a, tests[i].tanh_b), 1.1);
475 /* Test inputs that might cause overflow in a sloppy implementation. */
479 long double complex z;
481 /* tanh() uses a threshold around x=22, so check both sides. */
482 z = cpackl(21, 0.78539816339744830961566084581987572L);
483 testall_odd_tol(ctanh, z,
484 cpackl(1.0, 1.14990445285871196133287617611468468e-18L), 1);
486 testall_odd_tol(ctanh, z,
487 cpackl(1.0, 1.55622644822675930314266334585597964e-19L), 1);
489 z = cpackl(355, 0.78539816339744830961566084581987572L);
490 testall_odd_tol(ctanh, z,
491 cpackl(1.0, 8.95257245135025991216632140458264468e-309L), 1);
492 z = cpackl(30, 0x1p1023L);
493 testall_odd_tol(ctanh, z,
494 cpackl(1.0, -1.62994325413993477997492170229268382e-26L), 1);
495 z = cpackl(1, 0x1p1023L);
496 testall_odd_tol(ctanh, z,
497 cpackl(0.878606311888306869546254022621986509L,
498 -0.225462792499754505792678258169527424L), 1);
500 z = cpackl(710.6, 0.78539816339744830961566084581987572L);
501 testall_odd_tol(csinh, z,
502 cpackl(1.43917579766621073533185387499658944e308L,
503 1.43917579766621073533185387499658944e308L), 1);
504 testall_even_tol(ccosh, z,
505 cpackl(1.43917579766621073533185387499658944e308L,
506 1.43917579766621073533185387499658944e308L), 1);
508 z = cpackl(1500, 0.78539816339744830961566084581987572L);
509 testall_odd(csinh, z, cpackl(INFINITY, INFINITY), OPT_INEXACT,
510 FE_OVERFLOW, CS_BOTH);
511 testall_even(ccosh, z, cpackl(INFINITY, INFINITY), OPT_INEXACT,
512 FE_OVERFLOW, CS_BOTH);
516 main(int argc, char *argv[])
522 printf("ok 1 - ctrig zero\n");
525 printf("ok 2 - ctrig nan\n");
528 printf("ok 3 - ctrig inf\n");
531 printf("ok 4 - ctrig axes\n");
534 printf("ok 5 - ctrig small\n");
537 printf("ok 6 - ctrig large\n");