Fix a problem with BigDecimal, where the bitlength of a zero value
was not being computed properly.
Patch by: jat
Review by: conroy, janz
git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@9407 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/super/com/google/gwt/emul/java/math/BigDecimal.java b/user/super/com/google/gwt/emul/java/math/BigDecimal.java
index 7c99ce5..0cd351f 100644
--- a/user/super/com/google/gwt/emul/java/math/BigDecimal.java
+++ b/user/super/com/google/gwt/emul/java/math/BigDecimal.java
@@ -309,6 +309,10 @@
private static int bitLength(double value) {
// if |value| is less than 2^47, use log
if (value > -POW47 && value < POW47) {
+ if (value == 0.0) {
+ // special-case zero, otherwise we get -INFINITY below
+ return 0;
+ }
boolean negative = (value < 0.0);
if (negative) {
value = -value;
diff --git a/user/test/com/google/gwt/emultest/java/math/BigDecimalConstructorsTest.java b/user/test/com/google/gwt/emultest/java/math/BigDecimalConstructorsTest.java
index 044e19f..88e0caa 100644
--- a/user/test/com/google/gwt/emultest/java/math/BigDecimalConstructorsTest.java
+++ b/user/test/com/google/gwt/emultest/java/math/BigDecimalConstructorsTest.java
@@ -724,6 +724,24 @@
}
/**
+ * Test that constructing BigDecimals from zeros works properly.
+ */
+ public void testConstrZero() {
+ BigDecimal bd = new BigDecimal("0");
+ assertEquals(0, bd.intValueExact());
+ assertEquals(1, bd.precision());
+ assertEquals(0, bd.scale());
+ bd = new BigDecimal("0.0");
+ assertEquals(0, bd.intValueExact());
+ assertEquals(1, bd.precision());
+ assertEquals(1, bd.scale());
+ bd = new BigDecimal("0.00");
+ assertEquals(0, bd.intValueExact());
+ assertEquals(1, bd.precision());
+ assertEquals(2, bd.scale());
+ }
+
+ /**
* check ONE.
*/
public void testFieldONE() {