diff --git a/Modelica/Blocks/Math.mo b/Modelica/Blocks/Math.mo index 0ffb0e28fd..e9baf7c22b 100644 --- a/Modelica/Blocks/Math.mo +++ b/Modelica/Blocks/Math.mo @@ -1931,7 +1931,7 @@ Otherwise the input angle u is wrapped to the ")); 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 ( @@ -1959,7 +1959,7 @@ Otherwise the input angle u is wrapped to the 0) then integer(floor(r + 0.5)) else integer(ceil(r - 0.5)); + + annotation (Documentation(info=" + +

Syntax

+
+Math.nearestInteger(r);
+
+ +

Description

+

+The input value r of type Real is converted to the nearest integer value i (by rounding away from zero): +

+
+i = integer( floor( r + 0.5 ) )  for  r > 0;
+i = integer( ceil ( r - 0.5 ) )  for  r < 0;
+
+ +

Example

+
+import Modelica.Math;
+Math.nearestInteger(0.4);                     // = 0
+Math.nearestInteger(0.5);                     // = 1
+Math.nearestInteger(-0.4);                    // = 0
+Math.nearestInteger(-0.5);                    // = -1
+
+ +

Note

+ +

+This function does the same conversion as the block +RealToInteger. +

+"), GenerateEvents=true); +end nearestInteger; diff --git a/Modelica/Math/package.order b/Modelica/Math/package.order index c1f671ddc0..2e18c9335a 100644 --- a/Modelica/Math/package.order +++ b/Modelica/Math/package.order @@ -10,6 +10,7 @@ FastFourierTransform Icons isEqual isPowerOf2 +nearestInteger sin cos tan diff --git a/ModelicaTest/Math.mo b/ModelicaTest/Math.mo index 26d07b7cc9..df9d43d692 100644 --- a/ModelicaTest/Math.mo +++ b/ModelicaTest/Math.mo @@ -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;