s_ccosh.c revision 226458
1/*-
2 * Copyright (c) 2005 Bruce D. Evans and Steven G. Kargl
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 unmodified, this list of conditions, and the following
10 *    disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27/*
28 * Hyperbolic cosine of a complex argument z = x + i y.
29 *
30 * cosh(z) = cosh(x+iy)
31 *         = cosh(x) cos(y) + i sinh(x) sin(y).
32 *
33 * Exceptional values are noted in the comments within the source code.
34 * These values and the return value were taken from n1124.pdf.
35 */
36
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD: head/lib/msun/src/s_ccosh.c 226458 2011-10-17 05:41:03Z das $");
39
40#include <complex.h>
41#include <math.h>
42
43#include "math_private.h"
44
45double complex
46ccosh(double complex z)
47{
48	double x, y;
49	int32_t hx, hy, ix, iy, lx, ly;
50
51	x = creal(z);
52	y = cimag(z);
53
54	EXTRACT_WORDS(hx, lx, x);
55	EXTRACT_WORDS(hy, ly, y);
56
57	ix = 0x7fffffff & hx;
58	iy = 0x7fffffff & hy;
59
60	/* Handle the nearly-non-exceptional cases where x and y are finite. */
61	if (ix < 0x7ff00000 && iy < 0x7ff00000) {
62		if ((iy | ly) == 0)
63			return (cpack(cosh(x), x * y));
64		/* XXX We don't handle |x| > DBL_MAX ln(2) yet. */
65		return (cpack(cosh(x) * cos(y), sinh(x) * sin(y)));
66	}
67
68	/*
69	 * cosh(+-0 +- I Inf) = dNaN + I sign(d(+-0, dNaN))0.
70	 * The sign of 0 in the result is unspecified.  Choice = normally
71	 * the same as dNaN.  Raise the invalid floating-point exception.
72	 *
73	 * cosh(+-0 +- I NaN) = d(NaN) + I sign(d(+-0, NaN))0.
74	 * The sign of 0 in the result is unspecified.  Choice = normally
75	 * the same as d(NaN).
76	 */
77	if ((ix | lx) == 0 && iy >= 0x7ff00000)
78		return (cpack(y - y, copysign(0, x * (y - y))));
79
80	/*
81	 * cosh(+-Inf +- I 0) = +Inf + I (+-)(+-)0.
82	 *
83	 * cosh(NaN +- I 0)   = d(NaN) + I sign(d(NaN, +-0))0.
84	 * The sign of 0 in the result is unspecified.
85	 */
86	if ((iy | ly) == 0 && ix >= 0x7ff00000) {
87		if (((hx & 0xfffff) | lx) == 0)
88			return (cpack(x * x, copysign(0, x) * y));
89		return (cpack(x * x, copysign(0, (x + x) * y)));
90	}
91
92	/*
93	 * cosh(x +- I Inf) = dNaN + I dNaN.
94	 * Raise the invalid floating-point exception for finite nonzero x.
95	 *
96	 * cosh(x + I NaN) = d(NaN) + I d(NaN).
97	 * Optionally raises the invalid floating-point exception for finite
98	 * nonzero x.  Choice = don't raise (except for signaling NaNs).
99	 */
100	if (ix < 0x7ff00000 && iy >= 0x7ff00000)
101		return (cpack(y - y, x * (y - y)));
102
103	/*
104	 * cosh(+-Inf + I NaN)  = +Inf + I d(NaN).
105	 *
106	 * cosh(+-Inf +- I Inf) = +Inf + I dNaN.
107	 * The sign of Inf in the result is unspecified.  Choice = always +.
108	 * Raise the invalid floating-point exception.
109	 *
110	 * cosh(+-Inf + I y)   = +Inf cos(y) +- I Inf sin(y)
111	 */
112	if (ix >= 0x7ff00000 && ((hx & 0xfffff) | lx) == 0) {
113		if (iy >= 0x7ff00000)
114			return (cpack(x * x, x * (y - y)));
115		return (cpack((x * x) * cos(y), x * sin(y)));
116	}
117
118	/*
119	 * cosh(NaN + I NaN)  = d(NaN) + I d(NaN).
120	 *
121	 * cosh(NaN +- I Inf) = d(NaN) + I d(NaN).
122	 * Optionally raises the invalid floating-point exception.
123	 * Choice = raise.
124	 *
125	 * cosh(NaN + I y)    = d(NaN) + I d(NaN).
126	 * Optionally raises the invalid floating-point exception for finite
127	 * nonzero y.  Choice = don't raise (except for signaling NaNs).
128	 */
129	return (cpack((x * x) * (y - y), (x + x) * (y - y)));
130}
131
132double complex
133ccos(double complex z)
134{
135
136	/* ccos(z) = ccosh(I * z) */
137	return (ccosh(cpack(-cimag(z), creal(z))));
138}
139