mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-01-19 00:37:25 +01:00
[calculation] Fix Calculation
This commit is contained in:
@@ -13,29 +13,14 @@ Calculation::Calculation() :
|
||||
m_inputText(),
|
||||
m_exactOutputText(),
|
||||
m_approximateOutputText(),
|
||||
m_input(nullptr),
|
||||
m_exactOutput(nullptr),
|
||||
m_approximateOutput(nullptr),
|
||||
m_input(),
|
||||
m_exactOutput(),
|
||||
m_approximateOutput(),
|
||||
m_height(-1),
|
||||
m_equalSign(EqualSign::Unknown)
|
||||
{
|
||||
}
|
||||
|
||||
Calculation::~Calculation() {
|
||||
if (m_input != nullptr) {
|
||||
delete m_input;
|
||||
m_input = nullptr;
|
||||
}
|
||||
if (m_exactOutput != nullptr) {
|
||||
delete m_exactOutput;
|
||||
m_exactOutput = nullptr;
|
||||
}
|
||||
if (m_approximateOutput != nullptr) {
|
||||
delete m_approximateOutput;
|
||||
m_approximateOutput = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
Calculation& Calculation::operator=(const Calculation& other) {
|
||||
const char * otherInputText = other.m_inputText;
|
||||
const char * otherExactOutputText = other.m_exactOutputText;
|
||||
@@ -115,18 +100,15 @@ const char * Calculation::approximateOutputText() {
|
||||
return m_approximateOutputText;
|
||||
}
|
||||
|
||||
Expression * Calculation::input() {
|
||||
if (m_input == nullptr) {
|
||||
Expression Calculation::input() {
|
||||
if (!m_input.isDefined()) {
|
||||
m_input = Expression::parse(m_inputText);
|
||||
}
|
||||
return m_input;
|
||||
}
|
||||
|
||||
LayoutRef Calculation::createInputLayout() {
|
||||
if (input() != nullptr) {
|
||||
return input()->createLayout(Preferences::PrintFloatMode::Decimal, PrintFloat::k_numberOfStoredSignificantDigits);
|
||||
}
|
||||
return LayoutRef(nullptr);
|
||||
return input().createLayout(Preferences::PrintFloatMode::Decimal, PrintFloat::k_numberOfStoredSignificantDigits);
|
||||
}
|
||||
|
||||
bool Calculation::isEmpty() {
|
||||
@@ -144,7 +126,8 @@ bool Calculation::isEmpty() {
|
||||
}
|
||||
|
||||
void Calculation::tidy() {
|
||||
if (m_input != nullptr) {
|
||||
// TODO: we might want to do something here? To empty the pool?
|
||||
/* if (m_input != nullptr) {
|
||||
delete m_input;
|
||||
}
|
||||
m_input = nullptr;
|
||||
@@ -155,52 +138,41 @@ void Calculation::tidy() {
|
||||
if (m_approximateOutput != nullptr) {
|
||||
delete m_approximateOutput;
|
||||
}
|
||||
m_approximateOutput = nullptr;
|
||||
m_approximateOutput = nullptr;*/
|
||||
m_height = -1;
|
||||
m_equalSign = EqualSign::Unknown;
|
||||
}
|
||||
|
||||
Expression * Calculation::exactOutput(Context * context) {
|
||||
if (m_exactOutput == nullptr) {
|
||||
if (!m_exactOutput.isDefined()) {
|
||||
/* Because the angle unit might have changed, we do not simplify again. We
|
||||
* thereby avoid turning cos(Pi/4) into sqrt(2)/2 and displaying
|
||||
* 'sqrt(2)/2 = 0.999906' (which is totally wrong) instead of
|
||||
* 'cos(pi/4) = 0.999906' (which is true in degree). */
|
||||
m_exactOutput = Expression::parse(m_exactOutputText);
|
||||
if (m_exactOutput == nullptr) {
|
||||
m_exactOutput = new Undefined();
|
||||
if (!m_exactOutput.isDefined()) {
|
||||
m_exactOutput = Undefined();
|
||||
}
|
||||
}
|
||||
return m_exactOutput;
|
||||
}
|
||||
|
||||
LayoutRef Calculation::createExactOutputLayout(Context * context) {
|
||||
if (exactOutput(context) != nullptr) {
|
||||
return PoincareHelpers::CreateLayout(exactOutput(context));
|
||||
}
|
||||
return LayoutRef(nullptr);
|
||||
return PoincareHelpers::CreateLayout(exactOutput(context));
|
||||
}
|
||||
|
||||
Expression * Calculation::approximateOutput(Context * context) {
|
||||
if (m_approximateOutput == nullptr) {
|
||||
if (!m_approximateOutput.isDefined()) {
|
||||
/* To ensure that the expression 'm_output' is a matrix or a complex, we
|
||||
* call 'evaluate'. */
|
||||
Expression * exp = Expression::parse(m_approximateOutputText);
|
||||
if (exp != nullptr) {
|
||||
m_approximateOutput = PoincareHelpers::Approximate<double>(exp, *context);
|
||||
delete exp;
|
||||
} else {
|
||||
m_approximateOutput = new Undefined();
|
||||
}
|
||||
Expression exp = Expression::parse(m_approximateOutputText);
|
||||
m_approximateOutput = PoincareHelpers::Approximate<double>(exp, *context);
|
||||
}
|
||||
return m_approximateOutput;
|
||||
}
|
||||
|
||||
LayoutRef Calculation::createApproximateOutputLayout(Context * context) {
|
||||
if (approximateOutput(context) != nullptr) {
|
||||
return PoincareHelpers::CreateLayout(approximateOutput(context));
|
||||
}
|
||||
return LayoutRef(nullptr);
|
||||
return PoincareHelpers::CreateLayout(approximateOutput(context));
|
||||
}
|
||||
|
||||
bool Calculation::shouldOnlyDisplayApproximateOutput(Context * context) {
|
||||
@@ -210,14 +182,14 @@ bool Calculation::shouldOnlyDisplayApproximateOutput(Context * context) {
|
||||
if (strcmp(m_exactOutputText, "undef") == 0) {
|
||||
return true;
|
||||
}
|
||||
return input()->isApproximate(*context);
|
||||
return input().isApproximate(*context);
|
||||
}
|
||||
|
||||
Calculation::EqualSign Calculation::exactAndApproximateDisplayedOutputsAreEqual(Poincare::Context * context) {
|
||||
if (m_equalSign != EqualSign::Unknown) {
|
||||
return m_equalSign;
|
||||
}
|
||||
m_equalSign = exactOutput(context)->isEqualToItsApproximationLayout(approximateOutput(context), k_printedExpressionSize, Preferences::sharedPreferences()->angleUnit(), Preferences::sharedPreferences()->displayMode(), Preferences::sharedPreferences()->numberOfSignificantDigits(), *context) ? EqualSign::Equal : EqualSign::Approximation;
|
||||
m_equalSign = exactOutput(context).isEqualToItsApproximationLayout(approximateOutput(context), k_printedExpressionSize, Preferences::sharedPreferences()->angleUnit(), Preferences::sharedPreferences()->displayMode(), Preferences::sharedPreferences()->numberOfSignificantDigits(), *context) ? EqualSign::Equal : EqualSign::Approximation;
|
||||
return m_equalSign;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user