Skip to content

Add function Math.nearestInteger #4495

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

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions Modelica/Blocks/Math.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1931,7 +1931,7 @@ Otherwise the input angle <code>u</code> is wrapped to the <a href=\"https://en.
</html>"));
end WrapAngle;

block RealToInteger "Convert Real to Integer signal"
block RealToInteger "Convert Real to Integer signal (by rounding away from zero)"
extends Modelica.Blocks.Icons.IntegerBlock;
public
Interfaces.RealInput u "Connector of Real input signal" annotation (
Expand Down Expand Up @@ -1959,7 +1959,7 @@ Otherwise the input angle <code>u</code> is wrapped to the <a href=\"https://en.
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> (by rounding away from zero):
</p>
<blockquote><pre>
y = <strong>integer</strong>( <strong>floor</strong>( u + 0.5 ) ) for u &gt; 0;
Expand Down
43 changes: 43 additions & 0 deletions Modelica/Math/nearestInteger.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
within Modelica.Math;
function nearestInteger "Convert real number to nearest integer value (by rounding away from zero)"
extends Modelica.Icons.Function;

input Real r "Real number to convert to integer";
output Integer i "Integer value, which is nearest to the given real number (rounded away from zero)";

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 <code>r</code> of type Real is converted to the nearest integer value <code>i</code> (by rounding away from zero):
</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>"), GenerateEvents=true);
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