Author: luc
Date: Mon Feb 25 14:33:53 2013
New Revision: 1449722

URL: http://svn.apache.org/r1449722
Log:
Fixed definition of remainder and added missing copySign signature.

Modified:
    
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/ExtendedFieldElement.java
    
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/differentiation/DSCompiler.java
    
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructure.java
    
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/dfp/Dfp.java
    
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/Decimal64.java

Modified: 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/ExtendedFieldElement.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/ExtendedFieldElement.java?rev=1449722&r1=1449721&r2=1449722&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/ExtendedFieldElement.java
 (original)
+++ 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/ExtendedFieldElement.java
 Mon Feb 25 14:33:53 2013
@@ -44,15 +44,17 @@ public interface ExtendedFieldElement<T>
      */
     T divide(double a);
 
-    /** '%' operator.
+    /** IEEE remainder operator.
      * @param a right hand side parameter of the operator
-     * @return this%a
+     * @return this - n &times; a where n is the closest integer to this/a
+     * (the even integer is chosen for n if this/a is halfway between two 
integers)
      */
     T remainder(double a);
 
-    /** '%' operator.
+    /** IEEE remainder operator.
      * @param a right hand side parameter of the operator
-     * @return this%a
+     * @return this - n &times; a where n is the closest integer to this/a
+     * (the even integer is chosen for n if this/a is halfway between two 
integers)
      * @exception DimensionMismatchException if number of free parameters or 
orders are inconsistent
      */
     T remainder(T a)
@@ -96,6 +98,15 @@ public interface ExtendedFieldElement<T>
      * @param sign the sign for the returned value
      * @return the instance with the same sign as the {@code sign} argument
      */
+    T copySign(T sign);
+
+    /**
+     * Returns the instance with the sign of the argument.
+     * A NaN {@code sign} argument is treated as positive.
+     *
+     * @param sign the sign for the returned value
+     * @return the instance with the same sign as the {@code sign} argument
+     */
     T copySign(double sign);
 
     /**

Modified: 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/differentiation/DSCompiler.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/differentiation/DSCompiler.java?rev=1449722&r1=1449721&r2=1449722&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/differentiation/DSCompiler.java
 (original)
+++ 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/differentiation/DSCompiler.java
 Mon Feb 25 14:33:53 2013
@@ -812,7 +812,7 @@ public class DSCompiler {
                           final double[] result, final int resultOffset) {
 
         // compute k such that lhs % rhs = lhs - k rhs
-        final double rem = lhs[lhsOffset] % rhs[rhsOffset];
+        final double rem = FastMath.IEEEremainder(lhs[lhsOffset], 
rhs[rhsOffset]);
         final double k   = FastMath.rint((lhs[lhsOffset] - rem) / 
rhs[rhsOffset]);
 
         // set up value

Modified: 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructure.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructure.java?rev=1449722&r1=1449721&r2=1449722&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructure.java
 (original)
+++ 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructure.java
 Mon Feb 25 14:33:53 2013
@@ -336,7 +336,7 @@ public class DerivativeStructure impleme
     /** {@inheritDoc} */
     public DerivativeStructure remainder(final double a) {
         final DerivativeStructure ds = new DerivativeStructure(this);
-        ds.data[0] = ds.data[0] % a;
+        ds.data[0] = FastMath.IEEEremainder(ds.data[0], a);
         return ds;
     }
 
@@ -402,6 +402,16 @@ public class DerivativeStructure impleme
     }
 
     /** {@inheritDoc} */
