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 2021/06/08 20:53:36 UTC

[commons-numbers] 02/03: Sonar fix: Remove nested ternary.

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 a2befeb3fc9a5704a13477864a201f06873092ed
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Tue Jun 8 21:29:10 2021 +0100

    Sonar fix: Remove nested ternary.
    
    Updated javadoc since a is not returned when eqZero(a) is true, either 0
    or 1 are returned. a can be a small epsilon value and will not be
    returned in this case.
---
 .../org/apache/commons/numbers/core/Precision.java   | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/Precision.java b/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/Precision.java
index c4e376d..a5c73bb 100644
--- a/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/Precision.java
+++ b/commons-numbers-core/src/main/java/org/apache/commons/numbers/core/Precision.java
@@ -605,16 +605,26 @@ public final class Precision {
 
         /**
          * Returns the {@link Math#signum(double) sign} of the argument.
+         * The returned value is
+         * <ul>
+         *  <li>{@code -0.0} if {@code a} is considered equal to zero and negatively signed,</li>
+         *  <li>{@code +0.0} if {@code a} is considered equal to zero and positively signed,</li>
+         *  <li>{@code -1.0} if {@code a} is considered less than zero,</li>
+         *  <li>{@code +1.0} if {@code a} is considered greater than zero.</li>
+         * </ul>
+         *
+         * <p>The equality with zero uses the {@link #eqZero(double) eqZero} method.
          *
          * @param a Value.
-         * @return the sign (or {@code a} if {@code eqZero(a)} is true or
+         * @return the sign (or {@code a} if {@code a == 0} or
          * {@code a} is NaN).
+         * @see #eqZero(double)
          */
         default double signum(double a) {
-            return a == 0d ||
-                Double.isNaN(a) ?
-                a :
-                eqZero(a) ?
+            if (a == 0d || Double.isNaN(a)) {
+                return a;
+            }
+            return eqZero(a) ?
                 Math.copySign(0d, a) :
                 Math.copySign(1d, a);
         }