Accept "is not" for "!is"
[delight/core.git] / d-gcc-complex_t.h
blobeacb3ed403d581cbd2faf96509611dacc0a99180
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 // 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 /* NOTE: This file has been patched from the original DMD distribution to
12 work with the GDC compiler.
14 Modified by David Friedman, September 2004
16 Same as DMD complex_t.h, but use GCC's REAL_VALUE_TYPE-based real_t
17 instead of long double.
20 #ifndef DMD_COMPLEX_T_H
21 #define DMD_COMPLEX_T_H
23 /* Roll our own complex type for compilers that don't support complex
27 struct complex_t
29 real_t re;
30 real_t im;
32 complex_t() { this->re = 0; this->im = 0; }
33 complex_t(real_t re) { this->re = re; this->im = 0; }
34 complex_t(real_t re, real_t im) { this->re = re; this->im = im; }
36 complex_t operator + (complex_t y) { complex_t r; r.re = re + y.re; r.im = im + y.im; return r; }
37 complex_t operator - (complex_t y) { complex_t r; r.re = re - y.re; r.im = im - y.im; return r; }
38 complex_t operator - () { complex_t r; r.re = -re; r.im = -im; return r; }
39 complex_t operator * (complex_t y) { return complex_t(re * y.re - im * y.im, im * y.re + re * y.im); }
41 complex_t operator / (complex_t y)
43 real_t abs_y_re = y.re.isNegative() ? -y.re : y.re;
44 real_t abs_y_im = y.im.isNegative() ? -y.im : y.im;
45 real_t r, den;
47 if (abs_y_re < abs_y_im)
49 r = y.re / y.im;
50 den = y.im + r * y.re;
51 return complex_t((re * r + im) / den,
52 (im * r - re) / den);
54 else
56 r = y.im / y.re;
57 den = y.re + r * y.im;
58 return complex_t((re + r * im) / den,
59 (im - r * re) / den);
63 operator bool () { return !re.isZero() || !im.isZero(); }
65 int operator == (complex_t y) { return re == y.re && im == y.im; }
66 int operator != (complex_t y) { return re != y.re || im != y.im; }
69 inline complex_t operator * (real_t x, complex_t y) { return complex_t(x) * y; }
70 inline complex_t operator * (complex_t x, real_t y) { return x * complex_t(y); }
71 inline complex_t operator / (complex_t x, real_t y) { return x / complex_t(y); }
74 inline real_t creall(complex_t x)
76 return x.re;
79 inline real_t cimagl(complex_t x)
81 return x.im;
84 #endif