Skip to content

Commit

Permalink
Improve handling of exponentiation (#11659)
Browse files Browse the repository at this point in the history
- Disable simplification `x^0.5 => sqrt(x)` when `x` is negative in the
  old simplification module.
- Change the check added during code generation for a 0.5 exponent to
  use the same `Invalid root` error message as for other exponents
  instead of mentioning `sqrt`, since that's an implementation detail
  that only obscures the actual issue.
  • Loading branch information
perost committed Dec 1, 2023
1 parent eae5dc9 commit a178fcf
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/FrontEnd/Expression.mo
Expand Up @@ -4087,7 +4087,7 @@ algorithm
then expPow(makeDiv(e4,e3), negate(e2));

// x^0.5 => sqrt(x)
case (_, _) guard(isHalf(e2))
case (_, _) guard(isHalf(e2) and isPositiveOrZero(e1))
then Expression.makePureBuiltinCall("sqrt",{e1},DAE.T_REAL_DEFAULT);

else equation
Expand Down
6 changes: 5 additions & 1 deletion OMCompiler/Compiler/Template/CodegenCFunctions.tpl
Expand Up @@ -5736,7 +5736,11 @@ case BINARY(__) then
let &preExp +=
<<
<%tmp%> = <%e1%>;
<%assertCommonVar('<%tmp%> >= 0.0', '"Model error: Argument of sqrt(<%Util.escapeModelicaStringToCString(cstr)%>) was %g should be >= 0", <%tmp%>', context, &varDecls, dummyInfo)%>
if(<%tmp%> < 0.0) {
<%if acceptMetaModelicaGrammar()
then '<%generateThrow()%>;<%\n%>'
else 'throwStreamPrint(threadData, "%s:%d: Invalid root: (%g)^(%g)", __FILE__, __LINE__, <%tmp%>, 0.5);<%\n%>'%>
}
>>
'sqrt(<%tmp%>)'
else match realExpIntLit(exp2)
Expand Down

0 comments on commit a178fcf

Please sign in to comment.