1*8a7d0e8cSEnji Cooper /*- 2*8a7d0e8cSEnji Cooper * Copyright (c) 2008-2011 David Schultz <das@FreeBSD.org> 3*8a7d0e8cSEnji Cooper * All rights reserved. 4*8a7d0e8cSEnji Cooper * 5*8a7d0e8cSEnji Cooper * Redistribution and use in source and binary forms, with or without 6*8a7d0e8cSEnji Cooper * modification, are permitted provided that the following conditions 7*8a7d0e8cSEnji Cooper * are met: 8*8a7d0e8cSEnji Cooper * 1. Redistributions of source code must retain the above copyright 9*8a7d0e8cSEnji Cooper * notice, this list of conditions and the following disclaimer. 10*8a7d0e8cSEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright 11*8a7d0e8cSEnji Cooper * notice, this list of conditions and the following disclaimer in the 12*8a7d0e8cSEnji Cooper * documentation and/or other materials provided with the distribution. 13*8a7d0e8cSEnji Cooper * 14*8a7d0e8cSEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15*8a7d0e8cSEnji Cooper * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16*8a7d0e8cSEnji Cooper * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17*8a7d0e8cSEnji Cooper * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18*8a7d0e8cSEnji Cooper * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19*8a7d0e8cSEnji Cooper * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20*8a7d0e8cSEnji Cooper * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21*8a7d0e8cSEnji Cooper * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22*8a7d0e8cSEnji Cooper * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23*8a7d0e8cSEnji Cooper * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24*8a7d0e8cSEnji Cooper * SUCH DAMAGE. 25*8a7d0e8cSEnji Cooper */ 26*8a7d0e8cSEnji Cooper 27*8a7d0e8cSEnji Cooper /* 28*8a7d0e8cSEnji Cooper * Tests for csin[h](), ccos[h](), and ctan[h](). 29*8a7d0e8cSEnji Cooper */ 30*8a7d0e8cSEnji Cooper 31*8a7d0e8cSEnji Cooper #include <sys/cdefs.h> 32*8a7d0e8cSEnji Cooper __FBSDID("$FreeBSD$"); 33*8a7d0e8cSEnji Cooper 34*8a7d0e8cSEnji Cooper #include <assert.h> 35*8a7d0e8cSEnji Cooper #include <complex.h> 36*8a7d0e8cSEnji Cooper #include <fenv.h> 37*8a7d0e8cSEnji Cooper #include <float.h> 38*8a7d0e8cSEnji Cooper #include <math.h> 39*8a7d0e8cSEnji Cooper #include <stdio.h> 40*8a7d0e8cSEnji Cooper 41*8a7d0e8cSEnji Cooper #include "test-utils.h" 42*8a7d0e8cSEnji Cooper 43*8a7d0e8cSEnji Cooper #pragma STDC FENV_ACCESS ON 44*8a7d0e8cSEnji Cooper #pragma STDC CX_LIMITED_RANGE OFF 45*8a7d0e8cSEnji Cooper 46*8a7d0e8cSEnji Cooper /* 47*8a7d0e8cSEnji Cooper * Test that a function returns the correct value and sets the 48*8a7d0e8cSEnji Cooper * exception flags correctly. The exceptmask specifies which 49*8a7d0e8cSEnji Cooper * exceptions we should check. We need to be lenient for several 50*8a7d0e8cSEnji Cooper * reasons, but mainly because on some architectures it's impossible 51*8a7d0e8cSEnji Cooper * to raise FE_OVERFLOW without raising FE_INEXACT. 52*8a7d0e8cSEnji Cooper * 53*8a7d0e8cSEnji Cooper * These are macros instead of functions so that assert provides more 54*8a7d0e8cSEnji Cooper * meaningful error messages. 55*8a7d0e8cSEnji Cooper * 56*8a7d0e8cSEnji Cooper * XXX The volatile here is to avoid gcc's bogus constant folding and work 57*8a7d0e8cSEnji Cooper * around the lack of support for the FENV_ACCESS pragma. 58*8a7d0e8cSEnji Cooper */ 59*8a7d0e8cSEnji Cooper #define test_p(func, z, result, exceptmask, excepts, checksign) do { \ 60*8a7d0e8cSEnji Cooper volatile long double complex _d = z; \ 61*8a7d0e8cSEnji Cooper debug(" testing %s(%Lg + %Lg I) == %Lg + %Lg I\n", #func, \ 62*8a7d0e8cSEnji Cooper creall(_d), cimagl(_d), creall(result), cimagl(result)); \ 63*8a7d0e8cSEnji Cooper assert(feclearexcept(FE_ALL_EXCEPT) == 0); \ 64*8a7d0e8cSEnji Cooper assert(cfpequal_cs((func)(_d), (result), (checksign))); \ 65*8a7d0e8cSEnji Cooper assert(((void)(func), fetestexcept(exceptmask) == (excepts))); \ 66*8a7d0e8cSEnji Cooper } while (0) 67*8a7d0e8cSEnji Cooper 68*8a7d0e8cSEnji Cooper /* 69*8a7d0e8cSEnji Cooper * Test within a given tolerance. The tolerance indicates relative error 70*8a7d0e8cSEnji Cooper * in ulps. If result is 0, however, it measures absolute error in units 71*8a7d0e8cSEnji Cooper * of <format>_EPSILON. 72*8a7d0e8cSEnji Cooper */ 73*8a7d0e8cSEnji Cooper #define test_p_tol(func, z, result, tol) do { \ 74*8a7d0e8cSEnji Cooper volatile long double complex _d = z; \ 75*8a7d0e8cSEnji Cooper debug(" testing %s(%Lg + %Lg I) ~= %Lg + %Lg I\n", #func, \ 76*8a7d0e8cSEnji Cooper creall(_d), cimagl(_d), creall(result), cimagl(result)); \ 77*8a7d0e8cSEnji Cooper assert(cfpequal_tol((func)(_d), (result), (tol), FPE_ABS_ZERO)); \ 78*8a7d0e8cSEnji Cooper } while (0) 79*8a7d0e8cSEnji Cooper 80*8a7d0e8cSEnji Cooper /* These wrappers apply the identities f(conj(z)) = conj(f(z)). */ 81*8a7d0e8cSEnji Cooper #define test(func, z, result, exceptmask, excepts, checksign) do { \ 82*8a7d0e8cSEnji Cooper test_p(func, z, result, exceptmask, excepts, checksign); \ 83*8a7d0e8cSEnji Cooper test_p(func, conjl(z), conjl(result), exceptmask, excepts, checksign); \ 84*8a7d0e8cSEnji Cooper } while (0) 85*8a7d0e8cSEnji Cooper #define test_tol(func, z, result, tol) do { \ 86*8a7d0e8cSEnji Cooper test_p_tol(func, z, result, tol); \ 87*8a7d0e8cSEnji Cooper test_p_tol(func, conjl(z), conjl(result), tol); \ 88*8a7d0e8cSEnji Cooper } while (0) 89*8a7d0e8cSEnji Cooper #define test_odd_tol(func, z, result, tol) do { \ 90*8a7d0e8cSEnji Cooper test_tol(func, z, result, tol); \ 91*8a7d0e8cSEnji Cooper test_tol(func, -(z), -(result), tol); \ 92*8a7d0e8cSEnji Cooper } while (0) 93*8a7d0e8cSEnji Cooper #define test_even_tol(func, z, result, tol) do { \ 94*8a7d0e8cSEnji Cooper test_tol(func, z, result, tol); \ 95*8a7d0e8cSEnji Cooper test_tol(func, -(z), result, tol); \ 96*8a7d0e8cSEnji Cooper } while (0) 97*8a7d0e8cSEnji Cooper 98*8a7d0e8cSEnji Cooper /* Test the given function in all precisions. */ 99*8a7d0e8cSEnji Cooper #define testall(func, x, result, exceptmask, excepts, checksign) do { \ 100*8a7d0e8cSEnji Cooper test(func, x, result, exceptmask, excepts, checksign); \ 101*8a7d0e8cSEnji Cooper test(func##f, x, result, exceptmask, excepts, checksign); \ 102*8a7d0e8cSEnji Cooper } while (0) 103*8a7d0e8cSEnji Cooper #define testall_odd(func, x, result, exceptmask, excepts, checksign) do { \ 104*8a7d0e8cSEnji Cooper testall(func, x, result, exceptmask, excepts, checksign); \ 105*8a7d0e8cSEnji Cooper testall(func, -x, -result, exceptmask, excepts, checksign); \ 106*8a7d0e8cSEnji Cooper } while (0) 107*8a7d0e8cSEnji Cooper #define testall_even(func, x, result, exceptmask, excepts, checksign) do { \ 108*8a7d0e8cSEnji Cooper testall(func, x, result, exceptmask, excepts, checksign); \ 109*8a7d0e8cSEnji Cooper testall(func, -x, result, exceptmask, excepts, checksign); \ 110*8a7d0e8cSEnji Cooper } while (0) 111*8a7d0e8cSEnji Cooper 112*8a7d0e8cSEnji Cooper /* 113*8a7d0e8cSEnji Cooper * Test the given function in all precisions, within a given tolerance. 114*8a7d0e8cSEnji Cooper * The tolerance is specified in ulps. 115*8a7d0e8cSEnji Cooper */ 116*8a7d0e8cSEnji Cooper #define testall_tol(func, x, result, tol) do { \ 117*8a7d0e8cSEnji Cooper test_tol(func, x, result, tol * DBL_ULP()); \ 118*8a7d0e8cSEnji Cooper test_tol(func##f, x, result, tol * FLT_ULP()); \ 119*8a7d0e8cSEnji Cooper } while (0) 120*8a7d0e8cSEnji Cooper #define testall_odd_tol(func, x, result, tol) do { \ 121*8a7d0e8cSEnji Cooper test_odd_tol(func, x, result, tol * DBL_ULP()); \ 122*8a7d0e8cSEnji Cooper test_odd_tol(func##f, x, result, tol * FLT_ULP()); \ 123*8a7d0e8cSEnji Cooper } while (0) 124*8a7d0e8cSEnji Cooper #define testall_even_tol(func, x, result, tol) do { \ 125*8a7d0e8cSEnji Cooper test_even_tol(func, x, result, tol * DBL_ULP()); \ 126*8a7d0e8cSEnji Cooper test_even_tol(func##f, x, result, tol * FLT_ULP()); \ 127*8a7d0e8cSEnji Cooper } while (0) 128*8a7d0e8cSEnji Cooper 129*8a7d0e8cSEnji Cooper 130*8a7d0e8cSEnji Cooper /* Tests for 0 */ 131*8a7d0e8cSEnji Cooper void 132*8a7d0e8cSEnji Cooper test_zero(void) 133*8a7d0e8cSEnji Cooper { 134*8a7d0e8cSEnji Cooper long double complex zero = CMPLXL(0.0, 0.0); 135*8a7d0e8cSEnji Cooper 136*8a7d0e8cSEnji Cooper /* csinh(0) = ctanh(0) = 0; ccosh(0) = 1 (no exceptions raised) */ 137*8a7d0e8cSEnji Cooper testall_odd(csinh, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH); 138*8a7d0e8cSEnji Cooper testall_odd(csin, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH); 139*8a7d0e8cSEnji Cooper testall_even(ccosh, zero, 1.0, ALL_STD_EXCEPT, 0, CS_BOTH); 140*8a7d0e8cSEnji Cooper testall_even(ccos, zero, CMPLXL(1.0, -0.0), ALL_STD_EXCEPT, 0, CS_BOTH); 141*8a7d0e8cSEnji Cooper testall_odd(ctanh, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH); 142*8a7d0e8cSEnji Cooper testall_odd(ctan, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH); 143*8a7d0e8cSEnji Cooper } 144*8a7d0e8cSEnji Cooper 145*8a7d0e8cSEnji Cooper /* 146*8a7d0e8cSEnji Cooper * Tests for NaN inputs. 147*8a7d0e8cSEnji Cooper */ 148*8a7d0e8cSEnji Cooper void 149*8a7d0e8cSEnji Cooper test_nan() 150*8a7d0e8cSEnji Cooper { 151*8a7d0e8cSEnji Cooper long double complex nan_nan = CMPLXL(NAN, NAN); 152*8a7d0e8cSEnji Cooper long double complex z; 153*8a7d0e8cSEnji Cooper 154*8a7d0e8cSEnji Cooper /* 155*8a7d0e8cSEnji Cooper * IN CSINH CCOSH CTANH 156*8a7d0e8cSEnji Cooper * NaN,NaN NaN,NaN NaN,NaN NaN,NaN 157*8a7d0e8cSEnji Cooper * finite,NaN NaN,NaN [inval] NaN,NaN [inval] NaN,NaN [inval] 158*8a7d0e8cSEnji Cooper * NaN,finite NaN,NaN [inval] NaN,NaN [inval] NaN,NaN [inval] 159*8a7d0e8cSEnji Cooper * NaN,Inf NaN,NaN [inval] NaN,NaN [inval] NaN,NaN [inval] 160*8a7d0e8cSEnji Cooper * Inf,NaN +-Inf,NaN Inf,NaN 1,+-0 161*8a7d0e8cSEnji Cooper * 0,NaN +-0,NaN NaN,+-0 NaN,NaN [inval] 162*8a7d0e8cSEnji Cooper * NaN,0 NaN,0 NaN,+-0 NaN,0 163*8a7d0e8cSEnji Cooper */ 164*8a7d0e8cSEnji Cooper z = nan_nan; 165*8a7d0e8cSEnji Cooper testall_odd(csinh, z, nan_nan, ALL_STD_EXCEPT, 0, 0); 166*8a7d0e8cSEnji Cooper testall_even(ccosh, z, nan_nan, ALL_STD_EXCEPT, 0, 0); 167*8a7d0e8cSEnji Cooper testall_odd(ctanh, z, nan_nan, ALL_STD_EXCEPT, 0, 0); 168*8a7d0e8cSEnji Cooper testall_odd(csin, z, nan_nan, ALL_STD_EXCEPT, 0, 0); 169*8a7d0e8cSEnji Cooper testall_even(ccos, z, nan_nan, ALL_STD_EXCEPT, 0, 0); 170*8a7d0e8cSEnji Cooper testall_odd(ctan, z, nan_nan, ALL_STD_EXCEPT, 0, 0); 171*8a7d0e8cSEnji Cooper 172*8a7d0e8cSEnji Cooper z = CMPLXL(42, NAN); 173*8a7d0e8cSEnji Cooper testall_odd(csinh, z, nan_nan, OPT_INVALID, 0, 0); 174*8a7d0e8cSEnji Cooper testall_even(ccosh, z, nan_nan, OPT_INVALID, 0, 0); 175*8a7d0e8cSEnji Cooper /* XXX We allow a spurious inexact exception here. */ 176*8a7d0e8cSEnji Cooper testall_odd(ctanh, z, nan_nan, OPT_INVALID & ~FE_INEXACT, 0, 0); 177*8a7d0e8cSEnji Cooper testall_odd(csin, z, nan_nan, OPT_INVALID, 0, 0); 178*8a7d0e8cSEnji Cooper testall_even(ccos, z, nan_nan, OPT_INVALID, 0, 0); 179*8a7d0e8cSEnji Cooper testall_odd(ctan, z, nan_nan, OPT_INVALID, 0, 0); 180*8a7d0e8cSEnji Cooper 181*8a7d0e8cSEnji Cooper z = CMPLXL(NAN, 42); 182*8a7d0e8cSEnji Cooper testall_odd(csinh, z, nan_nan, OPT_INVALID, 0, 0); 183*8a7d0e8cSEnji Cooper testall_even(ccosh, z, nan_nan, OPT_INVALID, 0, 0); 184*8a7d0e8cSEnji Cooper testall_odd(ctanh, z, nan_nan, OPT_INVALID, 0, 0); 185*8a7d0e8cSEnji Cooper testall_odd(csin, z, nan_nan, OPT_INVALID, 0, 0); 186*8a7d0e8cSEnji Cooper testall_even(ccos, z, nan_nan, OPT_INVALID, 0, 0); 187*8a7d0e8cSEnji Cooper /* XXX We allow a spurious inexact exception here. */ 188*8a7d0e8cSEnji Cooper testall_odd(ctan, z, nan_nan, OPT_INVALID & ~FE_INEXACT, 0, 0); 189*8a7d0e8cSEnji Cooper 190*8a7d0e8cSEnji Cooper z = CMPLXL(NAN, INFINITY); 191*8a7d0e8cSEnji Cooper testall_odd(csinh, z, nan_nan, OPT_INVALID, 0, 0); 192*8a7d0e8cSEnji Cooper testall_even(ccosh, z, nan_nan, OPT_INVALID, 0, 0); 193*8a7d0e8cSEnji Cooper testall_odd(ctanh, z, nan_nan, OPT_INVALID, 0, 0); 194*8a7d0e8cSEnji Cooper testall_odd(csin, z, CMPLXL(NAN, INFINITY), ALL_STD_EXCEPT, 0, 0); 195*8a7d0e8cSEnji Cooper testall_even(ccos, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0, 196*8a7d0e8cSEnji Cooper CS_IMAG); 197*8a7d0e8cSEnji Cooper testall_odd(ctan, z, CMPLXL(0, 1), ALL_STD_EXCEPT, 0, CS_IMAG); 198*8a7d0e8cSEnji Cooper 199*8a7d0e8cSEnji Cooper z = CMPLXL(INFINITY, NAN); 200*8a7d0e8cSEnji Cooper testall_odd(csinh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0, 0); 201*8a7d0e8cSEnji Cooper testall_even(ccosh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0, 202*8a7d0e8cSEnji Cooper CS_REAL); 203*8a7d0e8cSEnji Cooper testall_odd(ctanh, z, CMPLXL(1, 0), ALL_STD_EXCEPT, 0, CS_REAL); 204*8a7d0e8cSEnji Cooper testall_odd(csin, z, nan_nan, OPT_INVALID, 0, 0); 205*8a7d0e8cSEnji Cooper testall_even(ccos, z, nan_nan, OPT_INVALID, 0, 0); 206*8a7d0e8cSEnji Cooper testall_odd(ctan, z, nan_nan, OPT_INVALID, 0, 0); 207*8a7d0e8cSEnji Cooper 208*8a7d0e8cSEnji Cooper z = CMPLXL(0, NAN); 209*8a7d0e8cSEnji Cooper testall_odd(csinh, z, CMPLXL(0, NAN), ALL_STD_EXCEPT, 0, 0); 210*8a7d0e8cSEnji Cooper testall_even(ccosh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, 0); 211*8a7d0e8cSEnji Cooper testall_odd(ctanh, z, nan_nan, OPT_INVALID, 0, 0); 212*8a7d0e8cSEnji Cooper testall_odd(csin, z, CMPLXL(0, NAN), ALL_STD_EXCEPT, 0, CS_REAL); 213*8a7d0e8cSEnji Cooper testall_even(ccos, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, 0); 214*8a7d0e8cSEnji Cooper testall_odd(ctan, z, CMPLXL(0, NAN), ALL_STD_EXCEPT, 0, CS_REAL); 215*8a7d0e8cSEnji Cooper 216*8a7d0e8cSEnji Cooper z = CMPLXL(NAN, 0); 217*8a7d0e8cSEnji Cooper testall_odd(csinh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, CS_IMAG); 218*8a7d0e8cSEnji Cooper testall_even(ccosh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, 0); 219*8a7d0e8cSEnji Cooper testall_odd(ctanh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, CS_IMAG); 220*8a7d0e8cSEnji Cooper testall_odd(csin, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, 0); 221*8a7d0e8cSEnji Cooper testall_even(ccos, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, 0); 222*8a7d0e8cSEnji Cooper testall_odd(ctan, z, nan_nan, OPT_INVALID, 0, 0); 223*8a7d0e8cSEnji Cooper } 224*8a7d0e8cSEnji Cooper 225*8a7d0e8cSEnji Cooper void 226*8a7d0e8cSEnji Cooper test_inf(void) 227*8a7d0e8cSEnji Cooper { 228*8a7d0e8cSEnji Cooper static const long double finites[] = { 229*8a7d0e8cSEnji Cooper 0, M_PI / 4, 3 * M_PI / 4, 5 * M_PI / 4, 230*8a7d0e8cSEnji Cooper }; 231*8a7d0e8cSEnji Cooper long double complex z, c, s; 232*8a7d0e8cSEnji Cooper int i; 233*8a7d0e8cSEnji Cooper 234*8a7d0e8cSEnji Cooper /* 235*8a7d0e8cSEnji Cooper * IN CSINH CCOSH CTANH 236*8a7d0e8cSEnji Cooper * Inf,Inf +-Inf,NaN inval +-Inf,NaN inval 1,+-0 237*8a7d0e8cSEnji Cooper * Inf,finite Inf cis(finite) Inf cis(finite) 1,0 sin(2 finite) 238*8a7d0e8cSEnji Cooper * 0,Inf +-0,NaN inval NaN,+-0 inval NaN,NaN inval 239*8a7d0e8cSEnji Cooper * finite,Inf NaN,NaN inval NaN,NaN inval NaN,NaN inval 240*8a7d0e8cSEnji Cooper */ 241*8a7d0e8cSEnji Cooper z = CMPLXL(INFINITY, INFINITY); 242*8a7d0e8cSEnji Cooper testall_odd(csinh, z, CMPLXL(INFINITY, NAN), 243*8a7d0e8cSEnji Cooper ALL_STD_EXCEPT, FE_INVALID, 0); 244*8a7d0e8cSEnji Cooper testall_even(ccosh, z, CMPLXL(INFINITY, NAN), 245*8a7d0e8cSEnji Cooper ALL_STD_EXCEPT, FE_INVALID, 0); 246*8a7d0e8cSEnji Cooper testall_odd(ctanh, z, CMPLXL(1, 0), ALL_STD_EXCEPT, 0, CS_REAL); 247*8a7d0e8cSEnji Cooper testall_odd(csin, z, CMPLXL(NAN, INFINITY), 248*8a7d0e8cSEnji Cooper ALL_STD_EXCEPT, FE_INVALID, 0); 249*8a7d0e8cSEnji Cooper testall_even(ccos, z, CMPLXL(INFINITY, NAN), 250*8a7d0e8cSEnji Cooper ALL_STD_EXCEPT, FE_INVALID, 0); 251*8a7d0e8cSEnji Cooper testall_odd(ctan, z, CMPLXL(0, 1), ALL_STD_EXCEPT, 0, CS_REAL); 252*8a7d0e8cSEnji Cooper 253*8a7d0e8cSEnji Cooper /* XXX We allow spurious inexact exceptions here (hard to avoid). */ 254*8a7d0e8cSEnji Cooper for (i = 0; i < sizeof(finites) / sizeof(finites[0]); i++) { 255*8a7d0e8cSEnji Cooper z = CMPLXL(INFINITY, finites[i]); 256*8a7d0e8cSEnji Cooper c = INFINITY * cosl(finites[i]); 257*8a7d0e8cSEnji Cooper s = finites[i] == 0 ? finites[i] : INFINITY * sinl(finites[i]); 258*8a7d0e8cSEnji Cooper testall_odd(csinh, z, CMPLXL(c, s), OPT_INEXACT, 0, CS_BOTH); 259*8a7d0e8cSEnji Cooper testall_even(ccosh, z, CMPLXL(c, s), OPT_INEXACT, 0, CS_BOTH); 260*8a7d0e8cSEnji Cooper testall_odd(ctanh, z, CMPLXL(1, 0 * sin(finites[i] * 2)), 261*8a7d0e8cSEnji Cooper OPT_INEXACT, 0, CS_BOTH); 262*8a7d0e8cSEnji Cooper z = CMPLXL(finites[i], INFINITY); 263*8a7d0e8cSEnji Cooper testall_odd(csin, z, CMPLXL(s, c), OPT_INEXACT, 0, CS_BOTH); 264*8a7d0e8cSEnji Cooper testall_even(ccos, z, CMPLXL(c, -s), OPT_INEXACT, 0, CS_BOTH); 265*8a7d0e8cSEnji Cooper testall_odd(ctan, z, CMPLXL(0 * sin(finites[i] * 2), 1), 266*8a7d0e8cSEnji Cooper OPT_INEXACT, 0, CS_BOTH); 267*8a7d0e8cSEnji Cooper } 268*8a7d0e8cSEnji Cooper 269*8a7d0e8cSEnji Cooper z = CMPLXL(0, INFINITY); 270*8a7d0e8cSEnji Cooper testall_odd(csinh, z, CMPLXL(0, NAN), ALL_STD_EXCEPT, FE_INVALID, 0); 271*8a7d0e8cSEnji Cooper testall_even(ccosh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, FE_INVALID, 0); 272*8a7d0e8cSEnji Cooper testall_odd(ctanh, z, CMPLXL(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0); 273*8a7d0e8cSEnji Cooper z = CMPLXL(INFINITY, 0); 274*8a7d0e8cSEnji Cooper testall_odd(csin, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, FE_INVALID, 0); 275*8a7d0e8cSEnji Cooper testall_even(ccos, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, FE_INVALID, 0); 276*8a7d0e8cSEnji Cooper testall_odd(ctan, z, CMPLXL(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0); 277*8a7d0e8cSEnji Cooper 278*8a7d0e8cSEnji Cooper z = CMPLXL(42, INFINITY); 279*8a7d0e8cSEnji Cooper testall_odd(csinh, z, CMPLXL(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0); 280*8a7d0e8cSEnji Cooper testall_even(ccosh, z, CMPLXL(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0); 281*8a7d0e8cSEnji Cooper /* XXX We allow a spurious inexact exception here. */ 282*8a7d0e8cSEnji Cooper testall_odd(ctanh, z, CMPLXL(NAN, NAN), OPT_INEXACT, FE_INVALID, 0); 283*8a7d0e8cSEnji Cooper z = CMPLXL(INFINITY, 42); 284*8a7d0e8cSEnji Cooper testall_odd(csin, z, CMPLXL(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0); 285*8a7d0e8cSEnji Cooper testall_even(ccos, z, CMPLXL(NAN, NAN), ALL_STD_EXCEPT, FE_INVALID, 0); 286*8a7d0e8cSEnji Cooper /* XXX We allow a spurious inexact exception here. */ 287*8a7d0e8cSEnji Cooper testall_odd(ctan, z, CMPLXL(NAN, NAN), OPT_INEXACT, FE_INVALID, 0); 288*8a7d0e8cSEnji Cooper } 289*8a7d0e8cSEnji Cooper 290*8a7d0e8cSEnji Cooper /* Tests along the real and imaginary axes. */ 291*8a7d0e8cSEnji Cooper void 292*8a7d0e8cSEnji Cooper test_axes(void) 293*8a7d0e8cSEnji Cooper { 294*8a7d0e8cSEnji Cooper static const long double nums[] = { 295*8a7d0e8cSEnji Cooper M_PI / 4, M_PI / 2, 3 * M_PI / 4, 296*8a7d0e8cSEnji Cooper 5 * M_PI / 4, 3 * M_PI / 2, 7 * M_PI / 4, 297*8a7d0e8cSEnji Cooper }; 298*8a7d0e8cSEnji Cooper long double complex z; 299*8a7d0e8cSEnji Cooper int i; 300*8a7d0e8cSEnji Cooper 301*8a7d0e8cSEnji Cooper for (i = 0; i < sizeof(nums) / sizeof(nums[0]); i++) { 302*8a7d0e8cSEnji Cooper /* Real axis */ 303*8a7d0e8cSEnji Cooper z = CMPLXL(nums[i], 0.0); 304*8a7d0e8cSEnji Cooper test_odd_tol(csinh, z, CMPLXL(sinh(nums[i]), 0), DBL_ULP()); 305*8a7d0e8cSEnji Cooper test_even_tol(ccosh, z, CMPLXL(cosh(nums[i]), 0), DBL_ULP()); 306*8a7d0e8cSEnji Cooper test_odd_tol(ctanh, z, CMPLXL(tanh(nums[i]), 0), DBL_ULP()); 307*8a7d0e8cSEnji Cooper test_odd_tol(csin, z, CMPLXL(sin(nums[i]), 308*8a7d0e8cSEnji Cooper copysign(0, cos(nums[i]))), DBL_ULP()); 309*8a7d0e8cSEnji Cooper test_even_tol(ccos, z, CMPLXL(cos(nums[i]), 310*8a7d0e8cSEnji Cooper -copysign(0, sin(nums[i]))), DBL_ULP()); 311*8a7d0e8cSEnji Cooper test_odd_tol(ctan, z, CMPLXL(tan(nums[i]), 0), DBL_ULP()); 312*8a7d0e8cSEnji Cooper 313*8a7d0e8cSEnji Cooper test_odd_tol(csinhf, z, CMPLXL(sinhf(nums[i]), 0), FLT_ULP()); 314*8a7d0e8cSEnji Cooper test_even_tol(ccoshf, z, CMPLXL(coshf(nums[i]), 0), FLT_ULP()); 315*8a7d0e8cSEnji Cooper printf("%a %a\n", creal(z), cimag(z)); 316*8a7d0e8cSEnji Cooper printf("%a %a\n", creal(ctanhf(z)), cimag(ctanhf(z))); 317*8a7d0e8cSEnji Cooper printf("%a\n", nextafterf(tanhf(nums[i]), INFINITY)); 318*8a7d0e8cSEnji Cooper test_odd_tol(ctanhf, z, CMPLXL(tanhf(nums[i]), 0), 319*8a7d0e8cSEnji Cooper 1.3 * FLT_ULP()); 320*8a7d0e8cSEnji Cooper test_odd_tol(csinf, z, CMPLXL(sinf(nums[i]), 321*8a7d0e8cSEnji Cooper copysign(0, cosf(nums[i]))), FLT_ULP()); 322*8a7d0e8cSEnji Cooper test_even_tol(ccosf, z, CMPLXL(cosf(nums[i]), 323*8a7d0e8cSEnji Cooper -copysign(0, sinf(nums[i]))), 2 * FLT_ULP()); 324*8a7d0e8cSEnji Cooper test_odd_tol(ctanf, z, CMPLXL(tanf(nums[i]), 0), FLT_ULP()); 325*8a7d0e8cSEnji Cooper 326*8a7d0e8cSEnji Cooper /* Imaginary axis */ 327*8a7d0e8cSEnji Cooper z = CMPLXL(0.0, nums[i]); 328*8a7d0e8cSEnji Cooper test_odd_tol(csinh, z, CMPLXL(copysign(0, cos(nums[i])), 329*8a7d0e8cSEnji Cooper sin(nums[i])), DBL_ULP()); 330*8a7d0e8cSEnji Cooper test_even_tol(ccosh, z, CMPLXL(cos(nums[i]), 331*8a7d0e8cSEnji Cooper copysign(0, sin(nums[i]))), DBL_ULP()); 332*8a7d0e8cSEnji Cooper test_odd_tol(ctanh, z, CMPLXL(0, tan(nums[i])), DBL_ULP()); 333*8a7d0e8cSEnji Cooper test_odd_tol(csin, z, CMPLXL(0, sinh(nums[i])), DBL_ULP()); 334*8a7d0e8cSEnji Cooper test_even_tol(ccos, z, CMPLXL(cosh(nums[i]), -0.0), DBL_ULP()); 335*8a7d0e8cSEnji Cooper test_odd_tol(ctan, z, CMPLXL(0, tanh(nums[i])), DBL_ULP()); 336*8a7d0e8cSEnji Cooper 337*8a7d0e8cSEnji Cooper test_odd_tol(csinhf, z, CMPLXL(copysign(0, cosf(nums[i])), 338*8a7d0e8cSEnji Cooper sinf(nums[i])), FLT_ULP()); 339*8a7d0e8cSEnji Cooper test_even_tol(ccoshf, z, CMPLXL(cosf(nums[i]), 340*8a7d0e8cSEnji Cooper copysign(0, sinf(nums[i]))), FLT_ULP()); 341*8a7d0e8cSEnji Cooper test_odd_tol(ctanhf, z, CMPLXL(0, tanf(nums[i])), FLT_ULP()); 342*8a7d0e8cSEnji Cooper test_odd_tol(csinf, z, CMPLXL(0, sinhf(nums[i])), FLT_ULP()); 343*8a7d0e8cSEnji Cooper test_even_tol(ccosf, z, CMPLXL(coshf(nums[i]), -0.0), 344*8a7d0e8cSEnji Cooper FLT_ULP()); 345*8a7d0e8cSEnji Cooper test_odd_tol(ctanf, z, CMPLXL(0, tanhf(nums[i])), 346*8a7d0e8cSEnji Cooper 1.3 * FLT_ULP()); 347*8a7d0e8cSEnji Cooper } 348*8a7d0e8cSEnji Cooper } 349*8a7d0e8cSEnji Cooper 350*8a7d0e8cSEnji Cooper void 351*8a7d0e8cSEnji Cooper test_small(void) 352*8a7d0e8cSEnji Cooper { 353*8a7d0e8cSEnji Cooper /* 354*8a7d0e8cSEnji Cooper * z = 0.5 + i Pi/4 355*8a7d0e8cSEnji Cooper * sinh(z) = (sinh(0.5) + i cosh(0.5)) * sqrt(2)/2 356*8a7d0e8cSEnji Cooper * cosh(z) = (cosh(0.5) + i sinh(0.5)) * sqrt(2)/2 357*8a7d0e8cSEnji Cooper * tanh(z) = (2cosh(0.5)sinh(0.5) + i) / (2 cosh(0.5)**2 - 1) 358*8a7d0e8cSEnji Cooper * z = -0.5 + i Pi/2 359*8a7d0e8cSEnji Cooper * sinh(z) = cosh(0.5) 360*8a7d0e8cSEnji Cooper * cosh(z) = -i sinh(0.5) 361*8a7d0e8cSEnji Cooper * tanh(z) = -coth(0.5) 362*8a7d0e8cSEnji Cooper * z = 1.0 + i 3Pi/4 363*8a7d0e8cSEnji Cooper * sinh(z) = (-sinh(1) + i cosh(1)) * sqrt(2)/2 364*8a7d0e8cSEnji Cooper * cosh(z) = (-cosh(1) + i sinh(1)) * sqrt(2)/2 365*8a7d0e8cSEnji Cooper * tanh(z) = (2cosh(1)sinh(1) - i) / (2cosh(1)**2 - 1) 366*8a7d0e8cSEnji Cooper */ 367*8a7d0e8cSEnji Cooper static const struct { 368*8a7d0e8cSEnji Cooper long double a, b; 369*8a7d0e8cSEnji Cooper long double sinh_a, sinh_b; 370*8a7d0e8cSEnji Cooper long double cosh_a, cosh_b; 371*8a7d0e8cSEnji Cooper long double tanh_a, tanh_b; 372*8a7d0e8cSEnji Cooper } tests[] = { 373*8a7d0e8cSEnji Cooper { 0.5L, 374*8a7d0e8cSEnji Cooper 0.78539816339744830961566084581987572L, 375*8a7d0e8cSEnji Cooper 0.36847002415910435172083660522240710L, 376*8a7d0e8cSEnji Cooper 0.79735196663945774996093142586179334L, 377*8a7d0e8cSEnji Cooper 0.79735196663945774996093142586179334L, 378*8a7d0e8cSEnji Cooper 0.36847002415910435172083660522240710L, 379*8a7d0e8cSEnji Cooper 0.76159415595576488811945828260479359L, 380*8a7d0e8cSEnji Cooper 0.64805427366388539957497735322615032L }, 381*8a7d0e8cSEnji Cooper { -0.5L, 382*8a7d0e8cSEnji Cooper 1.57079632679489661923132169163975144L, 383*8a7d0e8cSEnji Cooper 0.0L, 384*8a7d0e8cSEnji Cooper 1.12762596520638078522622516140267201L, 385*8a7d0e8cSEnji Cooper 0.0L, 386*8a7d0e8cSEnji Cooper -0.52109530549374736162242562641149156L, 387*8a7d0e8cSEnji Cooper -2.16395341373865284877000401021802312L, 388*8a7d0e8cSEnji Cooper 0.0L }, 389*8a7d0e8cSEnji Cooper { 1.0L, 390*8a7d0e8cSEnji Cooper 2.35619449019234492884698253745962716L, 391*8a7d0e8cSEnji Cooper -0.83099273328405698212637979852748608L, 392*8a7d0e8cSEnji Cooper 1.09112278079550143030545602018565236L, 393*8a7d0e8cSEnji Cooper -1.09112278079550143030545602018565236L, 394*8a7d0e8cSEnji Cooper 0.83099273328405698212637979852748609L, 395*8a7d0e8cSEnji Cooper 0.96402758007581688394641372410092315L, 396*8a7d0e8cSEnji Cooper -0.26580222883407969212086273981988897L } 397*8a7d0e8cSEnji Cooper }; 398*8a7d0e8cSEnji Cooper long double complex z; 399*8a7d0e8cSEnji Cooper int i; 400*8a7d0e8cSEnji Cooper 401*8a7d0e8cSEnji Cooper for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) { 402*8a7d0e8cSEnji Cooper z = CMPLXL(tests[i].a, tests[i].b); 403*8a7d0e8cSEnji Cooper testall_odd_tol(csinh, z, 404*8a7d0e8cSEnji Cooper CMPLXL(tests[i].sinh_a, tests[i].sinh_b), 1.1); 405*8a7d0e8cSEnji Cooper testall_even_tol(ccosh, z, 406*8a7d0e8cSEnji Cooper CMPLXL(tests[i].cosh_a, tests[i].cosh_b), 1.1); 407*8a7d0e8cSEnji Cooper testall_odd_tol(ctanh, z, 408*8a7d0e8cSEnji Cooper CMPLXL(tests[i].tanh_a, tests[i].tanh_b), 1.4); 409*8a7d0e8cSEnji Cooper } 410*8a7d0e8cSEnji Cooper } 411*8a7d0e8cSEnji Cooper 412*8a7d0e8cSEnji Cooper /* Test inputs that might cause overflow in a sloppy implementation. */ 413*8a7d0e8cSEnji Cooper void 414*8a7d0e8cSEnji Cooper test_large(void) 415*8a7d0e8cSEnji Cooper { 416*8a7d0e8cSEnji Cooper long double complex z; 417*8a7d0e8cSEnji Cooper 418*8a7d0e8cSEnji Cooper /* tanh() uses a threshold around x=22, so check both sides. */ 419*8a7d0e8cSEnji Cooper z = CMPLXL(21, 0.78539816339744830961566084581987572L); 420*8a7d0e8cSEnji Cooper testall_odd_tol(ctanh, z, 421*8a7d0e8cSEnji Cooper CMPLXL(1.0, 1.14990445285871196133287617611468468e-18L), 1.2); 422*8a7d0e8cSEnji Cooper z++; 423*8a7d0e8cSEnji Cooper testall_odd_tol(ctanh, z, 424*8a7d0e8cSEnji Cooper CMPLXL(1.0, 1.55622644822675930314266334585597964e-19L), 1); 425*8a7d0e8cSEnji Cooper 426*8a7d0e8cSEnji Cooper z = CMPLXL(355, 0.78539816339744830961566084581987572L); 427*8a7d0e8cSEnji Cooper test_odd_tol(ctanh, z, 428*8a7d0e8cSEnji Cooper CMPLXL(1.0, 8.95257245135025991216632140458264468e-309L), 429*8a7d0e8cSEnji Cooper DBL_ULP()); 430*8a7d0e8cSEnji Cooper #if !defined(__i386__) 431*8a7d0e8cSEnji Cooper z = CMPLXL(30, 0x1p1023L); 432*8a7d0e8cSEnji Cooper test_odd_tol(ctanh, z, 433*8a7d0e8cSEnji Cooper CMPLXL(1.0, -1.62994325413993477997492170229268382e-26L), 434*8a7d0e8cSEnji Cooper DBL_ULP()); 435*8a7d0e8cSEnji Cooper z = CMPLXL(1, 0x1p1023L); 436*8a7d0e8cSEnji Cooper test_odd_tol(ctanh, z, 437*8a7d0e8cSEnji Cooper CMPLXL(0.878606311888306869546254022621986509L, 438*8a7d0e8cSEnji Cooper -0.225462792499754505792678258169527424L), 439*8a7d0e8cSEnji Cooper DBL_ULP()); 440*8a7d0e8cSEnji Cooper #endif 441*8a7d0e8cSEnji Cooper 442*8a7d0e8cSEnji Cooper z = CMPLXL(710.6, 0.78539816339744830961566084581987572L); 443*8a7d0e8cSEnji Cooper test_odd_tol(csinh, z, 444*8a7d0e8cSEnji Cooper CMPLXL(1.43917579766621073533185387499658944e308L, 445*8a7d0e8cSEnji Cooper 1.43917579766621073533185387499658944e308L), DBL_ULP()); 446*8a7d0e8cSEnji Cooper test_even_tol(ccosh, z, 447*8a7d0e8cSEnji Cooper CMPLXL(1.43917579766621073533185387499658944e308L, 448*8a7d0e8cSEnji Cooper 1.43917579766621073533185387499658944e308L), DBL_ULP()); 449*8a7d0e8cSEnji Cooper 450*8a7d0e8cSEnji Cooper z = CMPLXL(1500, 0.78539816339744830961566084581987572L); 451*8a7d0e8cSEnji Cooper testall_odd(csinh, z, CMPLXL(INFINITY, INFINITY), OPT_INEXACT, 452*8a7d0e8cSEnji Cooper FE_OVERFLOW, CS_BOTH); 453*8a7d0e8cSEnji Cooper testall_even(ccosh, z, CMPLXL(INFINITY, INFINITY), OPT_INEXACT, 454*8a7d0e8cSEnji Cooper FE_OVERFLOW, CS_BOTH); 455*8a7d0e8cSEnji Cooper } 456*8a7d0e8cSEnji Cooper 457*8a7d0e8cSEnji Cooper int 458*8a7d0e8cSEnji Cooper main(int argc, char *argv[]) 459*8a7d0e8cSEnji Cooper { 460*8a7d0e8cSEnji Cooper 461*8a7d0e8cSEnji Cooper printf("1..6\n"); 462*8a7d0e8cSEnji Cooper 463*8a7d0e8cSEnji Cooper test_zero(); 464*8a7d0e8cSEnji Cooper printf("ok 1 - ctrig zero\n"); 465*8a7d0e8cSEnji Cooper 466*8a7d0e8cSEnji Cooper test_nan(); 467*8a7d0e8cSEnji Cooper printf("ok 2 - ctrig nan\n"); 468*8a7d0e8cSEnji Cooper 469*8a7d0e8cSEnji Cooper test_inf(); 470*8a7d0e8cSEnji Cooper printf("ok 3 - ctrig inf\n"); 471*8a7d0e8cSEnji Cooper 472*8a7d0e8cSEnji Cooper test_axes(); 473*8a7d0e8cSEnji Cooper printf("ok 4 - ctrig axes\n"); 474*8a7d0e8cSEnji Cooper 475*8a7d0e8cSEnji Cooper test_small(); 476*8a7d0e8cSEnji Cooper printf("ok 5 - ctrig small\n"); 477*8a7d0e8cSEnji Cooper 478*8a7d0e8cSEnji Cooper test_large(); 479*8a7d0e8cSEnji Cooper printf("ok 6 - ctrig large\n"); 480*8a7d0e8cSEnji Cooper 481*8a7d0e8cSEnji Cooper return (0); 482*8a7d0e8cSEnji Cooper } 483