[MATH-1070] Fix Precision.round(float, int, int) for RoundingMode ROUND_UP. Thanks to Oleksandr Muliarevych.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1547649 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart 2013-12-03 23:50:14 +00:00
parent ba3c2201c6
commit 8e5867eda8
3 changed files with 16 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-1070" due-to="Oleksandr Muliarevych">
Fix "Precision#round(float, int, int)" when using rounding mode "BigDecimal.ROUND_UP"
and the discarded fraction is zero.
</action>
<action dev="tn" type="fix" issue="MATH-1059">
Use "FastMath" instead of "Math" within Commons Math.
</action>

View File

@ -491,8 +491,7 @@ public class Precision {
unscaled = FastMath.floor(unscaled);
} else {
// The following equality test is intentional and needed for rounding purposes
if (FastMath.floor(unscaled) / 2.0 == FastMath.floor(Math
.floor(unscaled) / 2.0)) { // even
if (FastMath.floor(unscaled) / 2.0 == FastMath.floor(FastMath.floor(unscaled) / 2.0)) { // even
unscaled = FastMath.floor(unscaled);
} else { // odd
unscaled = FastMath.ceil(unscaled);
@ -516,7 +515,10 @@ public class Precision {
}
break;
case BigDecimal.ROUND_UP :
unscaled = FastMath.ceil(FastMath.nextAfter(unscaled, Double.POSITIVE_INFINITY));
// do not round if the discarded fraction is equal to zero
if (unscaled != FastMath.floor(unscaled)) {
unscaled = FastMath.ceil(FastMath.nextAfter(unscaled, Double.POSITIVE_INFINITY));
}
break;
default :
throw new MathIllegalArgumentException(LocalizedFormats.INVALID_ROUNDING_METHOD,

View File

@ -490,6 +490,13 @@ 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);
// 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);
Assert.assertEquals(0.06f, Precision.round(0.051f, 2, BigDecimal.ROUND_UP), 0.0f);
Assert.assertEquals(0.06f, Precision.round(0.0505f, 2, BigDecimal.ROUND_UP), 0.0f);
Assert.assertEquals(0.06f, Precision.round(0.059f, 2, BigDecimal.ROUND_UP), 0.0f);
}