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/12 12:26:22 UTC

[commons-numbers] 01/05: Use getExponent(double) in place of Math.log(double) / Math.log(2)

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 fbf308b12fee9757eece61a57cf356ced1b7ca1a
Author: aherbert <ah...@apache.org>
AuthorDate: Thu Dec 12 10:36:20 2019 +0000

    Use getExponent(double) in place of Math.log(double) / Math.log(2)
    
    The calls to Math.log() are only required to the nearest integer value
    for use in scaling.
---
 .../src/main/java/org/apache/commons/numbers/complex/Complex.java | 8 +++++---
 1 file changed, 5 insertions(+), 3 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 f3ac498..56529db 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
@@ -464,9 +464,11 @@ public final class Complex implements Serializable  {
         double c = re2;
         double d = im2;
         int ilogbw = 0;
-        final double logbw = Math.log(Math.max(Math.abs(c), Math.abs(d))) / Math.log(2);
-        if (Double.isFinite(logbw)) {
-            ilogbw = (int)logbw;
+        // Get the exponent to scale the divisor.
+        // This is equivalent to (int) Math.log2(double).
+        final int exponent = Math.getExponent(Math.max(Math.abs(c), Math.abs(d)));
+        if (exponent <= Double.MAX_EXPONENT) {
+            ilogbw = exponent;
             c = Math.scalb(c, -ilogbw);
             d = Math.scalb(d, -ilogbw);
         }