1 `/* Complex exponential functions
2 Copyright 2002, 2004 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
5 This file is part of the GNU Fortran 95 runtime library (libgfor).
7 Libgfortran is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 Libgfortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with libgfor; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include "libgfortran.h"'
24 include(`mtype.m4')dnl
29 cabs`'q (complex_type z)
31 return hypot`'q (REALPART (z), IMAGPART (z));
34 /* Complex argument. The angle made with the +ve real axis.
37 carg`'q (complex_type z)
41 return atan2`'q (IMAGPART (z), REALPART (z));
44 /* exp(z) = exp(a)*(cos(b) + isin(b)) */
46 cexp`'q (complex_type z)
54 COMPLEX_ASSIGN (v, cos`'q (b), sin`'q (b));
55 return exp`'q (a) * v;
58 /* log(z) = log (cabs(z)) + i*carg(z) */
60 clog`'q (complex_type z)
64 COMPLEX_ASSIGN (v, log`'q (cabs`'q (z)), carg`'q (z));
68 /* log10(z) = log10 (cabs(z)) + i*carg(z) */
70 clog10`'q (complex_type z)
74 COMPLEX_ASSIGN (v, log10`'q (cabs`'q (z)), carg`'q (z));
78 /* pow(base, power) = cexp (power * clog (base)) */
80 cpow`'q (complex_type base, complex_type power)
82 return cexp`'q (power * clog`'q (base));
85 /* sqrt(z). Algorithm pulled from glibc. */
87 csqrt`'q (complex_type z)
99 COMPLEX_ASSIGN (v, 0.0, copysign`'q (sqrt`'q (-re), im));
103 COMPLEX_ASSIGN (v, fabs`'q (sqrt (re)),
104 copysign`'q (0.0, im));
111 r = sqrt`'q (0.5 * fabs (im));
113 COMPLEX_ASSIGN (v, copysign`'q (r, im), r);
119 d = hypot`'q (re, im);
120 /* Use the identity 2 Re res Im res = Im x
121 to avoid cancellation error in d +/- Re x. */
124 r = sqrt`'q (0.5 * d + 0.5 * re);
129 s = sqrt`'q (0.5 * d - 0.5 * re);
130 r = fabs`'q ((0.5 * im) / s);
133 COMPLEX_ASSIGN (v, r, copysign`'q (s, im));