OSDN Git Service

2005-05-04 Thomas Koenig <Thomas.Koenig@online.de>
[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 (libgfortran).
6
7 Libgfortran is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file.  (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
19 executable.)
20
21 Libgfortran is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 GNU General Public License for more details.
25
26 You should have received a copy of the GNU General Public
27 License along with libgfortran; see the file COPYING.  If not,
28 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
29 Boston, MA 02111-1307, USA.  */
30 #include <math.h>
31 #include "libgfortran.h"
32
33
34 /* z = a + ib  */
35 /* Absolute value.  */
36 GFC_REAL_8
37 cabs (GFC_COMPLEX_8 z)
38 {
39   return hypot (REALPART (z), IMAGPART (z));
40 }
41
42 /* Complex argument.  The angle made with the +ve real axis.
43    Range -pi-pi.  */
44 GFC_REAL_8
45 carg (GFC_COMPLEX_8 z)
46 {
47   GFC_REAL_8 arg;
48
49   return atan2 (IMAGPART (z), REALPART (z));
50 }
51
52 /* exp(z) = exp(a)*(cos(b) + isin(b))  */
53 GFC_COMPLEX_8
54 cexp (GFC_COMPLEX_8 z)
55 {
56   GFC_REAL_8 a;
57   GFC_REAL_8 b;
58   GFC_COMPLEX_8 v;
59
60   a = REALPART (z);
61   b = IMAGPART (z);
62   COMPLEX_ASSIGN (v, cos (b), sin (b));
63   return exp (a) * v;
64 }
65
66 /* log(z) = log (cabs(z)) + i*carg(z)  */
67 GFC_COMPLEX_8
68 clog (GFC_COMPLEX_8 z)
69 {
70   GFC_COMPLEX_8 v;
71
72   COMPLEX_ASSIGN (v, log (cabs (z)), carg (z));
73   return v;
74 }
75
76 /* log10(z) = log10 (cabs(z)) + i*carg(z)  */
77 GFC_COMPLEX_8
78 clog10 (GFC_COMPLEX_8 z)
79 {
80   GFC_COMPLEX_8 v;
81
82   COMPLEX_ASSIGN (v, log10 (cabs (z)), carg (z));
83   return v;
84 }
85
86 /* pow(base, power) = cexp (power * clog (base))  */
87 GFC_COMPLEX_8
88 cpow (GFC_COMPLEX_8 base, GFC_COMPLEX_8 power)
89 {
90   return cexp (power * clog (base));
91 }
92
93 /* sqrt(z).  Algorithm pulled from glibc.  */
94 GFC_COMPLEX_8
95 csqrt (GFC_COMPLEX_8 z)
96 {
97   GFC_REAL_8 re;
98   GFC_REAL_8 im;
99   GFC_COMPLEX_8 v;
100
101   re = REALPART (z);
102   im = IMAGPART (z);
103   if (im == 0.0)
104     {
105       if (re < 0.0)
106         {
107           COMPLEX_ASSIGN (v, 0.0, copysign (sqrt (-re), im));
108         }
109       else
110         {
111           COMPLEX_ASSIGN (v, fabs (sqrt (re)),
112                           copysign (0.0, im));
113         }
114     }
115   else if (re == 0.0)
116     {
117       GFC_REAL_8 r;
118
119       r = sqrt (0.5 * fabs (im));
120
121       COMPLEX_ASSIGN (v, copysign (r, im), r);
122     }
123   else
124     {
125       GFC_REAL_8 d, r, s;
126
127       d = hypot (re, im);
128       /* Use the identity   2  Re res  Im res = Im x
129          to avoid cancellation error in  d +/- Re x.  */
130       if (re > 0)
131         {
132           r = sqrt (0.5 * d + 0.5 * re);
133           s = (0.5 * im) / r;
134         }
135       else
136         {
137           s = sqrt (0.5 * d - 0.5 * re);
138           r = fabs ((0.5 * im) / s);
139         }
140
141       COMPLEX_ASSIGN (v, r, copysign (s, im));
142     }
143   return v;
144 }
145