1 /** 2 * Implements a complex number type. 3 * 4 * Copyright: Copyright (C) 1999-2020 by The D Language Foundation, All Rights Reserved 5 * Authors: $(LINK2 http://www.digitalmars.com, Walter Bright) 6 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) 7 * Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/complex.d, _complex.d) 8 * Documentation: https://dlang.org/phobos/dmd_complex.html 9 * Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/complex.d 10 */ 11 12 module dmd.complex; 13 14 import dmd.root.ctfloat; 15 16 extern (C++) struct complex_t 17 { 18 real_t re; 19 real_t im; 20 21 this() @disable; 22 23 this(real_t re) 24 { 25 this(re, CTFloat.zero); 26 } 27 28 this(real_t re, real_t im) 29 { 30 this.re = re; 31 this.im = im; 32 } 33 34 extern (D) complex_t opBinary(string op)(complex_t y) 35 if (op == "+") 36 { 37 return complex_t(re + y.re, im + y.im); 38 } 39 40 extern (D) complex_t opBinary(string op)(complex_t y) 41 if (op == "-") 42 { 43 return complex_t(re - y.re, im - y.im); 44 } 45 46 extern (D) complex_t opUnary(string op)() 47 if (op == "-") 48 { 49 return complex_t(-re, -im); 50 } 51 52 extern (D) complex_t opBinary(string op)(complex_t y) 53 if (op == "*") 54 { 55 return complex_t(re * y.re - im * y.im, im * y.re + re * y.im); 56 } 57 58 extern (D) complex_t opBinaryRight(string op)(real_t x) 59 if (op == "*") 60 { 61 return complex_t(x) * this; 62 } 63 64 extern (D) complex_t opBinary(string op)(real_t y) 65 if (op == "*") 66 { 67 return this * complex_t(y); 68 } 69 70 extern (D) complex_t opBinary(string op)(real_t y) 71 if (op == "/") 72 { 73 return this / complex_t(y); 74 } 75 76 extern (D) complex_t opBinary(string op)(complex_t y) 77 if (op == "/") 78 { 79 if (CTFloat.fabs(y.re) < CTFloat.fabs(y.im)) 80 { 81 const r = y.re / y.im; 82 const den = y.im + r * y.re; 83 return complex_t((re * r + im) / den, (im * r - re) / den); 84 } 85 else 86 { 87 const r = y.im / y.re; 88 const den = y.re + r * y.im; 89 return complex_t((re + r * im) / den, (im - r * re) / den); 90 } 91 } 92 93 extern (D) bool opCast(T : bool)() const 94 { 95 return re || im; 96 } 97 98 int opEquals(complex_t y) const 99 { 100 return re == y.re && im == y.im; 101 } 102 } 103 104 extern (C++) real_t creall(complex_t x) 105 { 106 return x.re; 107 } 108 109 extern (C++) real_t cimagl(complex_t x) 110 { 111 return x.im; 112 }