Gracefully handle division by zero when attempting equation normalization

Instead of crashing the preprocessor (because DataTree::DivisionByZeroException
is not caught), just abort the normalization and mark it as failed.

Ref. #92
master
Sébastien Villemot 2023-09-28 15:32:38 +02:00
parent dec60b25e6
commit 86b24dc9bf
No known key found for this signature in database
GPG Key ID: 2CECE9350ECEBE4A
1 changed files with 27 additions and 6 deletions

View File

@ -5029,10 +5029,17 @@ BinaryOpNode::normalizeEquationHelper(const set<expr_t> &contain_var, expr_t rhs
the symbolic Jacobian has been used as a last resort, but this indicates
a problem in the matching which is beyond our control at this point). */
case BinaryOpcode::times:
if (arg1_contains_var)
rhs = datatree.AddDivide(rhs, arg2);
else
rhs = datatree.AddDivide(rhs, arg1);
try
{
if (arg1_contains_var)
rhs = datatree.AddDivide(rhs, arg2);
else
rhs = datatree.AddDivide(rhs, arg1);
}
catch (DataTree::DivisionByZeroException)
{
throw NormalizationFailed{};
}
break;
case BinaryOpcode::divide:
if (arg1_contains_var)
@ -5041,7 +5048,14 @@ BinaryOpNode::normalizeEquationHelper(const set<expr_t> &contain_var, expr_t rhs
/* Transforming a/x=RHS into x=a/RHS is incorrect if RHS=0. However,
per the same reasoning as for the multiplication case above, it
nevertheless makes sense to do the transformation. */
rhs = datatree.AddDivide(arg1, rhs);
try
{
rhs = datatree.AddDivide(arg1, rhs);
}
catch (DataTree::DivisionByZeroException)
{
throw NormalizationFailed{};
}
break;
case BinaryOpcode::power:
if (arg1_contains_var)
@ -5053,7 +5067,14 @@ BinaryOpNode::normalizeEquationHelper(const set<expr_t> &contain_var, expr_t rhs
throw NormalizationFailed();
else
// a^x=RHS is normalized in x=ln(RHS)/ln(a)
rhs = datatree.AddDivide(datatree.AddLog(rhs), datatree.AddLog(arg1));
try
{
rhs = datatree.AddDivide(datatree.AddLog(rhs), datatree.AddLog(arg1));
}
catch (DataTree::DivisionByZeroException)
{
throw NormalizationFailed{};
}
break;
case BinaryOpcode::equal:
cerr << "BinaryOpCode::normalizeEquationHelper: this case should not happen" << endl;