commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject svn commit: r1061626 - /commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/util/FastMath.java
Date Fri, 21 Jan 2011 04:35:22 GMT
Author: sebb
Date: Fri Jan 21 04:35:21 2011
New Revision: 1061626

URL: http://svn.apache.org/viewvc?rev=1061626&view=rev
Log:
MATH-476 FastMath code contains 'magic' numbers
Extracted 2^52 value as a constant

Modified:
    commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/util/FastMath.java

Modified: commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/util/FastMath.java
URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/util/FastMath.java?rev=1061626&r1=1061625&r2=1061626&view=diff
==============================================================================
--- commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/util/FastMath.java
(original)
+++ commons/proper/math/branches/MATH_2_X/src/main/java/org/apache/commons/math/util/FastMath.java
Fri Jan 21 04:35:21 2011
@@ -166,6 +166,9 @@ public class FastMath {
      */
     private static final double HEX_40000000 = 1073741824.0;
 
+    /** 2^52 - double numbers this large must be integral (no fraction) or NaN or Infinite
*/
+    private static final double TWO_POWER_52 = 4503599627370496.0;
+
     // Initialize tables
     static {
         int i;
@@ -1291,7 +1294,7 @@ public class FastMath {
         // y is the most significant 10 bits of the mantissa
         //double y = Double.longBitsToDouble(bits & 0xfffffc0000000000L);
         //double epsilon = (x - y) / y;
-        double epsilon = (double)(bits & 0x3ffffffffffL) / (4503599627370496.0 + (bits
& 0x000ffc0000000000L));
+        double epsilon = (double)(bits & 0x3ffffffffffL) / (TWO_POWER_52 + (bits &
0x000ffc0000000000L));
 
         double lnza = 0.0;
         double lnzb = 0.0;
@@ -1306,7 +1309,7 @@ public class FastMath {
 
             /* Need a more accurate epsilon, so adjust the division. */
             double numer = (double)(bits & 0x3ffffffffffL);
-            double denom = 4503599627370496.0 + (bits & 0x000ffc0000000000L);
+            double denom = TWO_POWER_52 + (bits & 0x000ffc0000000000L);
             aa = numer - xa*denom - xb * denom;
             xb += aa / denom;
 
@@ -1579,7 +1582,7 @@ public class FastMath {
         /* Handle special case x<0 */
         if (x < 0) {
             // y is an even integer in this case
-            if (y >= 4503599627370496.0 || y <= -4503599627370496.0) {
+            if (y >= TWO_POWER_52 || y <= -TWO_POWER_52) {
                 return pow(-x, y);
             }
 
@@ -2420,8 +2423,8 @@ public class FastMath {
         }
 
         /* Convert to double */
-        double tmpA = (prod2A >>> 12) / 4503599627370496.0;  // High order 52 bits
-        double tmpB = (((prod2A & 0xfffL) << 40) + (prod2B >>> 24)) /
4503599627370496.0 / 4503599627370496.0; // Low bits
+        double tmpA = (prod2A >>> 12) / TWO_POWER_52;  // High order 52 bits
+        double tmpB = (((prod2A & 0xfffL) << 40) + (prod2B >>> 24)) /
TWO_POWER_52 / TWO_POWER_52; // Low bits
 
         double sumA = tmpA + tmpB;
         double sumB = -(sumA - tmpA - tmpB);
@@ -3499,7 +3502,7 @@ public class FastMath {
             return x;
         }
 
-        if (x >= 4503599627370496.0 || x <= -4503599627370496.0) {
+        if (x >= TWO_POWER_52 || x <= -TWO_POWER_52) {
             return x;
         }
 



Mime
View raw message