fixed scalb for very large powers

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1062928 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2011-01-24 19:13:33 +00:00
parent 2f7074cff0
commit a3f6c210e3
2 changed files with 20 additions and 0 deletions

View File

@ -3421,6 +3421,12 @@ public class FastMath {
if (Double.isNaN(d) || Double.isInfinite(d) || (d == 0)) {
return d;
}
if (n < -2098) {
return (d > 0) ? 0.0 : -0.0;
}
if (n > 2097) {
return (d > 0) ? Double.POSITIVE_INFINITY : Double.NEGATIVE_INFINITY;
}
// decompose d
final long bits = Double.doubleToLongBits(d);
@ -3499,6 +3505,12 @@ public class FastMath {
if (Float.isNaN(f) || Float.isInfinite(f) || (f == 0f)) {
return f;
}
if (n < -277) {
return (f > 0) ? 0.0f : -0.0f;
}
if (n > 276) {
return (f > 0) ? Float.POSITIVE_INFINITY : Float.NEGATIVE_INFINITY;
}
// decompose f
final int bits = Float.floatToIntBits(f);

View File

@ -1061,6 +1061,12 @@ public class FastMathTest {
Assert.assertEquals(Double.NEGATIVE_INFINITY, FastMath.scalb(-1.1102230246251565E-16, 1079), 0D);
Assert.assertEquals(Double.NEGATIVE_INFINITY, FastMath.scalb(-2.2250738585072014E-308, 2047), 0D);
Assert.assertEquals(Double.NEGATIVE_INFINITY, FastMath.scalb(-2.2250738585072014E-308, 2048), 0D);
Assert.assertEquals(Double.NEGATIVE_INFINITY, FastMath.scalb(-1.7976931348623157E308, 2147483647), 0D);
Assert.assertEquals(Double.POSITIVE_INFINITY, FastMath.scalb( 1.7976931348623157E308, 2147483647), 0D);
Assert.assertEquals(Double.NEGATIVE_INFINITY, FastMath.scalb(-1.1102230246251565E-16, 2147483647), 0D);
Assert.assertEquals(Double.POSITIVE_INFINITY, FastMath.scalb( 1.1102230246251565E-16, 2147483647), 0D);
Assert.assertEquals(Double.NEGATIVE_INFINITY, FastMath.scalb(-2.2250738585072014E-308, 2147483647), 0D);
Assert.assertEquals(Double.POSITIVE_INFINITY, FastMath.scalb( 2.2250738585072014E-308, 2147483647), 0D);
}
@Test
@ -1077,6 +1083,8 @@ public class FastMathTest {
Assert.assertEquals(Float.POSITIVE_INFINITY, FastMath.scalb(Float.POSITIVE_INFINITY, -1000000), 0F);
Assert.assertEquals(-3.13994498e38f, FastMath.scalb(-1.1e-7f, 151), 0F);
Assert.assertEquals(Float.NEGATIVE_INFINITY, FastMath.scalb(-1.1e-7f, 152), 0F);
Assert.assertEquals(Float.POSITIVE_INFINITY, FastMath.scalb(3.4028235E38f, 2147483647), 0F);
Assert.assertEquals(Float.NEGATIVE_INFINITY, FastMath.scalb(-3.4028235E38f, 2147483647), 0F);;
}
private boolean compareClassMethods(Class<?> class1, Class<?> class2){