Author: tn
Date: Tue Dec  3 23:50:14 2013
New Revision: 1547649

URL: http://svn.apache.org/r1547649
Log:
[MATH-1070] Fix Precision.round(float, int, int) for RoundingMode ROUND_UP. 
Thanks to  Oleksandr Muliarevych.

Modified:
    commons/proper/math/trunk/src/changes/changes.xml
    
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/Precision.java
    
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/PrecisionTest.java
   (contents, props changed)

Modified: commons/proper/math/trunk/src/changes/changes.xml
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/changes/changes.xml?rev=1547649&r1=1547648&r2=1547649&view=diff
==============================================================================
--- commons/proper/math/trunk/src/changes/changes.xml (original)
+++ commons/proper/math/trunk/src/changes/changes.xml Tue Dec  3 23:50:14 2013
@@ -51,6 +51,10 @@ If the output is not quite correct, chec
   </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>

Modified: 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/Precision.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/Precision.java?rev=1547649&r1=1547648&r2=1547649&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/Precision.java
 (original)
+++ 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/Precision.java
 Tue Dec  3 23:50:14 2013
@@ -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,

Modified: 
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/PrecisionTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/PrecisionTest.java?rev=1547649&r1=1547648&r2=1547649&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/PrecisionTest.java
 (original)
+++ 
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/PrecisionTest.java
 Tue Dec  3 23:50:14 2013
@@ -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);
     }
 
 

Propchange: 
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/PrecisionTest.java
------------------------------------------------------------------------------
    svn:keywords = Id Revision


Reply via email to