xref: /netbsd/lib/libm/complex/catanf.c (revision 77a2799b)
1*77a2799bSmrg /* $NetBSD: catanf.c,v 1.2 2011/07/03 06:45:24 mrg Exp $ */
29d8b5fa7Sdrochner 
39d8b5fa7Sdrochner /*-
49d8b5fa7Sdrochner  * Copyright (c) 2007 The NetBSD Foundation, Inc.
59d8b5fa7Sdrochner  * All rights reserved.
69d8b5fa7Sdrochner  *
79d8b5fa7Sdrochner  * This code is derived from software written by Stephen L. Moshier.
89d8b5fa7Sdrochner  * It is redistributed by the NetBSD Foundation by permission of the author.
99d8b5fa7Sdrochner  *
109d8b5fa7Sdrochner  * Redistribution and use in source and binary forms, with or without
119d8b5fa7Sdrochner  * modification, are permitted provided that the following conditions
129d8b5fa7Sdrochner  * are met:
139d8b5fa7Sdrochner  * 1. Redistributions of source code must retain the above copyright
149d8b5fa7Sdrochner  *    notice, this list of conditions and the following disclaimer.
159d8b5fa7Sdrochner  * 2. Redistributions in binary form must reproduce the above copyright
169d8b5fa7Sdrochner  *    notice, this list of conditions and the following disclaimer in the
179d8b5fa7Sdrochner  *    documentation and/or other materials provided with the distribution.
189d8b5fa7Sdrochner  *
199d8b5fa7Sdrochner  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
209d8b5fa7Sdrochner  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
219d8b5fa7Sdrochner  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
229d8b5fa7Sdrochner  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
239d8b5fa7Sdrochner  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
249d8b5fa7Sdrochner  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
259d8b5fa7Sdrochner  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
269d8b5fa7Sdrochner  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
279d8b5fa7Sdrochner  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
289d8b5fa7Sdrochner  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
299d8b5fa7Sdrochner  * POSSIBILITY OF SUCH DAMAGE.
309d8b5fa7Sdrochner  */
319d8b5fa7Sdrochner 
329d8b5fa7Sdrochner #include "../src/namespace.h"
339d8b5fa7Sdrochner #include <complex.h>
349d8b5fa7Sdrochner #include <math.h>
35*77a2799bSmrg #include <float.h>
369d8b5fa7Sdrochner #include "cephes_subrf.h"
379d8b5fa7Sdrochner 
389d8b5fa7Sdrochner #ifdef __weak_alias
__weak_alias(catanf,_catanf)399d8b5fa7Sdrochner __weak_alias(catanf, _catanf)
409d8b5fa7Sdrochner #endif
419d8b5fa7Sdrochner 
42*77a2799bSmrg #define MAXNUMF FLT_MAX
439d8b5fa7Sdrochner 
449d8b5fa7Sdrochner float complex
459d8b5fa7Sdrochner catanf(float complex z)
469d8b5fa7Sdrochner {
479d8b5fa7Sdrochner 	float complex w;
489d8b5fa7Sdrochner 	float a, t, x, x2, y;
499d8b5fa7Sdrochner 
509d8b5fa7Sdrochner 	x = crealf(z);
519d8b5fa7Sdrochner 	y = cimagf(z);
529d8b5fa7Sdrochner 
539d8b5fa7Sdrochner 	if ((x == 0.0f) && (y > 1.0f))
549d8b5fa7Sdrochner 		goto ovrf;
559d8b5fa7Sdrochner 
569d8b5fa7Sdrochner 	x2 = x * x;
579d8b5fa7Sdrochner 	a = 1.0f - x2 - (y * y);
589d8b5fa7Sdrochner 	if (a == 0.0f)
599d8b5fa7Sdrochner 		goto ovrf;
609d8b5fa7Sdrochner 
619d8b5fa7Sdrochner 	t = 0.5f * atan2f(2.0f * x, a);
629d8b5fa7Sdrochner 	w = _redupif(t);
639d8b5fa7Sdrochner 
649d8b5fa7Sdrochner 	t = y - 1.0f;
659d8b5fa7Sdrochner 	a = x2 + (t * t);
669d8b5fa7Sdrochner 	if (a == 0.0f)
679d8b5fa7Sdrochner 		goto ovrf;
689d8b5fa7Sdrochner 
699d8b5fa7Sdrochner 	t = y + 1.0f;
709d8b5fa7Sdrochner 	a = (x2 + (t * t))/a;
719d8b5fa7Sdrochner 	w = w + (0.25f * logf(a)) * I;
729d8b5fa7Sdrochner 	return w;
739d8b5fa7Sdrochner 
749d8b5fa7Sdrochner ovrf:
759d8b5fa7Sdrochner #if 0
769d8b5fa7Sdrochner 	mtherr ("catan", OVERFLOW);
779d8b5fa7Sdrochner #endif
789d8b5fa7Sdrochner 	w = MAXNUMF + MAXNUMF * I;
799d8b5fa7Sdrochner 	return w;
809d8b5fa7Sdrochner }
81