You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2019/12/06 18:29:07 UTC

[commons-numbers] 13/19: Use changeSign() rather than copySign().

This is an automated email from the ASF dual-hosted git repository.

aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git

commit 52a8992fff8641ec3ba628baf3b982a403b60b61
Author: aherbert <ah...@apache.org>
AuthorDate: Fri Dec 6 15:27:16 2019 +0000

    Use changeSign() rather than copySign().
---
 .../apache/commons/numbers/complex/Complex.java    | 31 ++++++++++++++++++----
 1 file changed, 26 insertions(+), 5 deletions(-)

diff --git a/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java b/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java
index 3cf2705..59bb123 100644
--- a/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java
+++ b/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java
@@ -1002,7 +1002,7 @@ public final class Complex implements Serializable  {
                 final double re = PI_OVER_2 - getArgument(x, y);
                 final double im = Math.log(getAbsolute(x, y));
                 // Map back to the correct sign
-                return constructor.create(re, negative(imaginary) ? -im : im);
+                return constructor.create(re, changeSign(im, imaginary));
             }
             if (Double.isInfinite(imaginary)) {
                 return constructor.create(PI_OVER_2, Math.copySign(Double.POSITIVE_INFINITY, -imaginary));
@@ -1149,8 +1149,8 @@ public final class Complex implements Serializable  {
                 final double re = Math.log(getAbsolute(x, y));
                 final double im = getArgument(x, y);
                 // Map back to the correct sign
-                return constructor.create(Math.copySign(re, real),
-                                          Math.copySign(im, imaginary));
+                return constructor.create(changeSign(re, real),
+                                          changeSign(im, imaginary));
             }
             if (Double.isInfinite(imaginary)) {
                 return constructor.create(Math.copySign(Double.POSITIVE_INFINITY, real),
@@ -1232,8 +1232,8 @@ public final class Complex implements Serializable  {
                 final double re = 0.5 * Math.log(result.abs());
                 final double im = 0.5 * result.getArgument();
                 // Map back to the correct sign
-                return constructor.create(Math.copySign(re, real),
-                                          Math.copySign(im, imaginary));
+                return constructor.create(changeSign(re, real),
+                                          changeSign(im, imaginary));
             }
             if (Double.isInfinite(imaginary)) {
                 return constructor.create(Math.copySign(0, real), Math.copySign(PI_OVER_2, imaginary));
@@ -1948,6 +1948,27 @@ public final class Complex implements Serializable  {
     }
 
     /**
+     * Change the sign of the magnitude based on the signed value.
+     *
+     * <p>If the signed value is negative then the result is {@code -magnitude}; otherwise
+     * return {@code magnitude}.
+     *
+     * <p>A signed value of {@code -0.0} is treated as negative. A signed value of {@code NaN}
+     * is treated as positive.
+     *
+     * <p>This is not the same as {@link Math#copySign(double, double)} as this method
+     * will change the sign based on the signed value rather than copy the sign.
+     *
+     * @param magnitude the magnitude
+     * @param signedValue the signed value
+     * @return magnitude or -magnitude
+     * @see #negative(double)
+     */
+    private static double changeSign(double magnitude, double signedValue) {
+        return negative(signedValue) ? -magnitude : magnitude;
+    }
+
+    /**
      * Creates an exception.
      *
      * @param message Message prefix.