1 /**
2  * Compiler implementation of the
3  * $(LINK2 http://www.dlang.org, D programming language).
4  *
5  * Copyright:   Copyright (c) 1999-2016 by Digital Mars, All Rights Reserved
6  * Authors:     $(LINK2 http://www.digitalmars.com, Walter Bright)
7  * License:     $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
8  * Source:      $(DMDSRC _complex.d)
9  */
10 
11 module ddmd.complex;
12 
13 import ddmd.root.ctfloat;
14 
15 struct complex_t
16 {
17     real_t re;
18     real_t im;
19 
20     this() @disable;
21 
22     this(real_t re)
23     {
24         this(re, CTFloat.zero);
25     }
26 
27     this(real_t re, real_t im)
28     {
29         this.re = re;
30         this.im = im;
31     }
32 
33     complex_t opAdd(complex_t y)
34     {
35         return complex_t(re + y.re, im + y.im);
36     }
37 
38     complex_t opSub(complex_t y)
39     {
40         return complex_t(re - y.re, im - y.im);
41     }
42 
43     complex_t opNeg()
44     {
45         return complex_t(-re, -im);
46     }
47 
48     complex_t opMul(complex_t y)
49     {
50         return complex_t(re * y.re - im * y.im, im * y.re + re * y.im);
51     }
52 
53     complex_t opMul_r(real_t x)
54     {
55         return complex_t(x) * this;
56     }
57 
58     complex_t opMul(real_t y)
59     {
60         return this * complex_t(y);
61     }
62 
63     complex_t opDiv(real_t y)
64     {
65         return this / complex_t(y);
66     }
67 
68     complex_t opDiv(complex_t y)
69     {
70         if (CTFloat.fabs(y.re) < CTFloat.fabs(y.im))
71         {
72             const r = y.re / y.im;
73             const den = y.im + r * y.re;
74             return complex_t((re * r + im) / den, (im * r - re) / den);
75         }
76         else
77         {
78             const r = y.im / y.re;
79             const den = y.re + r * y.im;
80             return complex_t((re + r * im) / den, (im - r * re) / den);
81         }
82     }
83 
84     bool opCast(T : bool)()
85     {
86         return re || im;
87     }
88 
89     int opEquals(complex_t y)
90     {
91         return re == y.re && im == y.im;
92     }
93 }
94 
95 extern (C++) real_t creall(complex_t x)
96 {
97     return x.re;
98 }
99 
100 extern (C++) real_t cimagl(complex_t x)
101 {
102     return x.im;
103 }