1 /*- 2 * Copyright (c) 2008-2013 David Schultz <das@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 */ 26 27 /* 28 * Tests for casin[h](), cacos[h](), and catan[h](). 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include <sys/param.h> 35 #include <assert.h> 36 #include <complex.h> 37 #include <fenv.h> 38 #include <float.h> 39 #include <math.h> 40 #include <stdio.h> 41 42 #include "test-utils.h" 43 44 #pragma STDC FENV_ACCESS ON 45 #pragma STDC CX_LIMITED_RANGE OFF 46 47 /* 48 * Test that a function returns the correct value and sets the 49 * exception flags correctly. The exceptmask specifies which 50 * exceptions we should check. We need to be lenient for several 51 * reasons, but mainly because on some architectures it's impossible 52 * to raise FE_OVERFLOW without raising FE_INEXACT. 53 * 54 * These are macros instead of functions so that assert provides more 55 * meaningful error messages. 56 * 57 * XXX The volatile here is to avoid gcc's bogus constant folding and work 58 * around the lack of support for the FENV_ACCESS pragma. 59 */ 60 #define test_p(func, z, result, exceptmask, excepts, checksign) do { \ 61 volatile long double complex _d = z; \ 62 debug(" testing %s(%Lg + %Lg I) == %Lg + %Lg I\n", #func, \ 63 creall(_d), cimagl(_d), creall(result), cimagl(result)); \ 64 assert(feclearexcept(FE_ALL_EXCEPT) == 0); \ 65 assert(cfpequal_cs((func)(_d), (result), (checksign))); \ 66 assert(((void)(func), fetestexcept(exceptmask) == (excepts))); \ 67 } while (0) 68 69 /* 70 * Test within a given tolerance. The tolerance indicates relative error 71 * in ulps. 72 */ 73 #define test_p_tol(func, z, result, tol) do { \ 74 volatile long double complex _d = z; \ 75 debug(" testing %s(%Lg + %Lg I) ~= %Lg + %Lg I\n", #func, \ 76 creall(_d), cimagl(_d), creall(result), cimagl(result)); \ 77 assert(cfpequal_tol((func)(_d), (result), (tol), CS_BOTH)); \ 78 } while (0) 79 80 /* These wrappers apply the identities f(conj(z)) = conj(f(z)). */ 81 #define test(func, z, result, exceptmask, excepts, checksign) do { \ 82 test_p(func, z, result, exceptmask, excepts, checksign); \ 83 test_p(func, conjl(z), conjl(result), exceptmask, excepts, checksign); \ 84 } while (0) 85 #define test_tol(func, z, result, tol) do { \ 86 test_p_tol(func, z, result, tol); \ 87 test_p_tol(func, conjl(z), conjl(result), tol); \ 88 } while (0) 89 90 /* Test the given function in all precisions. */ 91 #define testall(func, x, result, exceptmask, excepts, checksign) do { \ 92 test(func, x, result, exceptmask, excepts, checksign); \ 93 test(func##f, x, result, exceptmask, excepts, checksign); \ 94 } while (0) 95 #define testall_odd(func, x, result, exceptmask, excepts, checksign) do { \ 96 testall(func, x, result, exceptmask, excepts, checksign); \ 97 testall(func, -(x), -result, exceptmask, excepts, checksign); \ 98 } while (0) 99 #define testall_even(func, x, result, exceptmask, excepts, checksign) do { \ 100 testall(func, x, result, exceptmask, excepts, checksign); \ 101 testall(func, -(x), result, exceptmask, excepts, checksign); \ 102 } while (0) 103 104 /* 105 * Test the given function in all precisions, within a given tolerance. 106 * The tolerance is specified in ulps. 107 */ 108 #define testall_tol(func, x, result, tol) do { \ 109 test_tol(func, x, result, (tol) * DBL_ULP()); \ 110 test_tol(func##f, x, result, (tol) * FLT_ULP()); \ 111 } while (0) 112 #define testall_odd_tol(func, x, result, tol) do { \ 113 testall_tol(func, x, result, tol); \ 114 testall_tol(func, -(x), -result, tol); \ 115 } while (0) 116 #define testall_even_tol(func, x, result, tol) do { \ 117 testall_tol(func, x, result, tol); \ 118 testall_tol(func, -(x), result, tol); \ 119 } while (0) 120 121 static const long double 122 pi = 3.14159265358979323846264338327950280L, 123 c3pi = 9.42477796076937971538793014983850839L; 124 125 126 /* Tests for 0 */ 127 static void 128 test_zero(void) 129 { 130 long double complex zero = CMPLXL(0.0, 0.0); 131 132 testall_tol(cacosh, zero, CMPLXL(0.0, pi / 2), 1); 133 testall_tol(cacosh, -zero, CMPLXL(0.0, -pi / 2), 1); 134 testall_tol(cacos, zero, CMPLXL(pi / 2, -0.0), 1); 135 testall_tol(cacos, -zero, CMPLXL(pi / 2, 0.0), 1); 136 137 testall_odd(casinh, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH); 138 testall_odd(casin, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH); 139 140 testall_odd(catanh, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH); 141 testall_odd(catan, zero, zero, ALL_STD_EXCEPT, 0, CS_BOTH); 142 } 143 144 /* 145 * Tests for NaN inputs. 146 */ 147 static void 148 test_nan(void) 149 { 150 long double complex nan_nan = CMPLXL(NAN, NAN); 151 long double complex z; 152 153 /* 154 * IN CACOSH CACOS CASINH CATANH 155 * NaN,NaN NaN,NaN NaN,NaN NaN,NaN NaN,NaN 156 * finite,NaN NaN,NaN* NaN,NaN* NaN,NaN* NaN,NaN* 157 * NaN,finite NaN,NaN* NaN,NaN* NaN,NaN* NaN,NaN* 158 * NaN,Inf Inf,NaN NaN,-Inf ?Inf,NaN ?0,pi/2 159 * +-Inf,NaN Inf,NaN NaN,?Inf +-Inf,NaN +-0,NaN 160 * +-0,NaN NaN,NaN* pi/2,NaN NaN,NaN* +-0,NaN 161 * NaN,0 NaN,NaN* NaN,NaN* NaN,0 NaN,NaN* 162 * 163 * * = raise invalid 164 */ 165 z = nan_nan; 166 testall(cacosh, z, nan_nan, ALL_STD_EXCEPT, 0, 0); 167 testall(cacos, z, nan_nan, ALL_STD_EXCEPT, 0, 0); 168 testall(casinh, z, nan_nan, ALL_STD_EXCEPT, 0, 0); 169 testall(casin, z, nan_nan, ALL_STD_EXCEPT, 0, 0); 170 testall(catanh, z, nan_nan, ALL_STD_EXCEPT, 0, 0); 171 testall(catan, z, nan_nan, ALL_STD_EXCEPT, 0, 0); 172 173 z = CMPLXL(0.5, NAN); 174 testall(cacosh, z, nan_nan, OPT_INVALID, 0, 0); 175 testall(cacos, z, nan_nan, OPT_INVALID, 0, 0); 176 testall(casinh, z, nan_nan, OPT_INVALID, 0, 0); 177 testall(casin, z, nan_nan, OPT_INVALID, 0, 0); 178 testall(catanh, z, nan_nan, OPT_INVALID, 0, 0); 179 testall(catan, z, nan_nan, OPT_INVALID, 0, 0); 180 181 z = CMPLXL(NAN, 0.5); 182 testall(cacosh, z, nan_nan, OPT_INVALID, 0, 0); 183 testall(cacos, z, nan_nan, OPT_INVALID, 0, 0); 184 testall(casinh, z, nan_nan, OPT_INVALID, 0, 0); 185 testall(casin, z, nan_nan, OPT_INVALID, 0, 0); 186 testall(catanh, z, nan_nan, OPT_INVALID, 0, 0); 187 testall(catan, z, nan_nan, OPT_INVALID, 0, 0); 188 189 z = CMPLXL(NAN, INFINITY); 190 testall(cacosh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0, CS_REAL); 191 testall(cacosh, -z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0, CS_REAL); 192 testall(cacos, z, CMPLXL(NAN, -INFINITY), ALL_STD_EXCEPT, 0, CS_IMAG); 193 testall(casinh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0, 0); 194 testall(casin, z, CMPLXL(NAN, INFINITY), ALL_STD_EXCEPT, 0, CS_IMAG); 195 testall_tol(catanh, z, CMPLXL(0.0, pi / 2), 1); 196 testall(catan, z, CMPLXL(NAN, 0.0), ALL_STD_EXCEPT, 0, CS_IMAG); 197 198 z = CMPLXL(INFINITY, NAN); 199 testall_even(cacosh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0, 200 CS_REAL); 201 testall_even(cacos, z, CMPLXL(NAN, INFINITY), ALL_STD_EXCEPT, 0, 0); 202 testall_odd(casinh, z, CMPLXL(INFINITY, NAN), ALL_STD_EXCEPT, 0, 203 CS_REAL); 204 testall_odd(casin, z, CMPLXL(NAN, INFINITY), ALL_STD_EXCEPT, 0, 0); 205 testall_odd(catanh, z, CMPLXL(0.0, NAN), ALL_STD_EXCEPT, 0, CS_REAL); 206 testall_odd_tol(catan, z, CMPLXL(pi / 2, 0.0), 1); 207 208 z = CMPLXL(0.0, NAN); 209 /* XXX We allow a spurious inexact exception here. */ 210 testall_even(cacosh, z, nan_nan, OPT_INVALID & ~FE_INEXACT, 0, 0); 211 testall_even_tol(cacos, z, CMPLXL(pi / 2, NAN), 1); 212 testall_odd(casinh, z, nan_nan, OPT_INVALID, 0, 0); 213 testall_odd(casin, z, CMPLXL(0.0, NAN), ALL_STD_EXCEPT, 0, CS_REAL); 214 testall_odd(catanh, z, CMPLXL(0.0, NAN), OPT_INVALID, 0, CS_REAL); 215 testall_odd(catan, z, nan_nan, OPT_INVALID, 0, 0); 216 217 z = CMPLXL(NAN, 0.0); 218 testall(cacosh, z, nan_nan, OPT_INVALID, 0, 0); 219 testall(cacos, z, nan_nan, OPT_INVALID, 0, 0); 220 testall(casinh, z, CMPLXL(NAN, 0), ALL_STD_EXCEPT, 0, CS_IMAG); 221 testall(casin, z, nan_nan, OPT_INVALID, 0, 0); 222 testall(catanh, z, nan_nan, OPT_INVALID, 0, CS_IMAG); 223 testall(catan, z, CMPLXL(NAN, 0.0), ALL_STD_EXCEPT, 0, 0); 224 } 225 226 static void 227 test_inf(void) 228 { 229 long double complex z; 230 231 /* 232 * IN CACOSH CACOS CASINH CATANH 233 * Inf,Inf Inf,pi/4 pi/4,-Inf Inf,pi/4 0,pi/2 234 * -Inf,Inf Inf,3pi/4 3pi/4,-Inf --- --- 235 * Inf,finite Inf,0 0,-Inf Inf,0 0,pi/2 236 * -Inf,finite Inf,pi pi,-Inf --- --- 237 * finite,Inf Inf,pi/2 pi/2,-Inf Inf,pi/2 0,pi/2 238 */ 239 z = CMPLXL(INFINITY, INFINITY); 240 testall_tol(cacosh, z, CMPLXL(INFINITY, pi / 4), 1); 241 testall_tol(cacosh, -z, CMPLXL(INFINITY, -c3pi / 4), 1); 242 testall_tol(cacos, z, CMPLXL(pi / 4, -INFINITY), 1); 243 testall_tol(cacos, -z, CMPLXL(c3pi / 4, INFINITY), 1); 244 testall_odd_tol(casinh, z, CMPLXL(INFINITY, pi / 4), 1); 245 testall_odd_tol(casin, z, CMPLXL(pi / 4, INFINITY), 1); 246 testall_odd_tol(catanh, z, CMPLXL(0, pi / 2), 1); 247 testall_odd_tol(catan, z, CMPLXL(pi / 2, 0), 1); 248 249 z = CMPLXL(INFINITY, 0.5); 250 /* XXX We allow a spurious inexact exception here. */ 251 testall(cacosh, z, CMPLXL(INFINITY, 0), OPT_INEXACT, 0, CS_BOTH); 252 testall_tol(cacosh, -z, CMPLXL(INFINITY, -pi), 1); 253 testall(cacos, z, CMPLXL(0, -INFINITY), OPT_INEXACT, 0, CS_BOTH); 254 testall_tol(cacos, -z, CMPLXL(pi, INFINITY), 1); 255 testall_odd(casinh, z, CMPLXL(INFINITY, 0), OPT_INEXACT, 0, CS_BOTH); 256 testall_odd_tol(casin, z, CMPLXL(pi / 2, INFINITY), 1); 257 testall_odd_tol(catanh, z, CMPLXL(0, pi / 2), 1); 258 testall_odd_tol(catan, z, CMPLXL(pi / 2, 0), 1); 259 260 z = CMPLXL(0.5, INFINITY); 261 testall_tol(cacosh, z, CMPLXL(INFINITY, pi / 2), 1); 262 testall_tol(cacosh, -z, CMPLXL(INFINITY, -pi / 2), 1); 263 testall_tol(cacos, z, CMPLXL(pi / 2, -INFINITY), 1); 264 testall_tol(cacos, -z, CMPLXL(pi / 2, INFINITY), 1); 265 testall_odd_tol(casinh, z, CMPLXL(INFINITY, pi / 2), 1); 266 /* XXX We allow a spurious inexact exception here. */ 267 testall_odd(casin, z, CMPLXL(0.0, INFINITY), OPT_INEXACT, 0, CS_BOTH); 268 testall_odd_tol(catanh, z, CMPLXL(0, pi / 2), 1); 269 testall_odd_tol(catan, z, CMPLXL(pi / 2, 0), 1); 270 } 271 272 /* Tests along the real and imaginary axes. */ 273 static void 274 test_axes(void) 275 { 276 static const long double nums[] = { 277 -2, -1, -0.5, 0.5, 1, 2 278 }; 279 long double complex z; 280 unsigned i; 281 282 for (i = 0; i < nitems(nums); i++) { 283 /* Real axis */ 284 z = CMPLXL(nums[i], 0.0); 285 if (fabsl(nums[i]) <= 1) { 286 testall_tol(cacosh, z, CMPLXL(0.0, acos(nums[i])), 1); 287 testall_tol(cacos, z, CMPLXL(acosl(nums[i]), -0.0), 1); 288 testall_tol(casin, z, CMPLXL(asinl(nums[i]), 0.0), 1); 289 testall_tol(catanh, z, CMPLXL(atanh(nums[i]), 0.0), 1); 290 } else { 291 testall_tol(cacosh, z, 292 CMPLXL(acosh(fabsl(nums[i])), 293 (nums[i] < 0) ? pi : 0), 1); 294 testall_tol(cacos, z, 295 CMPLXL((nums[i] < 0) ? pi : 0, 296 -acosh(fabsl(nums[i]))), 1); 297 testall_tol(casin, z, 298 CMPLXL(copysign(pi / 2, nums[i]), 299 acosh(fabsl(nums[i]))), 1); 300 testall_tol(catanh, z, 301 CMPLXL(atanh(1 / nums[i]), pi / 2), 1); 302 } 303 testall_tol(casinh, z, CMPLXL(asinh(nums[i]), 0.0), 1); 304 testall_tol(catan, z, CMPLXL(atan(nums[i]), 0), 1); 305 306 /* TODO: Test the imaginary axis. */ 307 } 308 } 309 310 static void 311 test_small(void) 312 { 313 /* 314 * z = 0.75 + i 0.25 315 * acos(z) = Pi/4 - i ln(2)/2 316 * asin(z) = Pi/4 + i ln(2)/2 317 * atan(z) = atan(4)/2 + i ln(17/9)/4 318 */ 319 complex long double z; 320 complex long double acos_z; 321 complex long double asin_z; 322 complex long double atan_z; 323 324 z = CMPLXL(0.75L, 0.25L); 325 acos_z = CMPLXL(pi / 4, -0.34657359027997265470861606072908828L); 326 asin_z = CMPLXL(pi / 4, 0.34657359027997265470861606072908828L); 327 atan_z = CMPLXL(0.66290883183401623252961960521423782L, 328 0.15899719167999917436476103600701878L); 329 330 testall_tol(cacos, z, acos_z, 2); 331 testall_odd_tol(casin, z, asin_z, 2); 332 testall_odd_tol(catan, z, atan_z, 2); 333 } 334 335 /* Test inputs that might cause overflow in a sloppy implementation. */ 336 static void 337 test_large(void) 338 { 339 340 /* TODO: Write these tests */ 341 } 342 343 int 344 main(void) 345 { 346 347 printf("1..6\n"); 348 349 test_zero(); 350 printf("ok 1 - invctrig zero\n"); 351 352 test_nan(); 353 printf("ok 2 - invctrig nan\n"); 354 355 test_inf(); 356 printf("ok 3 - invctrig inf\n"); 357 358 test_axes(); 359 printf("ok 4 - invctrig axes\n"); 360 361 test_small(); 362 printf("ok 5 - invctrig small\n"); 363 364 test_large(); 365 printf("ok 6 - invctrig large\n"); 366 367 return (0); 368 } 369