Refactoring VFormula::Eval().

Plus, in case of check If zero set value to Error instead of "0". This confuses
users.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2018-02-22 15:03:11 +02:00
parent 5f3ee65501
commit ced8963041

View File

@ -234,53 +234,35 @@ void VFormula::Eval()
{ {
return; return;
} }
if (formula.isEmpty())
{
value = tr("Error"); value = tr("Error");
_error = true; _error = true;
dValue = 0; dValue = 0;
} qreal result = 0;
else
if (not formula.isEmpty())
{ {
try try
{ {
QScopedPointer<Calculator> cal(new Calculator()); QScopedPointer<Calculator> cal(new Calculator());
QString expression = qApp->TrVars()->FormulaFromUser(formula, qApp->Settings()->GetOsSeparator()); const QString expression = qApp->TrVars()->FormulaFromUser(formula, qApp->Settings()->GetOsSeparator());
const qreal result = cal->EvalFormula(data->DataVariables(), expression); result = cal->EvalFormula(data->DataVariables(), expression);
if (qIsInf(result) || qIsNaN(result))
{
value = tr("Error");
_error = true;
dValue = 0;
}
else
{
//if result equal 0
if (checkZero && qFuzzyIsNull(result))
{
value = QString("0");
_error = true;
dValue = 0;
}
else
{
dValue = result;
value = QString(qApp->LocaleToString(result) + " " + postfix);
_error = false;
}
}
} }
catch (qmu::QmuParserError &e) catch (qmu::QmuParserError &e)
{ {
value = tr("Error");
_error = true;
dValue = 0;
qDebug() << "\nMath parser error:\n" qDebug() << "\nMath parser error:\n"
<< "--------------------------------------\n" << "--------------------------------------\n"
<< "Message: " << e.GetMsg() << "\n" << "Message: " << e.GetMsg() << "\n"
<< "Expression: " << e.GetExpr() << "\n" << "Expression: " << e.GetExpr() << "\n"
<< "--------------------------------------"; << "--------------------------------------";
return;
}
if (not qIsInf(result) && not qIsNaN(result) && not (checkZero && qFuzzyIsNull(result)))
{
dValue = result;
value = qApp->LocaleToString(result) + QLatin1Char(' ') + postfix;
_error = false;
} }
} }
} }