[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:
parent
c9181d3b05
commit
e91d0f0510
|
@ -51,6 +51,10 @@ If the output is not quite correct, check for invisible trailing spaces!
|
||||||
</properties>
|
</properties>
|
||||||
<body>
|
<body>
|
||||||
<release version="3.3" date="TBD" description="TBD">
|
<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">
|
<action dev="erans" type="fix" issue="MATH-1088">
|
||||||
The iterator returned by "MultiDimensionalCounter#iterator()" will now
|
The iterator returned by "MultiDimensionalCounter#iterator()" will now
|
||||||
correctly throw a "NoSuchElementException" when calling "next()" and the
|
correctly throw a "NoSuchElementException" when calling "next()" and the
|
||||||
|
|
|
@ -392,10 +392,11 @@ public class Precision {
|
||||||
*/
|
*/
|
||||||
public static double round(double x, int scale, int roundingMethod) {
|
public static double round(double x, int scale, int roundingMethod) {
|
||||||
try {
|
try {
|
||||||
return (new BigDecimal
|
final double rounded = (new BigDecimal(Double.toString(x))
|
||||||
(Double.toString(x))
|
|
||||||
.setScale(scale, roundingMethod))
|
.setScale(scale, roundingMethod))
|
||||||
.doubleValue();
|
.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) {
|
} catch (NumberFormatException ex) {
|
||||||
if (Double.isInfinite(x)) {
|
if (Double.isInfinite(x)) {
|
||||||
return x;
|
return x;
|
||||||
|
|
|
@ -393,6 +393,9 @@ public class PrecisionTest {
|
||||||
Assert.assertEquals(0.0, Precision.round(0.0, 2), 0.0);
|
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.POSITIVE_INFINITY, Precision.round(Double.POSITIVE_INFINITY, 2), 0.0);
|
||||||
Assert.assertEquals(Double.NEGATIVE_INFINITY, Precision.round(Double.NEGATIVE_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
|
@Test
|
||||||
|
@ -490,7 +493,10 @@ public class PrecisionTest {
|
||||||
Assert.assertEquals(0.0f, Precision.round(0.0f, 2), 0.0f);
|
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.POSITIVE_INFINITY, Precision.round(Float.POSITIVE_INFINITY, 2), 0.0f);
|
||||||
Assert.assertEquals(Float.NEGATIVE_INFINITY, Precision.round(Float.NEGATIVE_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
|
// MATH-1070
|
||||||
Assert.assertEquals(0.0f, Precision.round(0f, 2, BigDecimal.ROUND_UP), 0.0f);
|
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);
|
Assert.assertEquals(0.05f, Precision.round(0.05f, 2, BigDecimal.ROUND_UP), 0.0f);
|
||||||
|
|
Loading…
Reference in New Issue