xref: /netbsd/lib/libm/complex/catanl.c (revision 5e0f17b2)
1*5e0f17b2Schristos /* $NetBSD: catanl.c,v 1.1 2014/10/10 00:48:18 christos Exp $ */
2*5e0f17b2Schristos 
3*5e0f17b2Schristos /*-
4*5e0f17b2Schristos  * Copyright (c) 2007 The NetBSD Foundation, Inc.
5*5e0f17b2Schristos  * All rights reserved.
6*5e0f17b2Schristos  *
7*5e0f17b2Schristos  * This code is derived from software written by Stephen L. Moshier.
8*5e0f17b2Schristos  * It is redistributed by the NetBSD Foundation by permission of the author.
9*5e0f17b2Schristos  *
10*5e0f17b2Schristos  * Redistribution and use in source and binary forms, with or without
11*5e0f17b2Schristos  * modification, are permitted provided that the following conditions
12*5e0f17b2Schristos  * are met:
13*5e0f17b2Schristos  * 1. Redistributions of source code must retain the above copyright
14*5e0f17b2Schristos  *    notice, this list of conditions and the following disclaimer.
15*5e0f17b2Schristos  * 2. Redistributions in binary form must reproduce the above copyright
16*5e0f17b2Schristos  *    notice, this list of conditions and the following disclaimer in the
17*5e0f17b2Schristos  *    documentation and/or other materials provided with the distribution.
18*5e0f17b2Schristos  *
19*5e0f17b2Schristos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*5e0f17b2Schristos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*5e0f17b2Schristos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*5e0f17b2Schristos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*5e0f17b2Schristos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*5e0f17b2Schristos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*5e0f17b2Schristos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*5e0f17b2Schristos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*5e0f17b2Schristos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*5e0f17b2Schristos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*5e0f17b2Schristos  * POSSIBILITY OF SUCH DAMAGE.
30*5e0f17b2Schristos  */
31*5e0f17b2Schristos 
32*5e0f17b2Schristos #include "../src/namespace.h"
33*5e0f17b2Schristos #include <complex.h>
34*5e0f17b2Schristos #include <math.h>
35*5e0f17b2Schristos #include <float.h>
36*5e0f17b2Schristos #include "cephes_subrl.h"
37*5e0f17b2Schristos 
38*5e0f17b2Schristos #ifdef __weak_alias
__weak_alias(catanl,_catanl)39*5e0f17b2Schristos __weak_alias(catanl, _catanl)
40*5e0f17b2Schristos #endif
41*5e0f17b2Schristos 
42*5e0f17b2Schristos #define MAXNUM LDBL_MAX
43*5e0f17b2Schristos 
44*5e0f17b2Schristos long double complex
45*5e0f17b2Schristos catanl(long double complex z)
46*5e0f17b2Schristos {
47*5e0f17b2Schristos 	long double complex w;
48*5e0f17b2Schristos 	long double a, t, x, x2, y;
49*5e0f17b2Schristos 
50*5e0f17b2Schristos 	x = creall(z);
51*5e0f17b2Schristos 	y = cimagl(z);
52*5e0f17b2Schristos 
53*5e0f17b2Schristos 	if ((x == 0.0L) && (y > 1.0L))
54*5e0f17b2Schristos 		goto ovrf;
55*5e0f17b2Schristos 
56*5e0f17b2Schristos 	x2 = x * x;
57*5e0f17b2Schristos 	a = 1.0L - x2 - (y * y);
58*5e0f17b2Schristos 	if (a == 0.0)
59*5e0f17b2Schristos 		goto ovrf;
60*5e0f17b2Schristos 
61*5e0f17b2Schristos 	t = 0.5L * atan2l(2.0L * x, a);
62*5e0f17b2Schristos 	w = _redupil(t);
63*5e0f17b2Schristos 
64*5e0f17b2Schristos 	t = y - 1.0L;
65*5e0f17b2Schristos 	a = x2 + (t * t);
66*5e0f17b2Schristos 	if (a == 0.0L)
67*5e0f17b2Schristos 		goto ovrf;
68*5e0f17b2Schristos 
69*5e0f17b2Schristos 	t = y + 1.0L;
70*5e0f17b2Schristos 	a = (x2 + (t * t))/a;
71*5e0f17b2Schristos 	w = w + (0.25L * logl(a)) * I;
72*5e0f17b2Schristos 	return w;
73*5e0f17b2Schristos 
74*5e0f17b2Schristos ovrf:
75*5e0f17b2Schristos #if 0
76*5e0f17b2Schristos 	mtherr ("catanl", OVERFLOW);
77*5e0f17b2Schristos #endif
78*5e0f17b2Schristos 	w = MAXNUM + MAXNUM * I;
79*5e0f17b2Schristos 	return w;
80*5e0f17b2Schristos }
81