Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function Math.nearestInteger #4495

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions Modelica/Math/nearestInteger.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
within Modelica.Math;
function nearestInteger "Convert real number to nearest integer value"
extends Modelica.Icons.Function;

input Real r "Real number to convert to integer";
output Integer i "Integer value, which is closest to the given real number";

algorithm
i :=if (r > 0) then integer(floor(r + 0.5)) else integer(ceil(r - 0.5));

annotation (Documentation(info="<html>

<h4>Syntax</h4>
<blockquote><pre>
Math.<strong>nearestInteger</strong>(r);
</pre></blockquote>

<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:
</p>
<blockquote><pre>
i = <strong>integer</strong>( <strong>floor</strong>( r + 0.5 ) ) for r &gt; 0;
i = <strong>integer</strong>( <strong>ceil</strong>( r - 0.5 ) ) for r &lt; 0;
</pre></blockquote>

<h4>Example</h4>
<blockquote><pre>
import Modelica.Math;
Math.nearestInteger(0.4); // = 0
Math.nearestInteger(0.5); // = 1
Math.nearestInteger(-0.4); // = 0
Math.nearestInteger(-0.5); // = -1
</pre></blockquote>

<h4>Note</h4>

<p>
This function does the same conversion as the block
<a href=\"modelica://Modelica.Blocks.Math.RealToInteger\">RealToInteger</a>.
</p>
</html>"));
end nearestInteger;
1 change: 1 addition & 0 deletions Modelica/Math/package.order
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ FastFourierTransform
Icons
isEqual
isPowerOf2
nearestInteger
sin
cos
tan
Expand Down
8 changes: 8 additions & 0 deletions ModelicaTest/Math.mo
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ extends Modelica.Icons.ExamplesPackage;
assert(Math.isPowerOf2(1), "isPowerOf2(1) is wrong");
assert(Math.isPowerOf2(4), "isPowerOf2(4) is wrong");
assert(not Math.isPowerOf2(9), "isPowerOf2(9) is wrong");

assert(Math.nearestInteger(1.4999)==1, "nearestInteger(1.4999) failed");
assert(Math.nearestInteger(1.5)==2, "nearestInteger(1.5) failed");
assert(Math.nearestInteger(-1.4999)==-1, "nearestInteger(-1.4999) failed");
assert(Math.nearestInteger(-1.5)==-2, "nearestInteger(-1.5) failed");
// Test deactivated - would fail with current implementation
// assert(Math.nearestInteger(1.49999999999999999999)==1, "nearestInteger border case failed");

ok:=true;
end ScalarFunctions;

Expand Down