1*84d9c625SLionel Sambuc /* $NetBSD: cproj.c,v 1.5 2011/11/02 02:34:56 christos Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*-
42fe8fb19SBen Gras * Copyright (c) 2010 The NetBSD Foundation, Inc.
52fe8fb19SBen Gras * All rights reserved.
62fe8fb19SBen Gras *
72fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
82fe8fb19SBen Gras * modification, are permitted provided that the following conditions
92fe8fb19SBen Gras * are met:
102fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
112fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
122fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
132fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
142fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
152fe8fb19SBen Gras *
162fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
172fe8fb19SBen Gras * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
182fe8fb19SBen Gras * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
192fe8fb19SBen Gras * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
202fe8fb19SBen Gras * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
212fe8fb19SBen Gras * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
222fe8fb19SBen Gras * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
232fe8fb19SBen Gras * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
242fe8fb19SBen Gras * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
252fe8fb19SBen Gras * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
262fe8fb19SBen Gras * POSSIBILITY OF SUCH DAMAGE.
272fe8fb19SBen Gras */
282fe8fb19SBen Gras #include <sys/cdefs.h>
29*84d9c625SLionel Sambuc __RCSID("$NetBSD: cproj.c,v 1.5 2011/11/02 02:34:56 christos Exp $");
302fe8fb19SBen Gras
312fe8fb19SBen Gras #include <complex.h>
322fe8fb19SBen Gras #include <math.h>
332fe8fb19SBen Gras
342fe8fb19SBen Gras #include "../src/math_private.h"
352fe8fb19SBen Gras
362fe8fb19SBen Gras /*
372fe8fb19SBen Gras * cproj(double complex z)
382fe8fb19SBen Gras *
392fe8fb19SBen Gras * These functions return the value of the projection (not stereographic!)
402fe8fb19SBen Gras * onto the Riemann sphere.
412fe8fb19SBen Gras *
422fe8fb19SBen Gras * z projects to z, except that all complex infinities (even those with one
432fe8fb19SBen Gras * infinite part and one NaN part) project to positive infinity on the real axis.
442fe8fb19SBen Gras * If z has an infinite part, then cproj(z) shall be equivalent to:
452fe8fb19SBen Gras *
462fe8fb19SBen Gras * INFINITY + I * copysign(0.0, cimag(z))
472fe8fb19SBen Gras */
482fe8fb19SBen Gras double complex
cproj(double complex z)492fe8fb19SBen Gras cproj(double complex z)
502fe8fb19SBen Gras {
512fe8fb19SBen Gras double_complex w = { .z = z };
522fe8fb19SBen Gras
53*84d9c625SLionel Sambuc /*CONSTCOND*/
54*84d9c625SLionel Sambuc if (isinf(creal(z)) || isinf(cimag(z))) {
552fe8fb19SBen Gras #ifdef __INFINITY
56*84d9c625SLionel Sambuc REAL_PART(w) = HUGE_VAL;
572fe8fb19SBen Gras #else
582fe8fb19SBen Gras REAL_PART(w) = INFINITY;
592fe8fb19SBen Gras #endif
602fe8fb19SBen Gras IMAG_PART(w) = copysign(0.0, cimag(z));
612fe8fb19SBen Gras }
622fe8fb19SBen Gras
632fe8fb19SBen Gras return (w.z);
642fe8fb19SBen Gras }
65