diff --git a/src/main/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructure.java b/src/main/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructure.java index 54a206ff5..2f0270217 100644 --- a/src/main/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructure.java +++ b/src/main/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructure.java @@ -386,6 +386,22 @@ public class DerivativeStructure implements FieldElement, S FastMath.floor(data[0])); } + /** Get the whole number that is the nearest to the instance, or the even one if x is exactly half way between two integers. + * @return a double number r such that r is an integer r - 0.5 <= this <= r + 0.5 + */ + public DerivativeStructure rint() { + return new DerivativeStructure(compiler.getFreeParameters(), + compiler.getOrder(), + FastMath.rint(data[0])); + } + + /** Get the closest long to instance value. + * @return closest long to {@link #getValue()} + */ + public long round() { + return FastMath.round(data[0]); + } + /** * Returns the instance with the sign of the argument. * A NaN {@code sign} argument is treated as positive. diff --git a/src/test/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructureTest.java b/src/test/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructureTest.java index 762186162..60135c523 100644 --- a/src/test/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructureTest.java +++ b/src/test/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructureTest.java @@ -838,7 +838,7 @@ public class DerivativeStructureTest { } @Test - public void testCeilFloor() { + public void testCeilFloorRintLong() { DerivativeStructure x = new DerivativeStructure(1, 1, 0, -1.5); Assert.assertEquals(-1.5, x.getPartialDerivative(0), 1.0e-15); @@ -847,6 +847,10 @@ public class DerivativeStructureTest { Assert.assertEquals(+0.0, x.ceil().getPartialDerivative(1), 1.0e-15); Assert.assertEquals(-2.0, x.floor().getPartialDerivative(0), 1.0e-15); Assert.assertEquals(+0.0, x.floor().getPartialDerivative(1), 1.0e-15); + Assert.assertEquals(-2.0, x.rint().getPartialDerivative(0), 1.0e-15); + Assert.assertEquals(+0.0, x.rint().getPartialDerivative(1), 1.0e-15); + Assert.assertEquals(-2.0, x.subtract(x.getField().getOne()).rint().getPartialDerivative(0), 1.0e-15); + Assert.assertEquals(-1l, x.round()); }