1 /* $OpenBSD: s_catan.c,v 1.1 2008/09/07 20:36:09 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 /* catan() 19 * 20 * Complex circular arc tangent 21 * 22 * 23 * 24 * SYNOPSIS: 25 * 26 * double complex catan(); 27 * double complex z, w; 28 * 29 * w = catan (z); 30 * 31 * 32 * 33 * DESCRIPTION: 34 * 35 * If 36 * z = x + iy, 37 * 38 * then 39 * 1 ( 2x ) 40 * Re w = - arctan(-----------) + k PI 41 * 2 ( 2 2) 42 * (1 - x - y ) 43 * 44 * ( 2 2) 45 * 1 (x + (y+1) ) 46 * Im w = - log(------------) 47 * 4 ( 2 2) 48 * (x + (y-1) ) 49 * 50 * Where k is an arbitrary integer. 51 * 52 * catan(z) = -i catanh(iz). 53 * 54 * ACCURACY: 55 * 56 * Relative error: 57 * arithmetic domain # trials peak rms 58 * DEC -10,+10 5900 1.3e-16 7.8e-18 59 * IEEE -10,+10 30000 2.3e-15 8.5e-17 60 * The check catan( ctan(z) ) = z, with |x| and |y| < PI/2, 61 * had peak relative error 1.5e-16, rms relative error 62 * 2.9e-17. See also clog(). 63 */ 64 65 #include <complex.h> 66 #include <math.h> 67 68 #define MAXNUM 1.0e308 69 70 static const double DP1 = 3.14159265160560607910E0; 71 static const double DP2 = 1.98418714791870343106E-9; 72 static const double DP3 = 1.14423774522196636802E-17; 73 74 static double 75 _redupi(double x) 76 { 77 double t; 78 long i; 79 80 t = x/M_PI; 81 if(t >= 0.0) 82 t += 0.5; 83 else 84 t -= 0.5; 85 86 i = t; /* the multiple */ 87 t = i; 88 t = ((x - t * DP1) - t * DP2) - t * DP3; 89 return (t); 90 } 91 92 double complex 93 catan(double complex z) 94 { 95 double complex w; 96 double a, t, x, x2, y; 97 98 x = creal (z); 99 y = cimag (z); 100 101 if ((x == 0.0) && (y > 1.0)) 102 goto ovrf; 103 104 x2 = x * x; 105 a = 1.0 - x2 - (y * y); 106 if (a == 0.0) 107 goto ovrf; 108 109 t = 0.5 * atan2 (2.0 * x, a); 110 w = _redupi (t); 111 112 t = y - 1.0; 113 a = x2 + (t * t); 114 if (a == 0.0) 115 goto ovrf; 116 117 t = y + 1.0; 118 a = (x2 + (t * t))/a; 119 w = w + (0.25 * log (a)) * I; 120 return (w); 121 122 ovrf: 123 /*mtherr ("catan", OVERFLOW);*/ 124 w = MAXNUM + MAXNUM * I; 125 return (w); 126 } 127