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.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/branches/MATH_2_0@708001 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2008-10-26 14:55:45 +00:00
parent 223168ee50
commit e143e9097d
3 changed files with 41 additions and 0 deletions

View File

@ -712,6 +712,33 @@ public final class MathUtils {
}
/**
* 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>

View File

@ -39,6 +39,10 @@ The <action> type attribute can be add,update,fix,remove.
</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>

View File

@ -521,6 +521,16 @@ public final class MathUtilsTest extends TestCase {
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) {