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