1226458Sdas/*- 2226458Sdas * Copyright (c) 2005 Bruce D. Evans and Steven G. Kargl 3226458Sdas * All rights reserved. 4226458Sdas * 5226458Sdas * Redistribution and use in source and binary forms, with or without 6226458Sdas * modification, are permitted provided that the following conditions 7226458Sdas * are met: 8226458Sdas * 1. Redistributions of source code must retain the above copyright 9226458Sdas * notice unmodified, this list of conditions, and the following 10226458Sdas * disclaimer. 11226458Sdas * 2. Redistributions in binary form must reproduce the above copyright 12226458Sdas * notice, this list of conditions and the following disclaimer in the 13226458Sdas * documentation and/or other materials provided with the distribution. 14226458Sdas * 15226458Sdas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16226458Sdas * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17226458Sdas * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18226458Sdas * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19226458Sdas * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20226458Sdas * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21226458Sdas * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22226458Sdas * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23226458Sdas * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24226458Sdas * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25226458Sdas */ 26226458Sdas 27226458Sdas/* 28226458Sdas * Hyperbolic cosine of a complex argument z = x + i y. 29226458Sdas * 30226458Sdas * cosh(z) = cosh(x+iy) 31226458Sdas * = cosh(x) cos(y) + i sinh(x) sin(y). 32226458Sdas * 33226458Sdas * Exceptional values are noted in the comments within the source code. 34226458Sdas * These values and the return value were taken from n1124.pdf. 35284810Stijl * The sign of the result for some exceptional values is unspecified but 36284810Stijl * must satisfy both cosh(conj(z)) == conj(cosh(z)) and cosh(-z) == cosh(z). 37226458Sdas */ 38226458Sdas 39226458Sdas#include <sys/cdefs.h> 40226458Sdas__FBSDID("$FreeBSD$"); 41226458Sdas 42226458Sdas#include <complex.h> 43226458Sdas#include <math.h> 44226458Sdas 45226458Sdas#include "math_private.h" 46226458Sdas 47226599Sdasstatic const double huge = 0x1p1023; 48226599Sdas 49226458Sdasdouble complex 50226458Sdasccosh(double complex z) 51226458Sdas{ 52226599Sdas double x, y, h; 53226458Sdas int32_t hx, hy, ix, iy, lx, ly; 54226458Sdas 55226458Sdas x = creal(z); 56226458Sdas y = cimag(z); 57226458Sdas 58226458Sdas EXTRACT_WORDS(hx, lx, x); 59226458Sdas EXTRACT_WORDS(hy, ly, y); 60226458Sdas 61226458Sdas ix = 0x7fffffff & hx; 62226458Sdas iy = 0x7fffffff & hy; 63226458Sdas 64226458Sdas /* Handle the nearly-non-exceptional cases where x and y are finite. */ 65226458Sdas if (ix < 0x7ff00000 && iy < 0x7ff00000) { 66226458Sdas if ((iy | ly) == 0) 67284810Stijl return (CMPLX(cosh(x), x * y)); 68284810Stijl if (ix < 0x40360000) /* |x| < 22: normal case */ 69284810Stijl return (CMPLX(cosh(x) * cos(y), sinh(x) * sin(y))); 70226599Sdas 71226599Sdas /* |x| >= 22, so cosh(x) ~= exp(|x|) */ 72226599Sdas if (ix < 0x40862e42) { 73226599Sdas /* x < 710: exp(|x|) won't overflow */ 74226599Sdas h = exp(fabs(x)) * 0.5; 75284810Stijl return (CMPLX(h * cos(y), copysign(h, x) * sin(y))); 76226599Sdas } else if (ix < 0x4096bbaa) { 77226599Sdas /* x < 1455: scale to avoid overflow */ 78284810Stijl z = __ldexp_cexp(CMPLX(fabs(x), y), -1); 79284810Stijl return (CMPLX(creal(z), cimag(z) * copysign(1, x))); 80226599Sdas } else { 81226599Sdas /* x >= 1455: the result always overflows */ 82226599Sdas h = huge * x; 83284810Stijl return (CMPLX(h * h * cos(y), h * sin(y))); 84226599Sdas } 85226458Sdas } 86226458Sdas 87226458Sdas /* 88284810Stijl * cosh(+-0 +- I Inf) = dNaN + I (+-)(+-)0. 89284810Stijl * The sign of 0 in the result is unspecified. Choice = product 90284810Stijl * of the signs of the argument. Raise the invalid floating-point 91284810Stijl * exception. 92226458Sdas * 93284810Stijl * cosh(+-0 +- I NaN) = d(NaN) + I (+-)(+-)0. 94284810Stijl * The sign of 0 in the result is unspecified. Choice = product 95284810Stijl * of the signs of the argument. 96226458Sdas */ 97284810Stijl if ((ix | lx) == 0) /* && iy >= 0x7ff00000 */ 98284810Stijl return (CMPLX(y - y, x * copysign(0, y))); 99226458Sdas 100226458Sdas /* 101226458Sdas * cosh(+-Inf +- I 0) = +Inf + I (+-)(+-)0. 102226458Sdas * 103284810Stijl * cosh(NaN +- I 0) = d(NaN) + I (+-)(+-)0. 104284810Stijl * The sign of 0 in the result is unspecified. Choice = product 105284810Stijl * of the signs of the argument. 106226458Sdas */ 107284810Stijl if ((iy | ly) == 0) /* && ix >= 0x7ff00000 */ 108284810Stijl return (CMPLX(x * x, copysign(0, x) * y)); 109226458Sdas 110226458Sdas /* 111226458Sdas * cosh(x +- I Inf) = dNaN + I dNaN. 112226458Sdas * Raise the invalid floating-point exception for finite nonzero x. 113226458Sdas * 114226458Sdas * cosh(x + I NaN) = d(NaN) + I d(NaN). 115226458Sdas * Optionally raises the invalid floating-point exception for finite 116226458Sdas * nonzero x. Choice = don't raise (except for signaling NaNs). 117226458Sdas */ 118284810Stijl if (ix < 0x7ff00000) /* && iy >= 0x7ff00000 */ 119284810Stijl return (CMPLX(y - y, x * (y - y))); 120226458Sdas 121226458Sdas /* 122226458Sdas * cosh(+-Inf + I NaN) = +Inf + I d(NaN). 123226458Sdas * 124226458Sdas * cosh(+-Inf +- I Inf) = +Inf + I dNaN. 125226458Sdas * The sign of Inf in the result is unspecified. Choice = always +. 126226458Sdas * Raise the invalid floating-point exception. 127226458Sdas * 128226458Sdas * cosh(+-Inf + I y) = +Inf cos(y) +- I Inf sin(y) 129226458Sdas */ 130284810Stijl if (ix == 0x7ff00000 && lx == 0) { 131226458Sdas if (iy >= 0x7ff00000) 132284810Stijl return (CMPLX(INFINITY, x * (y - y))); 133284810Stijl return (CMPLX(INFINITY * cos(y), x * sin(y))); 134226458Sdas } 135226458Sdas 136226458Sdas /* 137226458Sdas * cosh(NaN + I NaN) = d(NaN) + I d(NaN). 138226458Sdas * 139226458Sdas * cosh(NaN +- I Inf) = d(NaN) + I d(NaN). 140226458Sdas * Optionally raises the invalid floating-point exception. 141226458Sdas * Choice = raise. 142226458Sdas * 143226458Sdas * cosh(NaN + I y) = d(NaN) + I d(NaN). 144226458Sdas * Optionally raises the invalid floating-point exception for finite 145226458Sdas * nonzero y. Choice = don't raise (except for signaling NaNs). 146226458Sdas */ 147284810Stijl return (CMPLX((x * x) * (y - y), (x + x) * (y - y))); 148226458Sdas} 149226458Sdas 150226458Sdasdouble complex 151226458Sdasccos(double complex z) 152226458Sdas{ 153226458Sdas 154226458Sdas /* ccos(z) = ccosh(I * z) */ 155284810Stijl return (ccosh(CMPLX(-cimag(z), creal(z)))); 156226458Sdas} 157