Fixed some lexing problems with DOS line-endings
[delight/core.git] / dmd / complex_t.h
blob1ed0b5fe444286da17567b95c4e42c72eb0bd1dd
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.
11 #ifndef DMD_COMPLEX_T_H
12 #define DMD_COMPLEX_T_H
14 /* Roll our own complex type for compilers that don't support complex
17 struct complex_t
19 long double re;
20 long double im;
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; }
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); }
31 complex_t operator / (complex_t y)
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;
37 if (abs_y_re < abs_y_im)
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);
44 else
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);
53 operator bool () { return re || im; }
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; }
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); }
64 inline long double creall(complex_t x)
66 return x.re;
69 inline long double cimagl(complex_t x)
71 return x.im;
74 #endif