diff --git a/AK/Complex.h b/AK/Complex.h index a7fd8300936..e9963d31f62 100644 --- a/AK/Complex.h +++ b/AK/Complex.h @@ -83,7 +83,7 @@ public: template constexpr Complex operator+=(U const& x) { - m_real += x.real(); + m_real += x; return *this; } @@ -98,7 +98,7 @@ public: template constexpr Complex operator-=(U const& x) { - m_real -= x.real(); + m_real -= x; return *this; } @@ -224,7 +224,7 @@ private: // reverse associativity operators for scalars template -constexpr Complex operator+(U const& b, Complex const& a) +constexpr Complex operator+(U const& a, Complex const& b) { Complex x = a; x += b; @@ -232,7 +232,7 @@ constexpr Complex operator+(U const& b, Complex const& a) } template -constexpr Complex operator-(U const& b, Complex const& a) +constexpr Complex operator-(U const& a, Complex const& b) { Complex x = a; x -= b; @@ -240,7 +240,7 @@ constexpr Complex operator-(U const& b, Complex const& a) } template -constexpr Complex operator*(U const& b, Complex const& a) +constexpr Complex operator*(U const& a, Complex const& b) { Complex x = a; x *= b; @@ -248,7 +248,7 @@ constexpr Complex operator*(U const& b, Complex const& a) } template -constexpr Complex operator/(U const& b, Complex const& a) +constexpr Complex operator/(U const& a, Complex const& b) { Complex x = a; x /= b; diff --git a/Tests/AK/TestComplex.cpp b/Tests/AK/TestComplex.cpp index 5079569d180..47574300241 100644 --- a/Tests/AK/TestComplex.cpp +++ b/Tests/AK/TestComplex.cpp @@ -42,3 +42,29 @@ TEST_CASE(Complex) EXPECT_APPROXIMATE(cexp(Complex(0., 1.) * M_PI).real(), -1.); #endif } + +TEST_CASE(real_operators_regression) +{ + { + auto c = Complex(0., 0.); + c += 1; + EXPECT_EQ(c.real(), 1); + } + { + auto c = Complex(0., 0.); + c -= 1; + EXPECT_EQ(c.real(), -1); + } + { + auto c1 = Complex(1., 1.); + auto c2 = 1 - c1; + EXPECT_EQ(c2.real(), 0); + EXPECT_EQ(c2.imag(), -1); + } + { + auto c1 = Complex(1., 1.); + auto c2 = 1 / c1; + EXPECT_EQ(c2.real(), 0.5); + EXPECT_EQ(c2.imag(), -0.5); + } +}