+    public DerivativeStructure copySign(final DerivativeStructure sign){
+        long m = Double.doubleToLongBits(data[0]);
+        long s = Double.doubleToLongBits(sign.data[0]);
+        if ((m >= 0 && s >= 0) || (m < 0 && s < 0)) { // Sign is currently OK
+            return this;
+        }
+        return negate(); // flip sign
+    }
+
+    /** {@inheritDoc} */
     public DerivativeStructure copySign(final double sign){
         long m = Double.doubleToLongBits(data[0]);
         long s = Double.doubleToLongBits(sign);

Modified: 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/dfp/Dfp.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/dfp/Dfp.java?rev=1449722&r1=1449721&r2=1449722&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/dfp/Dfp.java 
(original)
+++ 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/dfp/Dfp.java 
Mon Feb 25 14:33:53 2013
@@ -2568,9 +2568,19 @@ public class Dfp implements ExtendedFiel
     /** {@inheritDoc}
      * @since 3.2
      */
-    public Dfp copySign(final double sign) {
-        long s = Double.doubleToLongBits(sign);
-        if ((sign >= 0 && s >= 0) || (sign < 0 && s < 0)) { // Sign is 
currently OK
+    public Dfp copySign(final Dfp s) {
+        if ((sign >= 0 && s.sign >= 0) || (sign < 0 && s.sign < 0)) { // Sign 
is currently OK
+            return this;
+        }
+        return negate(); // flip sign
+    }
+
+    /** {@inheritDoc}
+     * @since 3.2
+     */
+    public Dfp copySign(final double s) {
+        long sb = Double.doubleToLongBits(s);
+        if ((sign >= 0 && sb >= 0) || (sign < 0 && sb < 0)) { // Sign is 
currently OK
             return this;
         }
         return negate(); // flip sign
@@ -2601,7 +2611,9 @@ public class Dfp implements ExtendedFiel
      * @since 3.2
      */
     public Dfp rootN(final int n) {
-        return DfpMath.pow(this, getOne().divide(n));
+        return (sign >= 0) ?
+               DfpMath.pow(this, getOne().divide(n)) :
+               DfpMath.pow(negate(), getOne().divide(n)).negate();
     }
 
     /** {@inheritDoc}
@@ -2747,7 +2759,7 @@ public class Dfp implements ExtendedFiel
     public Dfp tanh() {
         final Dfp ePlus  = DfpMath.exp(this);
         final Dfp eMinus = DfpMath.exp(negate());
-        return ePlus.add(eMinus).divide(ePlus.subtract(eMinus));
+        return ePlus.subtract(eMinus).divide(ePlus.add(eMinus));
     }
 
     /** {@inheritDoc}

Modified: 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/Decimal64.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/Decimal64.java?rev=1449722&r1=1449721&r2=1449722&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/Decimal64.java
 (original)
+++ 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/Decimal64.java
 Mon Feb 25 14:33:53 2013
@@ -330,12 +330,12 @@ public class Decimal64 extends Number
 
     /** {@inheritDoc} */
     public Decimal64 remainder(final double a) {
-        return new Decimal64(value % a);
+        return new Decimal64(FastMath.IEEEremainder(value, a));
     }
 
     /** {@inheritDoc} */
     public Decimal64 remainder(final Decimal64 a) {
-        return new Decimal64(value % a.value);
+        return new Decimal64(FastMath.IEEEremainder(value, a.value));
     }
 
     /** {@inheritDoc} */
@@ -369,6 +369,11 @@ public class Decimal64 extends Number
     }
 
     /** {@inheritDoc} */
+    public Decimal64 copySign(final Decimal64 sign) {
+        return new Decimal64(FastMath.copySign(value, sign.value));
+    }
+
+    /** {@inheritDoc} */
     public Decimal64 copySign(final double sign) {
         return new Decimal64(FastMath.copySign(value, sign));
     }
@@ -395,7 +400,11 @@ public class Decimal64 extends Number
 
     /** {@inheritDoc} */
     public Decimal64 rootN(final int n) {
-        return new Decimal64(FastMath.pow(value, 1.0 / n));
+        if (value < 0) {
+            return new Decimal64(-FastMath.pow(-value, 1.0 / n));
+        } else {
+            return new Decimal64(FastMath.pow(value, 1.0 / n));
+        }
     }
 
     /** {@inheritDoc} */


Reply via email to