13daee1d6SDavid Schultz /*- 23daee1d6SDavid Schultz * Copyright (c) 2005 Bruce D. Evans and Steven G. Kargl 33daee1d6SDavid Schultz * All rights reserved. 43daee1d6SDavid Schultz * 53daee1d6SDavid Schultz * Redistribution and use in source and binary forms, with or without 63daee1d6SDavid Schultz * modification, are permitted provided that the following conditions 73daee1d6SDavid Schultz * are met: 83daee1d6SDavid Schultz * 1. Redistributions of source code must retain the above copyright 93daee1d6SDavid Schultz * notice unmodified, this list of conditions, and the following 103daee1d6SDavid Schultz * disclaimer. 113daee1d6SDavid Schultz * 2. Redistributions in binary form must reproduce the above copyright 123daee1d6SDavid Schultz * notice, this list of conditions and the following disclaimer in the 133daee1d6SDavid Schultz * documentation and/or other materials provided with the distribution. 143daee1d6SDavid Schultz * 153daee1d6SDavid Schultz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 163daee1d6SDavid Schultz * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 173daee1d6SDavid Schultz * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 183daee1d6SDavid Schultz * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 193daee1d6SDavid Schultz * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 203daee1d6SDavid Schultz * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 213daee1d6SDavid Schultz * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 223daee1d6SDavid Schultz * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 233daee1d6SDavid Schultz * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 243daee1d6SDavid Schultz * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 253daee1d6SDavid Schultz */ 263daee1d6SDavid Schultz 273daee1d6SDavid Schultz /* 283daee1d6SDavid Schultz * Hyperbolic cosine of a complex argument z = x + i y. 293daee1d6SDavid Schultz * 303daee1d6SDavid Schultz * cosh(z) = cosh(x+iy) 313daee1d6SDavid Schultz * = cosh(x) cos(y) + i sinh(x) sin(y). 323daee1d6SDavid Schultz * 333daee1d6SDavid Schultz * Exceptional values are noted in the comments within the source code. 343daee1d6SDavid Schultz * These values and the return value were taken from n1124.pdf. 35*db7548d0STijl Coosemans * The sign of the result for some exceptional values is unspecified but 36*db7548d0STijl Coosemans * must satisfy both cosh(conj(z)) == conj(cosh(z)) and cosh(-z) == cosh(z). 373daee1d6SDavid Schultz */ 383daee1d6SDavid Schultz 393daee1d6SDavid Schultz #include <sys/cdefs.h> 403daee1d6SDavid Schultz __FBSDID("$FreeBSD$"); 413daee1d6SDavid Schultz 423daee1d6SDavid Schultz #include <complex.h> 433daee1d6SDavid Schultz #include <math.h> 443daee1d6SDavid Schultz 453daee1d6SDavid Schultz #include "math_private.h" 463daee1d6SDavid Schultz 47c6df46baSDavid Schultz static const double huge = 0x1p1023; 48c6df46baSDavid Schultz 493daee1d6SDavid Schultz double complex 503daee1d6SDavid Schultz ccosh(double complex z) 513daee1d6SDavid Schultz { 52c6df46baSDavid Schultz double x, y, h; 533daee1d6SDavid Schultz int32_t hx, hy, ix, iy, lx, ly; 543daee1d6SDavid Schultz 553daee1d6SDavid Schultz x = creal(z); 563daee1d6SDavid Schultz y = cimag(z); 573daee1d6SDavid Schultz 583daee1d6SDavid Schultz EXTRACT_WORDS(hx, lx, x); 593daee1d6SDavid Schultz EXTRACT_WORDS(hy, ly, y); 603daee1d6SDavid Schultz 613daee1d6SDavid Schultz ix = 0x7fffffff & hx; 623daee1d6SDavid Schultz iy = 0x7fffffff & hy; 633daee1d6SDavid Schultz 643daee1d6SDavid Schultz /* Handle the nearly-non-exceptional cases where x and y are finite. */ 653daee1d6SDavid Schultz if (ix < 0x7ff00000 && iy < 0x7ff00000) { 663daee1d6SDavid Schultz if ((iy | ly) == 0) 672cec876aSEd Schouten return (CMPLX(cosh(x), x * y)); 68*db7548d0STijl Coosemans if (ix < 0x40360000) /* |x| < 22: normal case */ 692cec876aSEd Schouten return (CMPLX(cosh(x) * cos(y), sinh(x) * sin(y))); 70c6df46baSDavid Schultz 71c6df46baSDavid Schultz /* |x| >= 22, so cosh(x) ~= exp(|x|) */ 72c6df46baSDavid Schultz if (ix < 0x40862e42) { 73c6df46baSDavid Schultz /* x < 710: exp(|x|) won't overflow */ 74c6df46baSDavid Schultz h = exp(fabs(x)) * 0.5; 752cec876aSEd Schouten return (CMPLX(h * cos(y), copysign(h, x) * sin(y))); 76c6df46baSDavid Schultz } else if (ix < 0x4096bbaa) { 77c6df46baSDavid Schultz /* x < 1455: scale to avoid overflow */ 782cec876aSEd Schouten z = __ldexp_cexp(CMPLX(fabs(x), y), -1); 792cec876aSEd Schouten return (CMPLX(creal(z), cimag(z) * copysign(1, x))); 80c6df46baSDavid Schultz } else { 81c6df46baSDavid Schultz /* x >= 1455: the result always overflows */ 82c6df46baSDavid Schultz h = huge * x; 832cec876aSEd Schouten return (CMPLX(h * h * cos(y), h * sin(y))); 84c6df46baSDavid Schultz } 853daee1d6SDavid Schultz } 863daee1d6SDavid Schultz 873daee1d6SDavid Schultz /* 88*db7548d0STijl Coosemans * cosh(+-0 +- I Inf) = dNaN + I (+-)(+-)0. 89*db7548d0STijl Coosemans * The sign of 0 in the result is unspecified. Choice = product 90*db7548d0STijl Coosemans * of the signs of the argument. Raise the invalid floating-point 91*db7548d0STijl Coosemans * exception. 923daee1d6SDavid Schultz * 93*db7548d0STijl Coosemans * cosh(+-0 +- I NaN) = d(NaN) + I (+-)(+-)0. 94*db7548d0STijl Coosemans * The sign of 0 in the result is unspecified. Choice = product 95*db7548d0STijl Coosemans * of the signs of the argument. 963daee1d6SDavid Schultz */ 97*db7548d0STijl Coosemans if ((ix | lx) == 0) /* && iy >= 0x7ff00000 */ 98*db7548d0STijl Coosemans return (CMPLX(y - y, x * copysign(0, y))); 993daee1d6SDavid Schultz 1003daee1d6SDavid Schultz /* 1013daee1d6SDavid Schultz * cosh(+-Inf +- I 0) = +Inf + I (+-)(+-)0. 1023daee1d6SDavid Schultz * 103*db7548d0STijl Coosemans * cosh(NaN +- I 0) = d(NaN) + I (+-)(+-)0. 104*db7548d0STijl Coosemans * The sign of 0 in the result is unspecified. Choice = product 105*db7548d0STijl Coosemans * of the signs of the argument. 1063daee1d6SDavid Schultz */ 107*db7548d0STijl Coosemans if ((iy | ly) == 0) /* && ix >= 0x7ff00000 */ 1082cec876aSEd Schouten return (CMPLX(x * x, copysign(0, x) * y)); 1093daee1d6SDavid Schultz 1103daee1d6SDavid Schultz /* 1113daee1d6SDavid Schultz * cosh(x +- I Inf) = dNaN + I dNaN. 1123daee1d6SDavid Schultz * Raise the invalid floating-point exception for finite nonzero x. 1133daee1d6SDavid Schultz * 1143daee1d6SDavid Schultz * cosh(x + I NaN) = d(NaN) + I d(NaN). 1153daee1d6SDavid Schultz * Optionally raises the invalid floating-point exception for finite 1163daee1d6SDavid Schultz * nonzero x. Choice = don't raise (except for signaling NaNs). 1173daee1d6SDavid Schultz */ 118*db7548d0STijl Coosemans if (ix < 0x7ff00000) /* && iy >= 0x7ff00000 */ 1192cec876aSEd Schouten return (CMPLX(y - y, x * (y - y))); 1203daee1d6SDavid Schultz 1213daee1d6SDavid Schultz /* 1223daee1d6SDavid Schultz * cosh(+-Inf + I NaN) = +Inf + I d(NaN). 1233daee1d6SDavid Schultz * 1243daee1d6SDavid Schultz * cosh(+-Inf +- I Inf) = +Inf + I dNaN. 1253daee1d6SDavid Schultz * The sign of Inf in the result is unspecified. Choice = always +. 1263daee1d6SDavid Schultz * Raise the invalid floating-point exception. 1273daee1d6SDavid Schultz * 1283daee1d6SDavid Schultz * cosh(+-Inf + I y) = +Inf cos(y) +- I Inf sin(y) 1293daee1d6SDavid Schultz */ 130*db7548d0STijl Coosemans if (ix == 0x7ff00000 && lx == 0) { 1313daee1d6SDavid Schultz if (iy >= 0x7ff00000) 132*db7548d0STijl Coosemans return (CMPLX(INFINITY, x * (y - y))); 133*db7548d0STijl Coosemans return (CMPLX(INFINITY * cos(y), x * sin(y))); 1343daee1d6SDavid Schultz } 1353daee1d6SDavid Schultz 1363daee1d6SDavid Schultz /* 1373daee1d6SDavid Schultz * cosh(NaN + I NaN) = d(NaN) + I d(NaN). 1383daee1d6SDavid Schultz * 1393daee1d6SDavid Schultz * cosh(NaN +- I Inf) = d(NaN) + I d(NaN). 1403daee1d6SDavid Schultz * Optionally raises the invalid floating-point exception. 1413daee1d6SDavid Schultz * Choice = raise. 1423daee1d6SDavid Schultz * 1433daee1d6SDavid Schultz * cosh(NaN + I y) = d(NaN) + I d(NaN). 1443daee1d6SDavid Schultz * Optionally raises the invalid floating-point exception for finite 1453daee1d6SDavid Schultz * nonzero y. Choice = don't raise (except for signaling NaNs). 1463daee1d6SDavid Schultz */ 1472cec876aSEd Schouten return (CMPLX((x * x) * (y - y), (x + x) * (y - y))); 1483daee1d6SDavid Schultz } 1493daee1d6SDavid Schultz 1503daee1d6SDavid Schultz double complex 1513daee1d6SDavid Schultz ccos(double complex z) 1523daee1d6SDavid Schultz { 1533daee1d6SDavid Schultz 1543daee1d6SDavid Schultz /* ccos(z) = ccosh(I * z) */ 1552cec876aSEd Schouten return (ccosh(CMPLX(-cimag(z), creal(z)))); 1563daee1d6SDavid Schultz } 157