You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by lu...@apache.org on 2011/05/05 21:41:28 UTC

svn commit: r1099938 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/dfp/Dfp.java site/xdoc/changes.xml test/java/org/apache/commons/math/dfp/DfpTest.java

Author: luc
Date: Thu May  5 19:41:28 2011
New Revision: 1099938

URL: http://svn.apache.org/viewvc?rev=1099938&view=rev
Log:
Fixed conversion problems to/from 0 in Decimal Floating Point (Dfp) class.

JIRA: MATH-567

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/dfp/Dfp.java
    commons/proper/math/trunk/src/site/xdoc/changes.xml
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/dfp/DfpTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/dfp/Dfp.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/dfp/Dfp.java?rev=1099938&r1=1099937&r2=1099938&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/dfp/Dfp.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/dfp/Dfp.java Thu May  5 19:41:28 2011
@@ -163,7 +163,7 @@ public class Dfp implements FieldElement
     /** Mantissa. */
     protected int[] mant;
 
-    /** Sign bit: & for positive, -1 for negative. */
+    /** Sign bit: 1 for positive, -1 for negative. */
     protected byte sign;
 
     /** Exponent. */
@@ -269,6 +269,10 @@ public class Dfp implements FieldElement
         if (exponent == -1023) {
             // Zero or sub-normal
             if (x == 0) {
+                // make sure 0 has the right sign
+                if ((bits & 0x8000000000000000L) != 0) {
+                    sign = -1;
+                }
                 return;
             }
 
@@ -2315,7 +2319,10 @@ public class Dfp implements FieldElement
 
         Dfp y = this;
         boolean negate = false;
-        if (lessThan(getZero())) {
+        int cmp0 = compare(this, getZero());
+        if (cmp0 == 0) {
+            return sign < 0 ? -0.0 : +0.0;
+        } else if (cmp0 < 0) {
             y = negate();
             negate = true;
         }

Modified: commons/proper/math/trunk/src/site/xdoc/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/site/xdoc/changes.xml?rev=1099938&r1=1099937&r2=1099938&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Thu May  5 19:41:28 2011
@@ -52,6 +52,9 @@ The <action> type attribute can be add,u
     If the output is not quite correct, check for invisible trailing spaces!
      -->
     <release version="3.0" date="TBD" description="TBD">
+      <action dev="luc" type="add" issue="MATH-567" due-to="Michel">
+        Fixed conversion problems to/from 0 in Decimal Floating Point (Dfp) class.
+      </action>
       <action dev="luc" type="fix" >
         Fixed initialization of multistep ODE integrators. Relying on the interpolation model
         of the starter integrator inside only one step was wrong. The model may have a too

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/dfp/DfpTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/dfp/DfpTest.java?rev=1099938&r1=1099937&r2=1099938&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/dfp/DfpTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/dfp/DfpTest.java Thu May  5 19:41:28 2011
@@ -17,6 +17,8 @@
 
 package org.apache.commons.math.dfp;
 
+import org.apache.commons.math.util.FastMath;
+import org.apache.commons.math.util.MathUtils;
 import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
@@ -1504,4 +1506,13 @@ public class DfpTest {
              DfpField.FLAG_INVALID, "Sqrt #9");
     }
 
+    @Test
+    public void testIssue567() {
+        DfpField field = new DfpField(100);
+        Assert.assertEquals(0.0, field.getZero().toDouble(), MathUtils.SAFE_MIN);
+        Assert.assertEquals(0.0, field.newDfp(0.0).toDouble(), MathUtils.SAFE_MIN);
+        Assert.assertEquals(-1, FastMath.copySign(1, field.newDfp(-0.0).toDouble()), MathUtils.EPSILON);
+        Assert.assertEquals(+1, FastMath.copySign(1, field.newDfp(+0.0).toDouble()), MathUtils.EPSILON);
+    }
+
 }