Skip to content

Commit

Permalink
Fix precise of complex numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Aug 26, 2023
1 parent de7c14b commit f65e47f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/fintamath/expressions/IExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "fintamath/literals/Variable.hpp"
#include "fintamath/literals/constants/IConstant.hpp"
#include "fintamath/literals/constants/Undefined.hpp"
#include "fintamath/numbers/Complex.hpp"
#include "fintamath/numbers/INumber.hpp"
#include "fintamath/numbers/Real.hpp"

Expand Down Expand Up @@ -115,7 +116,13 @@ void IExpression::preciseSimplifyChild(ArgumentPtr &child) {
child = exprChild->preciseSimplify();
}
else if (const auto numChild = cast<INumber>(child)) {
child = convert<Real>(*numChild);
// TODO! use multimethod
if (const auto complexChild = cast<Complex>(numChild)) {
child = Complex(*convert<Real>(complexChild->real()), *convert<Real>(complexChild->imag())).clone();
}
else {
child = convert<Real>(*numChild);
}
}
else if (const auto constChild = cast<IConstant>(child)) {
child = (*constChild)();
Expand Down
2 changes: 1 addition & 1 deletion src/fintamath/numbers/Complex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ std::string Complex::toString() const {
isImNeg = true;
}

if (imStr == "1") {
if (*im == Integer(1) || *im == Integer(-1)) {
imStr.clear();
}
else {
Expand Down
13 changes: 13 additions & 0 deletions tests/src/expressions/ExpressionTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1598,6 +1598,19 @@ TEST(ExpressionTests, preciseTest) {
EXPECT_EQ(Expression("(1/1000000000000000000000000000000000000000)!!").precise().toString(), "(1.0*10^-39)!!");
EXPECT_EQ(Expression("(-1)!!").precise().toString(), "(-1.0)!!");

EXPECT_EQ(Expression("I").precise().toString(), "I");
EXPECT_EQ(Expression("5 + I").precise().toString(), "5.0 + I");
EXPECT_EQ(Expression("5 I").precise().toString(), "5.0 I");
EXPECT_EQ(Expression("5 + 5 I").precise().toString(), "5.0 + 5.0 I");
EXPECT_EQ(Expression("5/I").precise().toString(), "-5.0 I");
EXPECT_EQ(Expression("E/I").precise().toString(), "-2.7182818284590452353602874713526624977572470936999595749669676277240766303535476 I");
EXPECT_EQ(Expression("-I").precise().toString(), "-I");
EXPECT_EQ(Expression("5 - I").precise().toString(), "5.0 - I");
EXPECT_EQ(Expression("5 -I").precise().toString(), "5.0 - I");
EXPECT_EQ(Expression("5 - 5 I").precise().toString(), "5.0 - 5.0 I");
EXPECT_EQ(Expression("5/-I").precise().toString(), "5.0 I");
EXPECT_EQ(Expression("E/-I").precise().toString(), "2.7182818284590452353602874713526624977572470936999595749669676277240766303535476 I");

EXPECT_EQ(Expression("10^10000").precise(8).toString(), "1.0*10^10000");
EXPECT_EQ(Expression("x+E").precise(8).toString(), "x + 2.7182818");
EXPECT_EQ(Expression("x^(100!)").precise(8).toString(), "x^(9.3326215*10^157)");
Expand Down

0 comments on commit f65e47f

Please sign in to comment.