[MATH-1089] Precision.round(double, ...) will return negative zero for negative values rounded to zero.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1558933 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart 2014-01-16 22:11:00 +00:00
parent c9181d3b05
commit e91d0f0510
3 changed files with 14 additions and 3 deletions

View File

@ -51,6 +51,10 @@ If the output is not quite correct, check for invisible trailing spaces!
</properties>
<body>
<release version="3.3" date="TBD" description="TBD">
<action dev="tn" type="fix" issue="MATH-1089">
"Precision#round(double, ...)" will now return negative zero for negative
values rounded to zero, similar to the float variant.
</action>
<action dev="erans" type="fix" issue="MATH-1088">
The iterator returned by "MultiDimensionalCounter#iterator()" will now
correctly throw a "NoSuchElementException" when calling "next()" and the

View File

@ -392,10 +392,11 @@ public class Precision {
*/
public static double round(double x, int scale, int roundingMethod) {
try {
return (new BigDecimal
(Double.toString(x))
final double rounded = (new BigDecimal(Double.toString(x))
.setScale(scale, roundingMethod))
.doubleValue();
// MATH-1089: negative values rounded to zero should result in negative zero
return rounded == 0.0 ? rounded * FastMath.copySign(1d, x) : rounded;
} catch (NumberFormatException ex) {
if (Double.isInfinite(x)) {
return x;

View File

@ -393,6 +393,9 @@ public class PrecisionTest {
Assert.assertEquals(0.0, Precision.round(0.0, 2), 0.0);
Assert.assertEquals(Double.POSITIVE_INFINITY, Precision.round(Double.POSITIVE_INFINITY, 2), 0.0);
Assert.assertEquals(Double.NEGATIVE_INFINITY, Precision.round(Double.NEGATIVE_INFINITY, 2), 0.0);
// comparison of positive and negative zero is not possible -> always equal thus do string comparison
Assert.assertEquals("-0.0", Double.toString(Precision.round(-0.0, 0)));
Assert.assertEquals("-0.0", Double.toString(Precision.round(-1e-10, 0)));
}
@Test
@ -490,7 +493,10 @@ public class PrecisionTest {
Assert.assertEquals(0.0f, Precision.round(0.0f, 2), 0.0f);
Assert.assertEquals(Float.POSITIVE_INFINITY, Precision.round(Float.POSITIVE_INFINITY, 2), 0.0f);
Assert.assertEquals(Float.NEGATIVE_INFINITY, Precision.round(Float.NEGATIVE_INFINITY, 2), 0.0f);
// comparison of positive and negative zero is not possible -> always equal thus do string comparison
Assert.assertEquals("-0.0", Float.toString(Precision.round(-0.0f, 0)));
Assert.assertEquals("-0.0", Float.toString(Precision.round(-1e-10f, 0)));
// MATH-1070
Assert.assertEquals(0.0f, Precision.round(0f, 2, BigDecimal.ROUND_UP), 0.0f);
Assert.assertEquals(0.05f, Precision.round(0.05f, 2, BigDecimal.ROUND_UP), 0.0f);