Skip to content

Commit

Permalink
Can use Modelica.Math.nearestInteger
Browse files Browse the repository at this point in the history
  • Loading branch information
beutlich committed Apr 11, 2020
1 parent cd85b11 commit aaaee45
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
8 changes: 4 additions & 4 deletions Modelica/Blocks/Math.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1904,7 +1904,7 @@ Otherwise the input angle <code>u</code> is wrapped to the interval [0,2*pi[.
Interfaces.IntegerOutput y "Connector of Integer output signal" annotation (
Placement(transformation(extent={{100,-10},{120,10}})));
equation
y = if (u > 0) then integer(floor(u + 0.5)) else integer(ceil(u - 0.5));
y = Modelica.Math.nearestInteger(u);
annotation (Icon(coordinateSystem(
preserveAspectRatio=true,
extent={{-100.0,-100.0},{100.0,100.0}}), graphics={
Expand All @@ -1924,11 +1924,11 @@ Otherwise the input angle <code>u</code> is wrapped to the interval [0,2*pi[.
30.0,-10.0},{30.0,-20.0},{50.0,0.0}})}), Documentation(info="<html>
<p>
This block computes the output <strong>y</strong>
as <em>nearest integer value</em> of the input <strong>u</strong>:
as <em>nearest integer value</em> of the input <strong>u</strong> utilizing the
function <a href=\"modelica://Modelica.Math.nearestInteger\">nearestInteger</a>:
</p>
<blockquote><pre>
y = <strong>integer</strong>( <strong>floor</strong>( u + 0.5 ) ) for u &gt; 0;
y = <strong>integer</strong>( <strong>ceil </strong>( u - 0.5 ) ) for u &lt; 0;
y = Modelica.Math.<strong>nearestInteger</strong>(u);
</pre></blockquote>
</html>"));
end RealToInteger;
Expand Down
12 changes: 6 additions & 6 deletions Modelica/Math/nearestInteger.mo
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ Math.<strong>nearestInteger</strong>(r);

<h4>Description</h4>
<p>
The input value \"r\" of type Real is converted to the closest Integer value \"i\",
using the <em>round half away from zero</em> rule with the equation:
The input value \"r\" of type Real is converted to the closest Integer value \"i\",
using the <em>round half away from zero</em> rule with the equation:
</p>
<blockquote><pre>
i = <strong>integer</strong>( <strong>floor</strong>( r + 0.5 ) ) for r &gt; 0;
Expand All @@ -39,12 +39,12 @@ Math.nearestInteger(1.39999999999999999+0.1); // = 1 (erroneous border case, see
<h4>Note</h4>

<p>
This function does the same conversion as the block
<a href=\"modelica://Modelica.Blocks.Math.RealToInteger\">RealToInteger</a>.
This function does the same conversion as the block
<a href=\"modelica://Modelica.Blocks.Math.RealToInteger\">RealToInteger</a>.
</p>
<p>
The underlying equation is simple, but not always correct. Due to floating point arithmetic some border cases
are not converted correct, like shown in the example above.
The underlying equation is simple, but not always correct. Due to floating point arithmetic some border cases
are not converted correct, like shown in the example above.
</p>
</html>"));
end nearestInteger;

0 comments on commit aaaee45

Please sign in to comment.