commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject svn commit: r1372199 - /commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/FastMath.java
Date Sun, 12 Aug 2012 21:22:59 GMT
Author: erans
Date: Sun Aug 12 21:22:58 2012
New Revision: 1372199

URL: http://svn.apache.org/viewvc?rev=1372199&view=rev
Log:
Code cleanup.

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/FastMath.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/FastMath.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/FastMath.java?rev=1372199&r1=1372198&r2=1372199&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/FastMath.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/FastMath.java Sun
Aug 12 21:22:58 2012
@@ -1328,47 +1328,42 @@ public class FastMath {
         return a + b;
     }
 
-    /** Compute log(1 + x).
-     * @param x a number
-     * @return log(1 + x)
+    /**
+     * Computes log(1 + x).
+     *
+     * @param x Number.
+     * @return {@code log(1 + x)}.
      */
     public static double log1p(final double x) {
-
         if (x == -1) {
-            return x/0.0;   // -Infinity
+            return Double.NEGATIVE_INFINITY;
         }
 
-        if (x > 0 && 1/x == 0) { // x = Infinity
-            return x;
+        if (x == Double.POSITIVE_INFINITY) {
+            return Double.POSITIVE_INFINITY;
         }
 
-        if (x>1e-6 || x<-1e-6) {
-            double xpa = 1.0 + x;
-            double xpb = -(xpa - 1.0 - x);
-
-            double hiPrec[] = new double[2];
+        if (x > 1e-6 ||
+            x < -1e-6) {
+            final double xpa = 1 + x;
+            final double xpb = -(xpa - 1 - x);
 
+            final double[] hiPrec = new double[2];
             final double lores = log(xpa, hiPrec);
-            if (Double.isInfinite(lores)){ // don't allow this to be converted to NaN
+            if (Double.isInfinite(lores)) { // Don't allow this to be converted to NaN
                 return lores;
             }
 
-            /* Do a taylor series expansion around xpa */
-            /* f(x+y) = f(x) + f'(x)*y + f''(x)/2 y^2 */
-            double fx1 = xpb/xpa;
-
-            double epsilon = 0.5 * fx1 + 1.0;
-            epsilon = epsilon * fx1;
-
-            return epsilon + hiPrec[1] + hiPrec[0];
+            // Do a taylor series expansion around xpa:
+            //   f(x+y) = f(x) + f'(x) y + f''(x)/2 y^2
+            final double fx1 = xpb / xpa;
+            final double epsilon = 0.5 * fx1 + 1;
+            return epsilon * fx1 + hiPrec[1] + hiPrec[0];
+        } else {
+            // Value is small |x| < 1e6, do a Taylor series centered on 1.
+            final double y = (x * F_1_3 - F_1_2) * x + 1;
+            return y * x;
         }
-
-        /* Value is small |x| < 1e6, do a Taylor series centered on 1.0 */
-        double y = x * F_1_3 - F_1_2;
-        y = y * x + 1.0;
-        y = y * x;
-
-        return y;
     }
 
     /** Compute the base 10 logarithm.



Mime
View raw message