mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 23:17:46 +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:
parent
963a6b3d3d
commit
58d0577a02
2 changed files with 32 additions and 6 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue