Skip to content

Commit

Permalink
Guard against some domain errors for trig functions
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@23220 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Nov 5, 2014
1 parent ab77fa6 commit e0e9c63
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
8 changes: 8 additions & 0 deletions Compiler/FrontEnd/Ceval.mo
Expand Up @@ -1273,11 +1273,13 @@ algorithm

case ("acos",{Values.REAL(real = rv)},_)
equation
true = rv >= -1.0 and rv <= 1.0;
rv_1 = realAcos(rv);
then
Values.REAL(rv_1);
case ("asin",{Values.REAL(real = rv)},_)
equation
true = rv >= -1.0 and rv <= 1.0;
rv_1 = realAsin(rv);
then
Values.REAL(rv_1);
Expand Down Expand Up @@ -1308,11 +1310,13 @@ algorithm
Values.REAL(rv_1);
case ("log",{Values.REAL(real = rv)},_)
equation
true = rv > 0;
rv_1 = realLn(rv);
then
Values.REAL(rv_1);
case ("log10",{Values.REAL(real = rv)},_)
equation
true = rv > 0;
rv_1 = realLog10(rv);
then
Values.REAL(rv_1);
Expand Down Expand Up @@ -3145,6 +3149,7 @@ algorithm
case (cache,env,{exp},impl,st,msg,_)
equation
(cache,Values.REAL(rv),_) = ceval(cache,env, exp, impl, st,msg,numIter+1);
true = rv > 0; // TODO: Print error-message?
rv_1 = realLn(rv);
then
(cache,Values.REAL(rv_1),st);
Expand Down Expand Up @@ -3176,6 +3181,7 @@ algorithm
case (cache,env,{exp},impl,st,msg,_)
equation
(cache,Values.REAL(rv),_) = ceval(cache,env, exp, impl, st,msg,numIter+1);
true = rv > 0; // TODO: Print error-message?
rv_1 = realLog10(rv);
then
(cache,Values.REAL(rv_1),st);
Expand Down Expand Up @@ -3274,6 +3280,7 @@ algorithm
case (cache,env,{exp},impl,st,msg,_)
equation
(cache,Values.REAL(rv),_) = ceval(cache,env, exp, impl, st,msg,numIter+1);
true = rv >= -1.0 and rv <= 1.0;
rv_1 = realAsin(rv);
then
(cache,Values.REAL(rv_1),st);
Expand Down Expand Up @@ -3306,6 +3313,7 @@ algorithm
case (cache,env,{exp},impl,st,msg,_)
equation
(cache,Values.REAL(rv),_) = ceval(cache,env, exp, impl, st,msg,numIter+1);
true = rv >= -1.0 and rv <= 1.0;
rv_1 = realAcos(rv);
then
(cache,Values.REAL(rv_1),st);
Expand Down
14 changes: 11 additions & 3 deletions Compiler/FrontEnd/ExpressionSimplify.mo
Expand Up @@ -1528,12 +1528,16 @@ algorithm
// sin function
case("asin",DAE.CALL(expLst={e}))
equation
r = realAsin(Expression.getRealConst(e));
r = Expression.getRealConst(e);
true = r >= -1.0 and r <= 1.0;
r = realAsin(r);
then DAE.RCONST(r);

// cos function
case("acos",DAE.CALL(expLst={e}))
equation
r = Expression.getRealConst(e);
true = r >= -1.0 and r <= 1.0;
r = realAcos(Expression.getRealConst(e));
then DAE.RCONST(r);

Expand All @@ -1554,14 +1558,18 @@ algorithm
// log function
case("log",DAE.CALL(expLst={e}))
equation
r = realLn(Expression.getRealConst(e));
r = Expression.getRealConst(e);
true = r > 0;
r = realLn(r);
then
DAE.RCONST(r);

// log10 function
case("log10",DAE.CALL(expLst={e}))
equation
r = realLog10(Expression.getRealConst(e));
r = Expression.getRealConst(e);
true = r > 0;
r = realLog10(r);
then
DAE.RCONST(r);

Expand Down
18 changes: 18 additions & 0 deletions Compiler/Template/CodegenC.tpl
Expand Up @@ -8964,6 +8964,24 @@ template daeExpCall(Exp call, Context context, Text &preExp, Text &varDecls, Tex
let &preExp += '<%tmp%> = <%argStr%>;<%retPre%>'
'log10(<%tmp%>)'

case CALL(path=IDENT(name="acos"), expLst={e1}, attr=attr as CALL_ATTR(__)) then
let argStr = daeExp(e1, context, &preExp, &varDecls, &auxFunction)
let tmp = tempDecl("modelica_real",&varDecls)
let ass = '(<%tmp%> >= -1.0 && <%tmp%> <= 1.0)'
let &preExpMsg = buffer ""
let retPre = assertCommonVar(ass,'"Model error: Argument of <%Util.escapeModelicaStringToCString(printExpStr(call))%> outside the domain -1.0 <= %g <= 1.0", <%tmp%>', context, &preExpMsg, &varDecls, dummyInfo)
let &preExp += '<%tmp%> = <%argStr%>;<%retPre%>'
'acos(<%tmp%>)'

case CALL(path=IDENT(name="asin"), expLst={e1}, attr=attr as CALL_ATTR(__)) then
let argStr = daeExp(e1, context, &preExp, &varDecls, &auxFunction)
let tmp = tempDecl("modelica_real",&varDecls)
let ass = '(<%tmp%> >= -1.0 && <%tmp%> <= 1.0)'
let &preExpMsg = buffer ""
let retPre = assertCommonVar(ass,'"Model error: Argument of <%Util.escapeModelicaStringToCString(printExpStr(call))%> outside the domain -1.0 <= %g <= 1.0", <%tmp%>', context, &preExpMsg, &varDecls, dummyInfo)
let &preExp += '<%tmp%> = <%argStr%>;<%retPre%>'
'asin(<%tmp%>)'

/* Begin code generation of event triggering math functions */

case CALL(path=IDENT(name="div"), expLst={e1,e2, index}, attr=CALL_ATTR(ty = ty)) then
Expand Down

0 comments on commit e0e9c63

Please sign in to comment.