Author: luc
Date: Sun Oct 26 07:55:45 2008
New Revision: 708001

URL: http://svn.apache.org/viewvc?rev=708001&view=rev
Log:
Added a scalb method in MathUtils.
This method is similar to the method with same name added in java.lang.Math as 
of Java 6.

Modified:
    
commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/util/MathUtils.java
    commons/proper/math/branches/MATH_2_0/src/site/xdoc/changes.xml
    
commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/util/MathUtilsTest.java

Modified: 
commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/util/MathUtils.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/util/MathUtils.java?rev=708001&r1=708000&r2=708001&view=diff
==============================================================================
--- 
commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/util/MathUtils.java
 (original)
+++ 
commons/proper/math/branches/MATH_2_0/src/java/org/apache/commons/math/util/MathUtils.java
 Sun Oct 26 07:55:45 2008
@@ -713,6 +713,33 @@
     }
 
     /**
+     * Scale a number by 2<sup>scaleFactor</sup>.
+     * <p>If <code>d</code> is 0 or NaN or Infinite, it is returned 
unchanged.</p>
+     * 
+     * @param d base number
+     * @param scaleFactor power of two by which d sould be multiplied
+     * @return d &times; 2<sup>scaleFactor</sup>
+     * @since 2.0
+     */
+    public static double scalb(final double d, final int scaleFactor) {
+
+        // handling of some important special cases
+        if ((d == 0) || Double.isNaN(d) || Double.isInfinite(d)) {
+            return d;
+        }
+
+        // split the double in raw components
+        final long bits     = Double.doubleToLongBits(d);
+        final long exponent = bits & 0x7ff0000000000000L;
+        final long rest     = bits & 0x800fffffffffffffL;
+
+        // shift the exponent
+        final long newBits = rest | (exponent + (((long) scaleFactor) << 52));
+        return Double.longBitsToDouble(newBits);
+
+    }
+
+    /**
      * Normalize an angle in a 2&pi wide interval around a center value.
      * <p>This method has three main uses:</p>
      * <ul>

Modified: commons/proper/math/branches/MATH_2_0/src/site/xdoc/changes.xml
URL: 
http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_0/src/site/xdoc/changes.xml?rev=708001&r1=708000&r2=708001&view=diff
==============================================================================
--- commons/proper/math/branches/MATH_2_0/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/branches/MATH_2_0/src/site/xdoc/changes.xml Sun Oct 26 
07:55:45 2008
@@ -39,6 +39,10 @@
   </properties>
   <body>
     <release version="2.0" date="TBD" description="TBD">
+      <action dev="luc" type="add" >
+        Added a scalb method in MathUtils. This method is similar to the method
+        with same name added in java.lang.Math as of Java 6.
+      </action>
       <action dev="brentworden" type="fix" issue="MATH-227" due-to="Joerg 
Henning">
         Fixed F distribution inverse CDF computation for small denominator 
degrees of freedom.
       </action>

Modified: 
commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/util/MathUtilsTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/util/MathUtilsTest.java?rev=708001&r1=708000&r2=708001&view=diff
==============================================================================
--- 
commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/util/MathUtilsTest.java
 (original)
+++ 
commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/util/MathUtilsTest.java
 Sun Oct 26 07:55:45 2008
@@ -521,6 +521,16 @@
         assertEquals(0, MathUtils.nextAfter(-Double.MIN_VALUE, 1), 0);
     }
 
+    public void testScalb() {
+        assertEquals( 0.0, MathUtils.scalb(0.0, 5), 1.0e-15);
+        assertEquals(32.0, MathUtils.scalb(1.0, 5), 1.0e-15);
+        assertEquals(1.0 / 32.0, MathUtils.scalb(1.0,  -5), 1.0e-15);
+        assertEquals(Math.PI, MathUtils.scalb(Math.PI, 0), 1.0e-15);
+        assertTrue(Double.isInfinite(MathUtils.scalb(Double.POSITIVE_INFINITY, 
1)));
+        assertTrue(Double.isInfinite(MathUtils.scalb(Double.NEGATIVE_INFINITY, 
1)));
+        assertTrue(Double.isNaN(MathUtils.scalb(Double.NaN, 1)));
+    }
+
     public void testNormalizeAngle() {
         for (double a = -15.0; a <= 15.0; a += 0.1) {
             for (double b = -15.0; b <= 15.0; b += 0.2) {


Reply via email to