Mercurial > projects > ldc
comparison dmd/complex_t.h @ 1:c53b6e3fe49a trunk
[svn r5] Initial commit. Most things are very rough.
author | lindquist |
---|---|
date | Sat, 01 Sep 2007 21:43:27 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:a9e71648e74d | 1:c53b6e3fe49a |
---|---|
1 | |
2 // Compiler implementation of the D programming language | |
3 // Copyright (c) 1999-2006 by Digital Mars | |
4 // All Rights Reserved | |
5 // written by Walter Bright and Burton Radons | |
6 // http://www.digitalmars.com | |
7 // License for redistribution is by either the Artistic License | |
8 // in artistic.txt, or the GNU General Public License in gnu.txt. | |
9 // See the included readme.txt for details. | |
10 | |
11 #ifndef DMD_COMPLEX_T_H | |
12 #define DMD_COMPLEX_T_H | |
13 | |
14 /* Roll our own complex type for compilers that don't support complex | |
15 */ | |
16 | |
17 struct complex_t | |
18 { | |
19 long double re; | |
20 long double im; | |
21 | |
22 complex_t() { this->re = 0; this->im = 0; } | |
23 complex_t(long double re) { this->re = re; this->im = 0; } | |
24 complex_t(long double re, long double im) { this->re = re; this->im = im; } | |
25 | |
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 y) { complex_t r; r.re = re - y.re; r.im = im - y.im; return r; } | |
28 complex_t operator - () { complex_t r; r.re = -re; r.im = -im; return r; } | |
29 complex_t operator * (complex_t y) { return complex_t(re * y.re - im * y.im, im * y.re + re * y.im); } | |
30 | |
31 complex_t operator / (complex_t y) | |
32 { | |
33 long double abs_y_re = y.re < 0 ? -y.re : y.re; | |
34 long double abs_y_im = y.im < 0 ? -y.im : y.im; | |
35 long double r, den; | |
36 | |
37 if (abs_y_re < abs_y_im) | |
38 { | |
39 r = y.re / y.im; | |
40 den = y.im + r * y.re; | |
41 return complex_t((re * r + im) / den, | |
42 (im * r - re) / den); | |
43 } | |
44 else | |
45 { | |
46 r = y.im / y.re; | |
47 den = y.re + r * y.im; | |
48 return complex_t((re + r * im) / den, | |
49 (im - r * re) / den); | |
50 } | |
51 } | |
52 | |
53 operator bool () { return re || im; } | |
54 | |
55 int operator == (complex_t y) { return re == y.re && im == y.im; } | |
56 int operator != (complex_t y) { return re != y.re || im != y.im; } | |
57 }; | |
58 | |
59 inline complex_t operator * (long double x, complex_t y) { return complex_t(x) * y; } | |
60 inline complex_t operator * (complex_t x, long double y) { return x * complex_t(y); } | |
61 inline complex_t operator / (complex_t x, long double y) { return x / complex_t(y); } | |
62 | |
63 | |
64 inline long double creall(complex_t x) | |
65 { | |
66 return x.re; | |
67 } | |
68 | |
69 inline long double cimagl(complex_t x) | |
70 { | |
71 return x.im; | |
72 } | |
73 | |
74 #endif |