Skip to content

Commit a0e6377

Browse files
hansonrhansonr
authored andcommitted
BigInteger complete; working on BigDecimal
1 parent 1726bfb commit a0e6377

File tree

8 files changed

+197
-87
lines changed

8 files changed

+197
-87
lines changed

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

Lines changed: 10 additions & 6 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(Integer.toBinaryString(valBits[0]));
920-
System.out.println(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("" + 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,13 +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(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);
960+
// intVal = intVal.multiply(bn);
961+
System.out.println(intVal.multiply(bn));
962+
System.out.println(bn.multiply(intVal));
963+
intVal = bn.multiply(intVal);
964+
System.out.println(intVal);
960965
System.out.println(bn);
961-
intVal = intVal.multiply(bn);
962966
scale = -exponent;
963967
} else { // (exponent > 0)
964968
intVal = intVal.multiply(BigInteger.valueOf(2).pow(exponent));
@@ -1000,7 +1004,7 @@ public BigDecimal(double val, MathContext mc) {
10001004
this.intCompact = compactVal;
10011005
this.scale = scale;
10021006
this.precision = prec;
1003-
System.out.println(intVal == null ? null : intVal.toString());
1007+
System.out.println("BigDecimal intVal " + (intVal == null ? null : intVal.toString()));
10041008
System.out.println("--");
10051009
}
10061010

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -518,15 +518,24 @@ private BigInteger(long val) {
518518
} else {
519519
signum = 1;
520520
}
521-
521+
if (val + 1 == val) {
522+
throw new IllegalArgumentException("SwingJS BigInteger(long) value too large");
523+
}
522524
int highWord = getHighBits(val);
523-
if (highWord == 0) {
524-
mag = new int[1];
525-
mag[0] = getLowBits(val);
526-
} else {
525+
int lowWord = getLowBits(val);
526+
long extra = val / TWO_TO_THE[48];
527+
if (extra != 0) {
528+
mag = new int[3];
529+
mag[0] = (int) extra;
530+
mag[1] = highWord;
531+
mag[2] = lowWord;
532+
} else if (highWord != 0) {
527533
mag = new int[2];
528534
mag[0] = highWord;
529-
mag[1] = getLowBits(val);
535+
mag[1] = lowWord;
536+
} else {
537+
mag = new int[1];
538+
mag[0] = lowWord;
530539
}
531540
}
532541

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@
4646
*/
4747

4848
import static java.math.BigDecimal.INFLATED;
49+
4950
import java.util.Arrays;
5051

51-
class MutableBigInteger {
52+
public class MutableBigInteger {
5253
/**
5354
* Holds the magnitude of this MutableBigInteger in big endian order.
5455
* The magnitude may start at an offset into the value array, and it may
@@ -516,7 +517,8 @@ boolean isNormal() {
516517
/**
517518
* Returns a String representation of this MutableBigInteger in radix 10.
518519
*/
519-
public String toString() {
520+
@Override
521+
public String toString() {
520522
BigInteger b = toBigInteger(1);
521523
return b.toString();
522524
}
@@ -2077,7 +2079,7 @@ MutableBigInteger modInverseMP2(int k) {
20772079
/**
20782080
* SwingJS: Returns the multiplicative inverse of val mod 2^24. Assumes val is odd.
20792081
*/
2080-
static int inverseMod24(int val) {
2082+
public static int inverseMod24(int val) {
20812083
// we must use 24 bits in SwingJS for 24-bit integer
20822084
// see http://marc-b-reynolds.github.io/math/2017/09/18/ModInverse.html
20832085
// Newton's iteration!
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package test;
2+
3+
import java.math.BigDecimal;
4+
import java.math.BigInteger;
5+
import java.math.MathContext;
6+
import java.math.MutableBigInteger;
7+
import java.util.GregorianCalendar;
8+
9+
import org.apache.xerces.jaxp.datatype.XMLGregorianCalendarImpl;
10+
11+
12+
public class Test_BigDec extends Test_ {
13+
14+
15+
public static void main(String[] args) {
16+
17+
testBDMul();
18+
testBDDiv();
19+
20+
21+
}
22+
23+
// failing testBDMul();
24+
25+
26+
27+
private static void testBDMul() {
28+
BigDecimal g;
29+
g = new BigDecimal(1);
30+
g = new BigDecimal(200.05);
31+
System.out.println("200.05 = " + g);
32+
assert (g.toString().equals("200.05000000000001136868377216160297393798828125"));
33+
System.out.println("200 = " + g.toBigInteger());
34+
assert (g.toBigInteger().toString().equals("200"));
35+
36+
BigDecimal e = new BigDecimal("200.05");
37+
System.out.println(e);
38+
BigDecimal f = new BigDecimal(45000);
39+
System.out.println(" f = " + f);
40+
assert(f.compareTo(new BigDecimal(45000)) == 0);
41+
g = f.multiply(e);
42+
System.out.println(" f * 200.05 = " + g);
43+
assert(g.toString().equals("9002250.00"));
44+
45+
f = f.movePointLeft(3);
46+
System.out.println(" f << 3 " + f);
47+
assert(f.compareTo(new BigDecimal(45000)) == -1);
48+
f = new BigDecimal(45000000L);
49+
System.out.println(" f = " + f);
50+
f = f.movePointLeft(6);
51+
System.out.println(" f << 6 " + f);
52+
assert(f.compareTo(new BigDecimal("4500.00")) == -1);
53+
assert(f.compareTo(new BigDecimal("0.00450000")) == 1);
54+
55+
56+
57+
long time = 1538673122263L;//System.currentTimeMillis();
58+
g = BigDecimal.valueOf(time, 3);
59+
System.out.println(time + " " + g);
60+
assert (g.toString().equals("1538673122.263"));
61+
62+
g = new BigDecimal(200.05, new MathContext(6));
63+
System.out.println(g);
64+
assert(g.toString().equals("200.050"));
65+
66+
67+
System.out.println("testBDMul OK");
68+
}
69+
70+
private static void testBDDiv() {
71+
BigDecimal g;
72+
g = BigDecimal.valueOf(5).divide(BigDecimal.valueOf(2));
73+
System.out.println(g);
74+
assert(g.toString().equals("2.5"));
75+
System.out.println("testBDDiv OK");
76+
}
77+
78+
79+
}
80+
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package test;
2+
3+
import test.math.BigDecimal;
4+
import test.math.MathContext;
5+
6+
public class Test_BigDecJava extends Test_ {
7+
8+
9+
public static void main(String[] args) {
10+
11+
testBDMul();
12+
testBDDiv();
13+
14+
15+
}
16+
17+
// failing testBDMul();
18+
19+
20+
21+
private static void testBDMul() {
22+
BigDecimal g;
23+
24+
g = new BigDecimal(200.05);
25+
System.out.println("200.05 = " + g);
26+
System.out.println("200 = " + g.toBigInteger());
27+
g = new BigDecimal(200.05, new MathContext(6));
28+
System.out.println(g);
29+
long time = 1538673122263L;//System.currentTimeMillis();
30+
g = BigDecimal.valueOf(time, 3);
31+
System.out.println(time + " " + g);
32+
assert (g.toString().equals("1538673122.263"));
33+
34+
BigDecimal e = new BigDecimal("200.05");
35+
System.out.println(e);
36+
BigDecimal f = new BigDecimal(45000);
37+
System.out.println(f);
38+
g = f.multiply(e);
39+
System.out.println(g);
40+
assert(g.toString().equals("9002250.00"));
41+
assert(f.compareTo(new BigDecimal(45000)) == 0);
42+
43+
f = f.movePointLeft(3);
44+
System.out.println(f);
45+
assert(f.compareTo(new BigDecimal(45000)) == -1);
46+
f = new BigDecimal(45000000L);
47+
System.out.println(f);
48+
f = f.movePointLeft(6);
49+
System.out.println(f);
50+
assert(f.compareTo(new BigDecimal("4500.00")) == -1);
51+
assert(f.compareTo(new BigDecimal("0.00450000")) == 1);
52+
System.out.println("testBDMul OK");
53+
}
54+
55+
private static void testBDDiv() {
56+
BigDecimal g;
57+
g = BigDecimal.valueOf(5).divide(BigDecimal.valueOf(2));
58+
System.out.println(g);
59+
assert(g.toString().equals("2.5"));
60+
System.out.println("testBDDiv OK");
61+
}
62+
63+
64+
}
65+

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

Lines changed: 8 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,38 @@
33
import java.math.BigDecimal;
44
import java.math.BigInteger;
55
import java.math.MathContext;
6+
import java.math.MutableBigInteger;
67
import java.util.GregorianCalendar;
78

89
import org.apache.xerces.jaxp.datatype.XMLGregorianCalendarImpl;
910

1011

1112
public class Test_BigInt extends Test_ {
1213

13-
static int inverseMod24(int val) {
14-
// Newton's iteration!
15-
int t = val;
16-
/**
17-
* @j2sNative
18-
*
19-
* t = (t * (2 - (val*t)&0xFFFFFF))&0xFFFFFF;
20-
* t = (t * (2 - (val*t)&0xFFFFFF))&0xFFFFFF;
21-
* t = (t * (2 - (val*t)&0xFFFFFF))&0xFFFFFF;
22-
*/
23-
return t;
24-
}
2514

2615
public static void main(String[] args) {
2716

28-
// failing testBDMul();
29-
30-
31-
32-
assert(((inverseMod24(5) * 5)&0xFFFFFF) == 1);
33-
17+
testInverseMod();
3418
testSub();
3519
testDiv3();
3620
testSquare();
3721
testPow10();
38-
3922
testBI();
40-
4123
testMulOdd();
4224
testMulEven();
4325
testModPow2();
4426
testModPow();
45-
4627
testShift();
47-
4828
testCalendar();
4929

5030
}
5131

32+
private static void testInverseMod() {
33+
for (int n = 1; n < 10000; n+=2)
34+
assert(((MutableBigInteger.inverseMod24(n) * n)&0xFFFFFF) == 1);
35+
System.out.println("testInverseMod OK");
36+
}
37+
5238
private static void testDiv3() {
5339
BigInteger m = BigInteger.valueOf(9);
5440
BigInteger three = BigInteger.valueOf(3);
@@ -210,48 +196,6 @@ private static void testBI() {
210196
System.out.println("testBigInt OK");
211197
}
212198

213-
private static void testBDMul() {
214-
BigDecimal g;
215-
216-
// g = new BigDecimal(200.05);
217-
// System.out.println(g.toBigInteger());
218-
// System.out.println(g);
219-
g = new BigDecimal(200.05, new MathContext(6));
220-
System.out.println(g);
221-
long time = 1538673122263L;//System.currentTimeMillis();
222-
g = BigDecimal.valueOf(time, 3);
223-
System.out.println(time + " " + g);
224-
assert (g.toString().equals("1538673122.263"));
225-
226-
BigDecimal e = new BigDecimal("200.05");
227-
System.out.println(e);
228-
BigDecimal f = new BigDecimal(45000);
229-
System.out.println(f);
230-
g = f.multiply(e);
231-
System.out.println(g);
232-
assert(g.toString().equals("9002250.00"));
233-
assert(f.compareTo(new BigDecimal(45000)) == 0);
234-
235-
f = f.movePointLeft(3);
236-
System.out.println(f);
237-
assert(f.compareTo(new BigDecimal(45000)) == -1);
238-
f = new BigDecimal(45000000L);
239-
System.out.println(f);
240-
f = f.movePointLeft(6);
241-
System.out.println(f);
242-
assert(f.compareTo(new BigDecimal("4500.00")) == -1);
243-
assert(f.compareTo(new BigDecimal("0.00450000")) == 1);
244-
System.out.println("testBDMul OK");
245-
}
246-
247-
private static void testBDDiv() {
248-
BigDecimal g;
249-
g = BigDecimal.valueOf(5).divide(BigDecimal.valueOf(2));
250-
System.out.println(g);
251-
assert(g.toString().equals("2.5"));
252-
System.out.println("testBDDiv OK");
253-
}
254-
255199
private static void testMulOdd() {
256200
BigInteger x = new BigInteger("7");
257201
BigInteger y = new BigInteger("13");

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package test;
22

3-
import test.math.BigDecimal;
4-
import test.math.BigInteger;
5-
import test.math.MathContext;
63
import java.util.GregorianCalendar;
74

85
import org.apache.xerces.jaxp.datatype.XMLGregorianCalendarImpl;
96

7+
import test.math.BigDecimal;
8+
import test.math.BigInteger;
9+
import test.math.MathContext;
10+
1011

1112
public class Test_BigIntJava extends Test_ {
1213

0 commit comments

Comments
 (0)