Skip to content

Commit

Permalink
support for loops over Real ranges
Browse files Browse the repository at this point in the history
See Modelica.Electrical.Digital.Examples.RAM (even if this is certainly a bad example for the feature).
Attempt to avoid rounding errors for cases like 1.1:0.1:2.5

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@25899 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
rfranke authored and sjoelund committed May 4, 2015
1 parent a3052d4 commit 778d3b3
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions Compiler/Template/CodegenCpp.tpl
Expand Up @@ -14605,6 +14605,8 @@ template algStmtForRange_impl(Exp range, Ident iterator, String type, String sho
::=
match range
case RANGE(__) then
let type = expTypeArray(ty)
let iterVar = tempDecl('int', &varDecls)
let iterName = contextIteratorName(iterator, context)
let startVar = tempDecl(type, &varDecls)
let stepVar = tempDecl(type, &varDecls)
Expand All @@ -14614,7 +14616,7 @@ case RANGE(__) then
let stepValue = match step case SOME(eo) then
daeExp(eo, context, &preExp, &varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
else
"(1)"
'1'
let stopValue = daeExp(stop, context, &preExp, &varDecls,simCode, &extraFuncs ,&extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
<<
<%preExp%>
Expand All @@ -14627,13 +14629,12 @@ case RANGE(__) then
else if(!(((<%stepVar%> > 0) && (<%startVar%> > <%stopVar%>)) || ((<%stepVar%> < 0) && (<%startVar%> < <%stopVar%>))))
{
<%type%> <%iterName%>;
//half-open range

BOOST_FOREACH(<%iterName%>,boost::irange( <%startValue%>,(int)<%stopValue%>+1,(int)<%stepVar%>))
{

int <%iterVar%>_end = <%if stringEq(type, "int") then
'(<%stopVar%> - <%startVar%>) / <%stepVar%>;'
else '(int)((<%stopVar%> - <%startVar%>) / <%stepVar%> + 1e-10);'%>
for (<%iterVar%> = 0; <%iterVar%> <= <%iterVar%>_end; <%iterVar%>++) {
<%iterName%> = <%startVar%> + <%iterVar%> * <%stepVar%>;
<%body%>

}
}
>> /* else we're looping over a zero-length range */
Expand Down

0 comments on commit 778d3b3

Please sign in to comment.