Added rint and round to DerivativeStructure.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1373780 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2012-08-16 10:07:42 +00:00
parent 486224fd36
commit 7b090c5eec
2 changed files with 21 additions and 1 deletions

View File

@ -386,6 +386,22 @@ public class DerivativeStructure implements FieldElement<DerivativeStructure>, 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.

View File

@ -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());
}