forked from modelica/ModelicaStandardLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add function to round to nearest integer
- Loading branch information
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
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 <i>round half away from zero</i> rule with the equation: | ||
</p> | ||
<blockquote><pre> | ||
i = <strong>integer</strong>( <strong>floor</strong>( r + 0.5 ) ) for r > 0; | ||
i = <strong>integer</strong>( <strong>ceil</strong>( r - 0.5 ) ) for r < 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 | ||
Math.nearestInteger(0.3999999999999999+0.1); // = 0 | ||
Math.nearestInteger(1.39999999999999999+0.1); // = 1 (errorneous border case, see note below) | ||
</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> | ||
<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. | ||
</p> | ||
</html>")); | ||
end nearestInteger; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ FastFourierTransform | |
Icons | ||
isEqual | ||
isPowerOf2 | ||
nearestInteger | ||
sin | ||
cos | ||
tan | ||
|