You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2019/06/30 21:55:51 UTC

[commons-numbers] 04/09: NUMBERS-119: Slight refactoring.

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

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

commit dad57d5eba8bfdec17214890123f9f1a5862bb1b
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Sun Jun 30 22:15:35 2019 +0200

    NUMBERS-119: Slight refactoring.
---
 .../commons/numbers/fraction/BigFraction.java      | 23 +++++++++++-----------
 1 file changed, 11 insertions(+), 12 deletions(-)

diff --git a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java
index 109a0cc..4a79128 100644
--- a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java
+++ b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/BigFraction.java
@@ -219,32 +219,32 @@ public class BigFraction extends Number implements Comparable<BigFraction>, Seri
             throw new IllegalArgumentException("cannot convert infinite value");
         }
 
-        final long bits     = Double.doubleToLongBits(value);
-        final long sign     = bits & 0x8000000000000000L;
+        final long bits = Double.doubleToLongBits(value);
+        final long sign = bits & 0x8000000000000000L;
         final long exponent = bits & 0x7ff0000000000000L;
         final long mantissa = bits & 0x000fffffffffffffL;
 
-        // compute m and k such that value = m * 2^k
+        // Compute m and k such that value = m * 2^k
         long m;
         int k;
 
         if (exponent != 0) {
-            // this was a normalized number, add the implicit most significant bit
+            // Normalized number: Add the implicit most significant bit.
             m = mantissa | 0x0010000000000000L;
-            k = ((int) (exponent >> 52)) - 1023 - 52;
+            k = ((int) (exponent >> 52)) - 1075; // Exponent bias is 1023.
         } else {
             m = mantissa;
-            if (m == 0) { //number is zero, set k to 0 for simplicity
-                k = 0;
-            } else {
-                // subnormal number, the effective exponent bias is only 1022
-                k = - 1022 - 52;
+            k = 0; // For simplicity, when number is 0.
+            if (m != 0) {
+                // Subnormal number, the effective exponent bias is 1022, not 1023.
+                k = -1074;
             }
         }
         if (sign != 0) {
             m = -m;
         }
-        while (((m & 0x001ffffffffffffeL) != 0) && ((m & 0x1) == 0)) {
+        while ((m & 0x001ffffffffffffeL) != 0 &&
+               (m & 0x1) == 0) {
             m >>= 1;
             ++k;
         }
@@ -256,7 +256,6 @@ public class BigFraction extends Number implements Comparable<BigFraction>, Seri
             numerator   = BigInteger.valueOf(m).multiply(BigInteger.ZERO.flipBit(k));
             denominator = BigInteger.ONE;
         }
-
     }
 
     /**