OSDN Git Service

2004-05-21 Frank Ch. Eigler <fche@redhat.com>
[pf3gnuchains/gcc-fork.git] / libgfortran / generated / exp_c8.c
1 /* Complex exponential functions
2    Copyright 2002, 2004 Free Software Foundation, Inc.
3    Contributed by Paul Brook <paul@nowt.org>
4
5 This file is part of the GNU Fortran 95 runtime library (libgfor).
6
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.
11
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.
16
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.  */
21 #include <math.h>
22 #include "libgfortran.h"
23
24
25 /* z = a + ib  */
26 /* Absolute value.  */
27 GFC_REAL_8
28 cabs (GFC_COMPLEX_8 z)
29 {
30   return hypot (REALPART (z), IMAGPART (z));
31 }
32
33 /* Complex argument.  The angle made with the +ve real axis.  Range 0-2pi.  */
34 GFC_REAL_8
35 carg (GFC_COMPLEX_8 z)
36 {
37   GFC_REAL_8 arg;
38
39   arg = atan2 (IMAGPART (z), REALPART (z));
40   if (arg < 0)
41     return arg + 2 * M_PI;
42   else
43     return arg;
44 }
45
46 /* exp(z) = exp(a)*(cos(b) + isin(b))  */
47 GFC_COMPLEX_8
48 cexp (GFC_COMPLEX_8 z)
49 {
50   GFC_REAL_8 a;
51   GFC_REAL_8 b;
52   GFC_COMPLEX_8 v;
53
54   a = REALPART (z);
55   b = IMAGPART (z);
56   COMPLEX_ASSIGN (v, cos (b), sin (b));
57   return exp (a) * v;
58 }
59
60 /* log(z) = log (cabs(z)) + i*carg(z)  */
61 GFC_COMPLEX_8
62 clog (GFC_COMPLEX_8 z)
63 {
64   GFC_COMPLEX_8 v;
65
66   COMPLEX_ASSIGN (v, log (cabs (z)), carg (z));
67   return v;
68 }
69
70 /* log10(z) = log10 (cabs(z)) + i*carg(z)  */
71 GFC_COMPLEX_8
72 clog10 (GFC_COMPLEX_8 z)
73 {
74   GFC_COMPLEX_8 v;
75
76   COMPLEX_ASSIGN (v, log10 (cabs (z)), carg (z));
77   return v;
78 }
79
80 /* pow(base, power) = cexp (power * clog (base))  */
81 GFC_COMPLEX_8
82 cpow (GFC_COMPLEX_8 base, GFC_COMPLEX_8 power)
83 {
84   return cexp (power * clog (base));
85 }
86
87 /* sqrt(z).  Algorithm pulled from glibc.  */
88 GFC_COMPLEX_8
89 csqrt (GFC_COMPLEX_8 z)
90 {
91   GFC_REAL_8 re;
92   GFC_REAL_8 im;
93   GFC_COMPLEX_8 v;
94
95   re = REALPART (z);
96   im = IMAGPART (z);
97   if (im == 0.0)
98     {
99       if (re < 0.0)
100         {
101           COMPLEX_ASSIGN (v, 0.0, copysign (sqrt (-re), im));
102         }
103       else
104         {
105           COMPLEX_ASSIGN (v, fabs (sqrt (re)),
106                           copysign (0.0, im));
107         }
108     }
109   else if (re == 0.0)
110     {
111       GFC_REAL_8 r;
112
113       r = sqrt (0.5 * fabs (im));
114
115       COMPLEX_ASSIGN (v, copysign (r, im), r);
116     }
117   else
118     {
119       GFC_REAL_8 d, r, s;
120
121       d = hypot (re, im);
122       /* Use the identity   2  Re res  Im res = Im x
123          to avoid cancellation error in  d +/- Re x.  */
124       if (re > 0)
125         {
126           r = sqrt (0.5 * d + 0.5 * re);
127           s = (0.5 * im) / r;
128         }
129       else
130         {
131           s = sqrt (0.5 * d - 0.5 * re);
132           r = fabs ((0.5 * im) / s);
133         }
134
135       COMPLEX_ASSIGN (v, r, copysign (s, im));
136     }
137   return v;
138 }
139