﻿id	summary	reporter	owner	description	type	status	priority	milestone	component	version	resolution	keywords	cc
4868	Issue with Integer-Real conversions in min/max functions with the NF	Francesco Casella	Per Östlund	"Please check the simulation results of [https://libraries.openmodelica.org/branches/newInst/Modelica_3.2.2/files/Modelica_3.2.2_Modelica.Electrical.Analog.Examples.OpAmps.Comparator.diff.opAmp.out.v.html Modelica.Electrical.Analog.Examples.OpAmps.Comparator]. The output of the comparator remains constant instead of being a square wave.

The root cause of the problem is that instead of having {{{Rn = Rp = 500}}} we get {{{Rp = 1000, Rn = 0}}}.

The equations in the source code are
{{{
Rp = R*(1 + alpha*(T_heatPort - T_ref))*(1 - min(1, max(0, rInt)));
Rn = R*(1 + alpha*(T_heatPort - T_ref))*min(1, max(0, rInt));
}}}

The corresponding flattened equations with the NF are
{{{
potentiometer.Rp = potentiometer.R * (1.0 + potentiometer.alpha * (potentiometer.T_heatPort - potentiometer.T_ref)) *
  /*Real*/(1 - min(1, max(0, potentiometer.rInt)));
potentiometer.Rn = potentiometer.R * (1.0 + potentiometer.alpha * (potentiometer.T_heatPort - potentiometer.T_ref)) *
  /*Real*/(min(1, max(0, potentiometer.rInt)));
}}}
while the old FE gives:
{{{
potentiometer.Rp = potentiometer.R * (1.0 + potentiometer.alpha * (potentiometer.T_heatPort - potentiometer.T_ref)) *
  (1.0 - min(1.0, max(0.0, potentiometer.rInt)));
potentiometer.Rn = potentiometer.R * (1.0 + potentiometer.alpha * (potentiometer.T_heatPort - potentiometer.T_ref)) *
  min(1.0, max(0.0, potentiometer.rInt));
}}}
Apparently, the NF casts the Real argument of min and max to Integer rather casting the Integer argument to Real, and later casts the result back to Real, leading to a completely wrong result.

According to the specification 10.3.4, ""min(x,y) returns the least element of the scalars x and y; as defined by <"", so I guess when comparing a Real and an Integer, the latter should be cast to the former, and not the other way round."	defect	closed	high	2.0.0	New Instantiation		fixed		
