1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 06:47:34 +00:00

AK: Fix bugs in Complex += -= + - * / operators

There were two issues:

1) the C+=R and C-=R operators expected arithmetic types to have .real()

2) the R+C, R-C, R*C and R/C operators applied the operation in wrong
   order (did C+R, C-R, C*R and C/R instead). This wouldn't matter for
   + and * which are commutative, but is incorrect for - and /.
This commit is contained in:
Martin Janiczek 2023-11-23 01:34:40 +01:00 committed by Tim Flynn
parent 963a6b3d3d
commit 58d0577a02
2 changed files with 32 additions and 6 deletions

View file

@ -42,3 +42,29 @@ TEST_CASE(Complex)
EXPECT_APPROXIMATE(cexp(Complex<double>(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);
}
}