xref: /netbsd-src/lib/libm/complex/cproj.c (revision c1d5350b00afa0fac6e8504dd582669e0662e3de)
1*c1d5350bSchristos /*	$NetBSD: cproj.c,v 1.5 2011/11/02 02:34:56 christos Exp $	*/
2e2a86dd3Schristos 
3e2a86dd3Schristos /*-
4e2a86dd3Schristos  * Copyright (c) 2010 The NetBSD Foundation, Inc.
5e2a86dd3Schristos  * All rights reserved.
6e2a86dd3Schristos  *
7e2a86dd3Schristos  * Redistribution and use in source and binary forms, with or without
8e2a86dd3Schristos  * modification, are permitted provided that the following conditions
9e2a86dd3Schristos  * are met:
10e2a86dd3Schristos  * 1. Redistributions of source code must retain the above copyright
11e2a86dd3Schristos  *    notice, this list of conditions and the following disclaimer.
12e2a86dd3Schristos  * 2. Redistributions in binary form must reproduce the above copyright
13e2a86dd3Schristos  *    notice, this list of conditions and the following disclaimer in the
14e2a86dd3Schristos  *    documentation and/or other materials provided with the distribution.
15e2a86dd3Schristos  *
16e2a86dd3Schristos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17e2a86dd3Schristos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18e2a86dd3Schristos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19e2a86dd3Schristos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20e2a86dd3Schristos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21e2a86dd3Schristos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22e2a86dd3Schristos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23e2a86dd3Schristos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24e2a86dd3Schristos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25e2a86dd3Schristos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26e2a86dd3Schristos  * POSSIBILITY OF SUCH DAMAGE.
27e2a86dd3Schristos  */
28e2a86dd3Schristos #include <sys/cdefs.h>
29*c1d5350bSchristos __RCSID("$NetBSD: cproj.c,v 1.5 2011/11/02 02:34:56 christos Exp $");
30e2a86dd3Schristos 
31e2a86dd3Schristos #include <complex.h>
32e2a86dd3Schristos #include <math.h>
33e2a86dd3Schristos 
34e2a86dd3Schristos #include "../src/math_private.h"
35e2a86dd3Schristos 
36e2a86dd3Schristos /*
37e2a86dd3Schristos  * cproj(double complex z)
38e2a86dd3Schristos  *
39e2a86dd3Schristos  * These functions return the value of the projection (not stereographic!)
40e2a86dd3Schristos  * onto the Riemann sphere.
41e2a86dd3Schristos  *
42e2a86dd3Schristos  * z projects to z, except that all complex infinities (even those with one
43e2a86dd3Schristos  * infinite part and one NaN part) project to positive infinity on the real axis.
44e2a86dd3Schristos  * If z has an infinite part, then cproj(z) shall be equivalent to:
45e2a86dd3Schristos  *
46e2a86dd3Schristos  * INFINITY + I * copysign(0.0, cimag(z))
47e2a86dd3Schristos  */
48e2a86dd3Schristos double complex
cproj(double complex z)49e2a86dd3Schristos cproj(double complex z)
50e2a86dd3Schristos {
51e2a86dd3Schristos 	double_complex w = { .z = z };
52e2a86dd3Schristos 
53*c1d5350bSchristos 	/*CONSTCOND*/
54cf7b1c6bSchristos 	if (isinf(creal(z)) || isinf(cimag(z))) {
55fda2d7caSchristos #ifdef __INFINITY
56*c1d5350bSchristos 		REAL_PART(w) = HUGE_VAL;
57fda2d7caSchristos #else
58e2a86dd3Schristos 		REAL_PART(w) = INFINITY;
59fda2d7caSchristos #endif
60e2a86dd3Schristos 		IMAG_PART(w) = copysign(0.0, cimag(z));
61e2a86dd3Schristos 	}
62e2a86dd3Schristos 
63e2a86dd3Schristos 	return (w.z);
64e2a86dd3Schristos }
65