root/branches/tango/rebuild/complex_t.h

Revision 589, 2.1 kB (checked in by Gregor, 2 years ago)

MERGE: trunk r588

Line 
1 // Compiler implementation of the D programming language
2 // Copyright (c) 1999-2006 by Digital Mars
3 // All Rights Reserved
4 // written by Walter Bright and Burton Radons
5 // http://www.digitalmars.com
6 // License for redistribution is by either the Artistic License
7 // in artistic.txt, or the GNU General Public License in gnu.txt.
8 // See the included readme.txt for details.
9
10 #ifndef DMD_COMPLEX_T_H
11 #define DMD_COMPLEX_T_H
12
13 /* Roll our own complex type for compilers that don't support complex
14  */
15
16 struct complex_t
17 {
18     long double re;
19     long double im;   
20
21     complex_t() { this->re = 0; this->im = 0; }
22     complex_t(long double re) { this->re = re; this->im = 0; }
23     complex_t(long double re, long double im) { this->re = re; this->im = im; }
24
25     complex_t operator + (complex_t y) { complex_t r; r.re = re + y.re; r.im = im + y.im; return r; }
26     complex_t operator - (complex_t y) { complex_t r; r.re = re - y.re; r.im = im - y.im; return r; }
27     complex_t operator - () { complex_t r; r.re = -re; r.im = -im; return r; }
28     complex_t operator * (complex_t y) { return complex_t(re * y.re - im * y.im, im * y.re + re * y.im); }
29    
30     complex_t operator / (complex_t y)
31     {
32     long double abs_y_re = y.re < 0 ? -y.re : y.re;
33     long double abs_y_im = y.im < 0 ? -y.im : y.im;
34     long double r, den;
35
36     if (abs_y_re < abs_y_im)
37     {
38         r = y.re / y.im;
39         den = y.im + r * y.re;
40         return complex_t((re * r + im) / den,
41                  (im * r - re) / den);
42     }
43     else
44     {
45         r = y.im / y.re;
46         den = y.re + r * y.im;
47         return complex_t((re + r * im) / den,
48                  (im - r * re) / den);
49     }
50     }
51
52     operator bool () { return re || im; }
53
54     int operator == (complex_t y) { return re == y.re && im == y.im; }
55     int operator != (complex_t y) { return re != y.re || im != y.im; }
56 };
57
58 inline complex_t operator * (long double x, complex_t y) { return complex_t(x) * y; }
59 inline complex_t operator * (complex_t x, long double y) { return x * complex_t(y); }
60 inline complex_t operator / (complex_t x, long double y) { return x / complex_t(y); }
61
62
63 inline long double creall(complex_t x)
64 {
65     return x.re;
66 }
67
68 inline long double cimagl(complex_t x)
69 {
70     return x.im;
71 }
72
73 #endif
Note: See TracBrowser for help on using the browser.