xref: /dflybsd-src/contrib/openbsd_libm/src/s_cpowl.c (revision 4382f29d99a100bd77a81697c2f699c11f6a472a)
1*05a0b428SJohn Marino /*	$OpenBSD: s_cpowl.c,v 1.2 2011/07/20 19:28:33 martynas Exp $	*/
2*05a0b428SJohn Marino 
3*05a0b428SJohn Marino /*
4*05a0b428SJohn Marino  * Copyright (c) 2008 Stephen L. Moshier <steve@moshier.net>
5*05a0b428SJohn Marino  *
6*05a0b428SJohn Marino  * Permission to use, copy, modify, and distribute this software for any
7*05a0b428SJohn Marino  * purpose with or without fee is hereby granted, provided that the above
8*05a0b428SJohn Marino  * copyright notice and this permission notice appear in all copies.
9*05a0b428SJohn Marino  *
10*05a0b428SJohn Marino  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11*05a0b428SJohn Marino  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12*05a0b428SJohn Marino  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13*05a0b428SJohn Marino  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14*05a0b428SJohn Marino  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15*05a0b428SJohn Marino  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16*05a0b428SJohn Marino  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*05a0b428SJohn Marino  */
18*05a0b428SJohn Marino 
19*05a0b428SJohn Marino /*							cpowl
20*05a0b428SJohn Marino  *
21*05a0b428SJohn Marino  *	Complex power function
22*05a0b428SJohn Marino  *
23*05a0b428SJohn Marino  *
24*05a0b428SJohn Marino  *
25*05a0b428SJohn Marino  * SYNOPSIS:
26*05a0b428SJohn Marino  *
27*05a0b428SJohn Marino  * long double complex cpowl();
28*05a0b428SJohn Marino  * long double complex a, z, w;
29*05a0b428SJohn Marino  *
30*05a0b428SJohn Marino  * w = cpowl (a, z);
31*05a0b428SJohn Marino  *
32*05a0b428SJohn Marino  *
33*05a0b428SJohn Marino  *
34*05a0b428SJohn Marino  * DESCRIPTION:
35*05a0b428SJohn Marino  *
36*05a0b428SJohn Marino  * Raises complex A to the complex Zth power.
37*05a0b428SJohn Marino  * Definition is per AMS55 # 4.2.8,
38*05a0b428SJohn Marino  * analytically equivalent to cpow(a,z) = cexp(z clog(a)).
39*05a0b428SJohn Marino  *
40*05a0b428SJohn Marino  * ACCURACY:
41*05a0b428SJohn Marino  *
42*05a0b428SJohn Marino  *                      Relative error:
43*05a0b428SJohn Marino  * arithmetic   domain     # trials      peak         rms
44*05a0b428SJohn Marino  *    IEEE      -10,+10     30000       9.4e-15     1.5e-15
45*05a0b428SJohn Marino  *
46*05a0b428SJohn Marino  */
47*05a0b428SJohn Marino 
48*05a0b428SJohn Marino #include <complex.h>
49*05a0b428SJohn Marino #include <math.h>
50*05a0b428SJohn Marino 
51*05a0b428SJohn Marino long double complex
cpowl(long double complex a,long double complex z)52*05a0b428SJohn Marino cpowl(long double complex a, long double complex z)
53*05a0b428SJohn Marino {
54*05a0b428SJohn Marino 	long double complex w;
55*05a0b428SJohn Marino 	long double x, y, r, theta, absa, arga;
56*05a0b428SJohn Marino 
57*05a0b428SJohn Marino 	x = creall(z);
58*05a0b428SJohn Marino 	y = cimagl(z);
59*05a0b428SJohn Marino 	absa = cabsl(a);
60*05a0b428SJohn Marino 	if (absa == 0.0L) {
61*05a0b428SJohn Marino 		return (0.0L + 0.0L * I);
62*05a0b428SJohn Marino 	}
63*05a0b428SJohn Marino 	arga = cargl(a);
64*05a0b428SJohn Marino 	r = powl(absa, x);
65*05a0b428SJohn Marino 	theta = x * arga;
66*05a0b428SJohn Marino 	if (y != 0.0L) {
67*05a0b428SJohn Marino 		r = r * expl(-y * arga);
68*05a0b428SJohn Marino 		theta = theta + y * logl(absa);
69*05a0b428SJohn Marino 	}
70*05a0b428SJohn Marino 	w = r * cosl(theta) + (r * sinl(theta)) * I;
71*05a0b428SJohn Marino 	return (w);
72*05a0b428SJohn Marino }
73