Author: tn
Date: Thu Jan 16 22:11:00 2014
New Revision: 1558933

URL: http://svn.apache.org/r1558933
Log:
[MATH-1089] Precision.round(double, ...) will return negative zero for negative 
values rounded to zero.

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

Modified: commons/proper/math/trunk/src/changes/changes.xml
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/changes/changes.xml?rev=1558933&r1=1558932&r2=1558933&view=diff
==============================================================================
--- commons/proper/math/trunk/src/changes/changes.xml (original)
+++ commons/proper/math/trunk/src/changes/changes.xml Thu Jan 16 22:11:00 2014
@@ -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-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

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=1558933&r1=1558932&r2=1558933&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
 Thu Jan 16 22:11:00 2014
@@ -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;

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=1558933&r1=1558932&r2=1558933&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
 Thu Jan 16 22:11:00 2014
@@ -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);


Reply via email to