Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit 2f863d0

Browse files
perostOpenModelica-Hudson
authored andcommitted
[NF] Make Real range generation more robust.
- Generate Real ranges using start+step*i, instead of relying on the code generation that uses repeated addition which causes unnecessary inaccuracies. Belonging to [master]: - #2909
1 parent 0f5c4be commit 2f863d0

1 file changed

Lines changed: 28 additions & 2 deletions

File tree

Compiler/NFFrontEnd/NFCeval.mo

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ algorithm
686686

687687
case (Expression.REAL(), Expression.REAL(), Expression.REAL())
688688
algorithm
689-
expl := list(Expression.REAL(r) for r in start.value:step.value:stop.value);
689+
expl := evalRangeReal(start.value, step.value, stop.value);
690690
then
691691
(Type.REAL(), expl);
692692

@@ -706,7 +706,7 @@ algorithm
706706

707707
case (Expression.REAL(), Expression.REAL())
708708
algorithm
709-
expl := list(Expression.REAL(r) for r in start.value:stop.value);
709+
expl := evalRangeReal(start.value, 1.0, stop.value);
710710
then
711711
(Type.REAL(), expl);
712712

@@ -734,6 +734,32 @@ algorithm
734734
expl, literal = true);
735735
end evalRange;
736736

737+
function evalRangeReal
738+
input Real start;
739+
input Real step;
740+
input Real stop;
741+
output list<Expression> result;
742+
protected
743+
Integer steps;
744+
algorithm
745+
steps := Util.realRangeSize(start, step, stop);
746+
747+
// Real ranges are tricky, make sure that start and stop are reproduced
748+
// exactly if they are part of the range.
749+
if steps == 0 then
750+
result := {};
751+
return;
752+
elseif steps == 1 then
753+
result := {Expression.REAL(start)};
754+
else
755+
result := {Expression.REAL(stop)};
756+
for i in steps-1:-1:1 loop
757+
result := Expression.REAL(start + i * step) :: result;
758+
end for;
759+
result := Expression.REAL(start) :: result;
760+
end if;
761+
end evalRangeReal;
762+
737763
function printFailedEvalError
738764
input String name;
739765
input Expression exp;

0 commit comments

Comments
 (0)