Skip to content

Commit 4e683a6

Browse files
hansonrhansonr
authored andcommitted
BigDecimal fixes
1 parent 968c5f0 commit 4e683a6

File tree

5 files changed

+35
-24
lines changed

5 files changed

+35
-24
lines changed

sources/net.sf.j2s.java.core/src/java/math/BigDecimal.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -916,8 +916,8 @@ public BigDecimal(double val, MathContext mc) {
916916
// Translate the double into sign, exponent and significand, according
917917
// to the formulae in JLS, Section 20.10.22.
918918
int[] valBits = doubleToInt2(val);
919-
System.out.println("BigDecimal val[0] " + Integer.toBinaryString(valBits[0]));
920-
System.out.println("BigDecimal val[1] " + Integer.toBinaryString(valBits[1]));
919+
//System.out.println("BigDecimal val[0] " + Integer.toBinaryString(valBits[0]));
920+
//System.out.println("BigDecimal val[1] " + Integer.toBinaryString(valBits[1]));
921921
int sign = (val < 0 ? -1 : 1);
922922
int exponent = (int) ((valBits[1] >> 20) & 0x7ff) - 1075;
923923
int highBits = (exponent == -1075
@@ -927,7 +927,7 @@ public BigDecimal(double val, MathContext mc) {
927927
long significand = BigInteger.longLeftShift(highBits, 32) + valBits[0];
928928

929929

930-
System.out.println("BigDecimal sign/exp/sign " + sign + " " + exponent + " " + significand);
930+
//System.out.println("BigDecimal sign/exp/sign " + sign + " " + exponent + " " + significand);
931931

932932

933933
// At this point, val == sign * significand * 2**exponent.
@@ -952,17 +952,17 @@ public BigDecimal(double val, MathContext mc) {
952952
// Calculate intVal and scale
953953
BigInteger intVal = BigInteger.valueOf(sign * significand);
954954
intVal.signum = sign;
955-
System.out.println("BigDecimal intVal " + intVal);
955+
//System.out.println("BigDecimal intVal " + intVal);
956956
if (exponent == 0) {
957957
} else {
958958
if (exponent < 0) {
959959
BigInteger bn = BigInteger.valueOf(5).pow(-exponent);
960960
// intVal = intVal.multiply(bn);
961-
System.out.println(intVal.multiply(bn));
962-
System.out.println(bn.multiply(intVal));
961+
//System.out.println(intVal.multiply(bn));
962+
//System.out.println(bn.multiply(intVal));
963963
intVal = bn.multiply(intVal);
964-
System.out.println(intVal);
965-
System.out.println(bn);
964+
//System.out.println(intVal);
965+
//System.out.println(bn);
966966
scale = -exponent;
967967
} else { // (exponent > 0)
968968
intVal = intVal.multiply(BigInteger.valueOf(2).pow(exponent));
@@ -1004,8 +1004,8 @@ public BigDecimal(double val, MathContext mc) {
10041004
this.intCompact = compactVal;
10051005
this.scale = scale;
10061006
this.precision = prec;
1007-
System.out.println("BigDecimal intVal " + (intVal == null ? null : intVal.toString()));
1008-
System.out.println("--");
1007+
//System.out.println("BigDecimal intVal " + (intVal == null ? null : intVal.toString()));
1008+
//System.out.println("--");
10091009
}
10101010

10111011
private final static double[] bufd;
@@ -3427,7 +3427,7 @@ int putIntCompact(long intCompact) {
34273427

34283428
// Get 2 digits/iteration using longs until quotient fits into an int
34293429
while (intCompact > Integer.MAX_VALUE) {
3430-
q = intCompact / 100;
3430+
q = /** @j2sNative Math.floor(intCompact / 100) || */0;
34313431
r = (int)(intCompact - q * 100);
34323432
intCompact = q;
34333433
cmpCharArray[--charPos] = DIGIT_ONES[r];

sources/net.sf.j2s.java.core/src/java/math/BigInteger.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -264,14 +264,20 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
264264
TWO_TO_THE[i] = TWO_TO_THE[i - 1] * 2;
265265
}
266266

267+
/**
268+
* SwingJS addition, for BigDecimal
269+
* @param x
270+
* @return
271+
*/
267272
static int longNumberOfLeadingZeros(long x) {
268-
int n = -SPARE_BITS_INT;
273+
int n;
269274
long y = getHighBits(x);
270275
if (y == 0) {
271276
x = getLowBits(x);
277+
n = 32;
272278
} else {
273-
n = BITS_PER_INT - 2 * SPARE_BITS_INT;
274-
x = y;
279+
x = y;
280+
n = 0;
275281
}
276282
return n + Integer.numberOfLeadingZeros((int) x);
277283
}
@@ -3994,13 +4000,13 @@ public String toString() {
39944000
}
39954001

39964002
static void dumpBits(int[] val, String msg) {
3997-
System.out.println("js bigint:" + msg);
4003+
System.err.println("js bigint:" + msg);
39984004
for (int i = 0; i < val.length; i++) {
39994005
String s = "00000000000000000000000000000000000000000000000000000000000000000000" + Integer.toBinaryString(val[i]);
40004006
s = s.substring(s.length() - 32);
40014007
for (int j = 0; j < 32; j += 8)
4002-
System.out.print(s.substring(j, j+8) + " ");
4003-
System.out.println(" " + Integer.toHexString(val[i]));
4008+
System.err.print(s.substring(j, j+8) + " ");
4009+
System.err.println(" " + Integer.toHexString(val[i]));
40044010
}
40054011
}
40064012

sources/net.sf.j2s.java.core/src/test/Test_BigDec.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public static void main(String[] args) {
2626

2727
private static void testBDMul() {
2828
BigDecimal g;
29+
g = new BigDecimal(200.05, new MathContext(6));
30+
System.out.println("200.050 == " + g.toString());
31+
assert(g.toString().equals("200.050"));
32+
2933
g = new BigDecimal(1);
3034
g = new BigDecimal(200.05);
3135
System.out.println("200.05 = " + g);
@@ -56,14 +60,9 @@ private static void testBDMul() {
5660

5761
long time = 1538673122263L;//System.currentTimeMillis();
5862
g = BigDecimal.valueOf(time, 3);
59-
System.out.println(time + " " + g);
63+
System.out.println(time + " " + g.toString());
6064
assert (g.toString().equals("1538673122.263"));
6165

62-
g = new BigDecimal(200.05, new MathContext(6));
63-
System.out.println(g);
64-
assert(g.toString().equals("200.050"));
65-
66-
6766
System.out.println("testBDMul OK");
6867
}
6968

sources/net.sf.j2s.java.core/src/test/Test_BigDecJava.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public static void main(String[] args) {
2121
private static void testBDMul() {
2222
BigDecimal g;
2323

24+
g = new BigDecimal(200.05, new MathContext(6));
25+
System.out.println("200.050 == " + g.toString());
26+
assert(g.toString().equals("200.050"));
27+
28+
2429
g = new BigDecimal(200.05);
2530
System.out.println("200.05 = " + g);
2631
System.out.println("200 = " + g.toBigInteger());

sources/net.sf.j2s.java.core/src/test/math/BigDecimal.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3805,7 +3805,8 @@ static int longDigitLength(long x) {
38053805
x = -x;
38063806
if (x < 10) // must screen for 0, might as well 10
38073807
return 1;
3808-
int r = ((64 - Long.numberOfLeadingZeros(x) + 1) * 1233) >>> 12;
3808+
int n = Long.numberOfLeadingZeros(x);
3809+
int r = ((64 - n + 1) * 1233) >>> 12;
38093810
long[] tab = LONG_TEN_POWERS_TABLE;
38103811
// if r >= length, must have max possible digits for long
38113812
return (r >= tab.length || x < tab[r]) ? r : r + 1;

0 commit comments

Comments
 (0)