挑战了一下Java的BigDecimal,结果很遗憾性能不佳,然亦有收获。
import javay.math.BigNum;
import javay.math.BigNumRound;
import javay.math.MathBn;
import javay.swing.CalcultorConts;
/**
* @author DBJ
*
*/
public class BigNumTest {
public void testbyte() throws Exception {
byte b = Byte.MIN_VALUE;
BigNum testa = BigNum.BYTE_MIN_VALUE;
while(b != Byte.MAX_VALUE) {
testa = testa.add(BigNum.ONE);
b ++;
if (b != testa.toByte()) {
System.out.println(b + "<>" + testa);
break ;
} else {
System.out.println(b + "==" + testa);
}
}
}
public void testshort() throws Exception {
short b = Short.MIN_VALUE;
BigNum testa = BigNum.SHORT_MIN_VALUE;
while(b != Short.MAX_VALUE) {
testa = testa.add(BigNum.ONE);
b ++;
if (b != testa.toShort()) {
System.out.println(b + "<>" + testa);
throw new Exception(b + "<>" + testa);
// break ;
} else {
System.out.println(b + "==" + testa);
}
}
}
public void testint() throws Exception {
int b = Integer.MIN_VALUE;
BigNum testa = BigNum.INT_MIN_VALUE;
while(b != Integer.MAX_VALUE) {
testa = testa.add(BigNum.ONE);
b ++;
if (b != testa.toInt()) {
System.out.println(b + "<>" + testa);
throw new Exception(b + "<>" + testa);
// break ;
} else {
System.out.println(b + "==" + testa);
}
}
}
public void testlong() throws Exception {
long b = Long.MIN_VALUE;
BigNum testa = BigNum.LONG_MIN_VALUE;
while(b != Long.MAX_VALUE) {
testa = testa.add(BigNum.ONE);
b ++;
if (b != testa.toLong()) {
System.out.println(b + "<>" + testa);
throw new Exception(b + "<>" + testa);
// break ;
} else {
System.out.println(b + "==" + testa);
}
}
}
public void testSubtract() throws Exception {
// byte b = Byte.MAX_VALUE;
// BigNum testa = BigNum.BYTE_MAX_VALUE;
// while(b != Byte.MIN_VALUE) {
// testa = testa.subtract(BigNum.ONE);
// b --;
// if (b != testa.toByte()) {
// System.out.println(b + "<>" + testa);
// break ;
// } else {
// System.out.println(b + "==" + testa);
// }
// }
BigNum s = BigNum.PI.subtract(new BigNum("0.1"));
System.out.println(s);
}
public void print99() throws Exception {
for(int i = 1; i <= 9; i ++) {
for (int j = 1; j <= 9; j ++) {
BigNum val = new BigNum("" + i).multiply(new BigNum("" + j));
if ((i * j) != val.toLong()) {
System.out.println((i * j) + "<>" + val.toLong());
throw new Exception((i * j) + "<>" + val.toLong());
} else {
System.out.println((i * j) + "==" + val.toLong());
}
}
System.out.println();
}
}
public void testDivide() throws Exception {
// byte b = Byte.MAX_VALUE;
// BigNum testa = BigNum.BYTE_MAX_VALUE;
// while(b != Byte.MIN_VALUE) {
// BigNum testc = testa.divide(new BigNum("2"), 0, 0);
// byte c = (byte) (b / 2);
// testa = testa.subtract(BigNum.ONE);
// b --;
// if (c != testc.toByte()) {
// System.out.println(c + "<>" + testc);
// throw new Exception(c + "<>" + testc);
//// break ;
// } else {
// System.out.println(c + "==" + testc);
// }
// }
BigNum bg15 = new BigNum("15.0");
BigNum bg180 = new BigNum("180.0");
BigNum r = bg15.divide(bg180, CalcultorConts.DECIMAL_LEN, BigNumRound.HALF_EVENT);
System.out.println(r);
BigNum bg104508 = new BigNum("10.4508");
BigNum bg2 = new BigNum("2.0");
r = bg104508.divide(bg2, CalcultorConts.DECIMAL_LEN, BigNumRound.HALF_EVENT);
System.out.println("r=" + r);
BigNum bg30 = new BigNum("30.0");
r = bg30.divide(bg180, CalcultorConts.DECIMAL_LEN, BigNumRound.HALF_EVENT);
System.out.println("r=" + r);
BigNum bg45 = new BigNum("45.0");
r = bg45.divide(bg180, CalcultorConts.DECIMAL_LEN, BigNumRound.HALF_EVENT);
System.out.println("r=" + r);
BigNum a = new BigNum("0.143547577225009399403922177216000");
BigNum b = new BigNum("121645100408832000.000000000");
BigNum c = a.divide(b, CalcultorConts.DECIMAL_LEN, BigNumRound.HALF_EVENT);
System.out.println("c=" + c);
a = new BigNum("0.02");
b = new BigNum("40.0");
c = a.divide(b, CalcultorConts.DECIMAL_LEN, BigNumRound.HALF_EVENT);
System.out.println("c=" + c);
}
public void testDivide2() {
BigNum a = new BigNum("1.0");
BigNum b = new BigNum("0.000244140625");
BigNum c = a.divide(b, 40, BigNumRound.HALF_EVENT);
System.out.println(c);
}
public void testMod() {
BigNum test324 = new BigNum("3.24");
BigNum test03 = new BigNum("0.3");
BigNum test = test324.mod(test03);
System.out.println(test);
BigNum test3 = new BigNum("3");
test = test3.mod(test3);
System.out.println("3 % 3 = " + test);
BigNum test324a = new BigNum("324");
test = test324a.mod(test3);
System.out.println("324 % 3 = " + test);
test324 = new BigNum("3.24");
BigNum test003 = new BigNum("0.03");
test = test324.mod(test003);
System.out.println("3.24 % 0.03 = " + test);
}
public void test003() {
BigNum test003 = new BigNum("0.03");
System.out.println("0.03=" + test003);
}
public void testCmp() {
BigNum test = new BigNum(0);
// test.test_cmp_ary();
}
public void testDouble() {
// double a = 15.3;
// BigNum b = new BigNum(a);
// System.out.println(a + "=" + b.toString() + "," + b.toDouble(CalcultorConts.MAX_DOUBLE_SCALE) + "------------------------>");
//// BigNum c = b.log10();
// System.out.println("<---------------------log10(" + b + ")=" + c + "," + Math.log10(a));
//
// BigNum e = b.ln(3, BigNumRound.HALF_EVENT);
// System.out.println("<---------------------ln(" + b + ")=" + e + "," + Math.log1p(a));
}
public void testRD() {
for (int i = 0; i <= 180; i += 15) {
BigNum r = MathBn.toRadians(new BigNum(i));
BigNum d = MathBn.toDegrees(r);
System.out.println(i + "=" + r + "," + d);
}
}
public void testRound() {
BigNum data = new BigNum("1.2345");
// data.test_add_ary();
BigNum dat = data.round(1, BigNumRound.UP);
System.out.println(dat);
dat = data.round(1, BigNumRound.DOWN);
System.out.println(dat);
dat = data.round(1, BigNumRound.CELLING);
System.out.println(dat);
dat = data.round(1, BigNumRound.FLOOR);
System.out.println(dat);
dat = data.round(1, BigNumRound.HALF_UP);
System.out.println(dat);
dat = data.round(1, BigNumRound.HALF_DOWN);
System.out.println(dat);
dat = data.round(1, BigNumRound.HALF_EVENT);
System.out.println(dat);
data = new BigNum("5.2254");
dat = data.round(2, BigNumRound.HALF_EVENT);
System.out.println(dat);
data = new BigNum("5.215");
dat = data.round(2, BigNumRound.HALF_EVENT);
System.out.println(dat);
data = new BigNum("5.225");
dat = data.round(2, BigNumRound.HALF_EVENT);
System.out.println(dat);
data = new BigNum("5.214");
dat = data.round(2, BigNumRound.HALF_EVENT);
System.out.println(dat);
}
public void testToRadians() {
for (int degree = 0; degree <= 360; degree += 15) {
if (degree % 30 == 0 || degree % 45 == 0) {
BigNum d = new BigNum(degree);
BigNum r1 = MathBn.toRadians(d);
double r2 = Math.toRadians(degree);
System.out.println(String.format("%3d : %s - %13.10f = %13.10f", degree, r1, r2, r1.toDouble(CalcultorConts.MAX_DOUBLE_SCALE) - r2));
System.out.println();
}
}
}
public void testSin() {
for (int degree = 0; degree <= 360; degree += 15) {
if (degree % 30 == 0 || degree % 45 == 0) {
BigNum d = new BigNum(degree);
BigNum radian = MathBn.toRadians(d);
BigNum d1 = MathBn.sin(radian);
double d2 = Math.sin(radian.toDouble(CalcultorConts.MAX_DOUBLE_SCALE));
System.out.println(String.format("%3d : %s - %13.10f = %13.10f", degree, d1, d2, d1.toDouble(CalcultorConts.MAX_DOUBLE_SCALE) - d2));
System.out.println("****************************************************************");
}
}
}
public void testMultiply() {
BigNum a = new BigNum("0.25");
BigNum b = a.multiply(BigNum.PI);
System.out.println("0.25 * PI =" + b);
BigNum n4 = new BigNum("4.0");
BigNum c = BigNum.PI.divide(n4, CalcultorConts.DECIMAL_LEN, BigNumRound.HALF_EVENT);
System.out.println(c);
a = new BigNum("0.5");
BigNum d = a.multiply(BigNum.PI);
System.out.println("0.5 * PI =" + d);
a = new BigNum("30.0");
BigNum e = a.multiply(BigNum.PI);
System.out.println("30.0 * PI =" + e);
}
public void testAdd() {
BigNum s = BigNum.PI.add(BigNum.PI);
System.out.println("2pi=" + s);
}
public void testLastZero() {
BigNum a = new BigNum("1.23000000");
System.out.println("a=" + a);
a = new BigNum("30.0");
System.out.println("a=" + a);
a = new BigNum("300.0");
System.out.println("a=" + a);
}
public void testLogic() {
// BigNum a = new BigNum("1.25");
// BigNum b = new BigNum("3.75");
// System.out.println("AND" + a.and(b));
// System.out.println(" OR" + a.or(b));
// System.out.println("XOR" + a.xor(b));
BigNum a = new BigNum("2.125");
// BigNum b = new BigNum("4");
System.out.println(a.rsh(new BigNum("0")));
System.out.println(a.rsh(new BigNum("1")));
System.out.println(a.rsh(new BigNum("2")));
System.out.println(a.rsh(new BigNum("3")));
System.out.println(a.rsh(new BigNum("4")));
System.out.println(a.rsh(new BigNum("5")));
System.out.println(a.rsh(new BigNum("6")));
System.out.println(a.rsh(new BigNum("7")));
System.out.println(a.rsh(new BigNum("8")));
// System.out.println(a.lsh(b));
System.out.println("------and----");
System.out.println(new BigNum("1").and(new BigNum("2")));
}
public void testSCI() {
BigNum a = new BigNum("0.00001234");
System.out.println(a.toScientificNotation());
System.out.println(new BigNum("0.0000").toScientificNotation());
System.out.println(new BigNum("123450").toScientificNotation());
}
public void testAddxx() {
BigNum a = new BigNum("45.6");
BigNum b = new BigNum("78.9");
a = new BigNum("99.9");
b = new BigNum("2.0");
// b = new BigNum("10000000000000000000000000.0000000000000001");
// BigNum c = a.multiply(b);
// System.out.println(a + "*" + b + "=" + c);
// BigNum c = a.divide(b, 2, BigNumRound.HALF_EVENT);
BigNum c = a.add(b);
System.out.println(a + "/" + b + "=" + c);
// System.out.println(a.toString());
}
public void test(int testno, String input, String plan) throws Exception {
BigNum a = new BigNum(input);
String str2 = a.toString();
if(!plan.equals(str2)) throw new Exception("ERROR-- " + testno + " --" + input + "=" + plan + " vs " + str2);
else System.out.println("-- " + testno + " --" + input + "=" + plan + " vs " + str2);
}
public void testStr() throws Exception {
System.out.println("--------------------------------- TEST BEGIN==>");
test( 1, ".", "0.0");
test( 2, "+.", "0.0");
test( 3, "-.", "-0.0");
test( 4, "0.", "0.0");
test( 5, "+0.", "0.0");
test( 6, "-0.", "-0.0");
test( 7, ".0", "0.0");
test( 8, "+.0", "0.0");
test( 9, "-.0", "-0.0");
test(10, "0", "0.0");
test(11, "+0", "0.0");
test(12, "-0", "-0.0");
test(13, "0.0", "0.0");
test(14, "+0.0", "0.0");
test(15, "-0.0", "-0.0");
test(16, "45", "45.0");
test(17, "+45", "45.0");
test(18, "-45", "-45.0");
test(19, "45.", "45.0");
test(20, "+45.", "45.0");
test(21, "-45.", "-45.0");
test(22, "45.0", "45.0");
test(23, "+45.0", "45.0");
test(24, "-45.0", "-45.0");
test(25, ".67", "0.67");
test(26, "+.67", "0.67");
test(27, "-.67", "-0.67");
test(28, "0.67", "0.67");
test(29, "+0.67", "0.67");
test(30, "-0.67", "-0.67");
test(31, "45.67", "45.67");
test(32, "+45.67", "45.67");
test(33, "-45.67", "-45.67");
test(34, "00.", "0.0");
test(35, "+00.", "0.0");
test(36, "-00.", "-0.0");
test(37, ".00", "0.0");
test(38, "+.00", "0.0");
test(39, "-.00", "-0.0");
test(40, "00", "0.0");
test(41, "+00", "0.0");
test(42, "-00", "-0.0");
test(43, "00.0", "0.0");
test(44, "+00.0", "0.0");
test(45, "-00.0", "-0.0");
test(46, "45.00", "45.0");
test(47, "+45.00", "45.0");
test(48, "-45.00", "-45.0");
test(49, "00.67", "0.67");
test(50, "+00.67", "0.67");
test(51, "-00.67", "-0.67");
test(52, "00.00", "0.0");
test(53, "+00.00", "0.0");
test(54, "-00.00", "-0.0");
test(55, "0.00", "0.0");
test(56, "+0.00", "0.0");
test(57, "-0.00", "-0.0");
test(58, "10", "10.0");
test(59, "+10", "10.0");
test(60, "-10", "-10.0");
}
// public static BigNum pi = new BigNum("3.14159265358979323846264338327950288419716939937510");
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// BigNum e = new BigNum("test");
// BigNum test = new BigNum("0.0T");
BigNumTest proc = new BigNumTest();
proc.testStr();
// proc.testbyte();
//// proc.testshort();
//// proc.testint();
//// proc.testlong();
// proc.testSubtract();
// proc.print99();
// proc.testMultiply();
// proc.testDivide();
//// proc.test003();
// proc.testCmp();
// proc.testMod();
// proc.testDouble();
// proc.testRD();
// proc.testRound();
// proc.testToRadians();
// proc.testSubtract();
// proc.testAdd();
// proc.testSin();
// proc.testLastZero();
// proc.testDivide2();
// proc.testLogic();
// proc.testSCI();
proc.testAddxx();
// System.out.println("byte" + Byte.MIN_VALUE + "...+" + Byte.MAX_VALUE); // 8
// System.out.println("short" + Short.MIN_VALUE + "...+" + Short.MAX_VALUE); // 16
// System.out.println("int" + Integer.MIN_VALUE + "...+" + Integer.MAX_VALUE); // 32
// System.out.println("long" + Long.MIN_VALUE + "...+" + Long.MAX_VALUE); // 64
// System.out.println(BigNumRound.UP.ordinal());
// BigNum test1 = new BigNum("0");
// System.out.println("---------------");
// BigNum test2 = new BigNum("1");
// System.out.println("---------------");
// BigNum test3 = new BigNum("9");
// System.out.println("---------------");
// BigNum test4 = new BigNum("10");
// for(int i = 0; i <=100; i ++) {
// // System.out.println("---------------");
// BigNum t = new BigNum("" + i);
// }
// BigNum test1 = new BigNum("1");
// BigNum test2 = new BigNum("2");
// BigNum test3 = test1.add(test2);
// System.out.println(test3);
// BigNum test40 = new BigNum("40");
// System.out.println(test40.abs() + "," + test40.toByte());
// BigNum test4 = new BigNum("4");
// System.out.println(test4.abs());
// if (test40.abs().compareTo(test4.abs()) < 0) {
// System.out.println("<");
// }
// BigNum test36 = test40.subtract(test4);
// System.out.println(test36);
// BigNum test20 = new BigNum("2.0");
// BigNum test12 = test1.multiply(test2);
// System.out.println(test12);
// BigNum test3 = new BigNum("3");
// BigNum test203 = test2.multiply(test3);
// System.out.println("2=" + test2 + ",*3=" + test3 + ",=" + test203);
// BigNum test4 = new BigNum("4");
//
// BigNum test43 = test4.subtract(test3);
// System.out.println(test43);
// System.out.println("---------------");
// BigNum test4 = new BigNum("126.0257");
// System.out.println(test4.toString());
// System.out.println("---------------");
// BigNum test5 = new BigNum("37");
// System.out.println(test5.toString());
// System.out.println("---------------");
// BigNum test6 = new BigNum("+655.36");
// System.out.println("---------------");
// BigNum test7 = new BigNum("65537");
// System.out.println("---------------");
// BigNum test8 = test4.add(test5);
// System.out.println(test8.toString());
// System.out.println("---------------");
// BigNum test9 = test4.multiply(test5);
// System.out.println(test9.toString());
// System.out.println("---------------");
// System.out.println(test4.compareTo(test5));
// System.out.println("---------------");
// BigNum test9 = test4.divide(test5, 0, 0);
// System.out.println(test9.toString());
// System.out.println("---------------");
// System.out.println(new BigNum("0.01260257").divide(test5, 0, 0));
// System.out.println(new BigNum("0.1260257").divide(test5, 0, 0));
// System.out.println(new BigNum("1.260257").divide(test5, 0, 0));
// System.out.println("---------------");
// System.out.println(new BigNum("12.60257").divide(test5, 0, 0));
// System.out.println("---------------");
// System.out.println(new BigNum("126.0257").divide(test5, 0, 0));
// System.out.println(new BigNum("1260.257").divide(test5, 0, 0));
// System.out.println(new BigNum("12602.57").divide(test5, 0, 0));
// System.out.println(new BigNum("126025.7").divide(test5, 0, 0));
// System.out.println(new BigNum("1260257.0").divide(test5, 0, 0));
// System.out.println(new BigNum("12602570.0").divide(test5, 0, 0));
//
// char[][] a = new char[2][3];
// a[0][0] = 'a';
// a[0][1] = 'b';
// a[0][2] = 'c';
// a[1][0] = 'A';
// a[1][1] = 'B';
// a[1][2] = 'C';
//
// for(int y = 0; y < a[0].length; y ++) {
// //System.out.print("y=" + y + "a=" + a.length + "a[0]=" + a[0].length);
// for (int x = 0; x < a.length; x ++) {
// System.out.print("a[" + x + "][" + y + "]=" + a[x][y]);
// }
// System.out.println();
// }
// System.out.println("End.");
// System.out.println(new BigNum("10").divide(new BigNum("3"), 1, 0));
// BigNum test121p = new BigNum("+12.0");
// System.out.println(test121p);
// BigNum test120p = new BigNum("+12.");
// System.out.println(test120p);
// BigNum test12p = new BigNum("+12");
// System.out.println(test12p);
// BigNum test121 = new BigNum("12.0");
// System.out.println(test121);
// BigNum test120 = new BigNum("12.");
// System.out.println(test120);
// BigNum test12 = new BigNum("12");
// System.out.println(test12);
// BigNum test121n = new BigNum("-12.0");
// System.out.println(test121n);
// BigNum test120n = new BigNum("-12.");
// System.out.println(test120n);
// BigNum test12n = new BigNum("-12");
// System.out.println(test12n);
// BigNum test23 = new BigNum("23");
// System.out.println(test23);
// BigNum value = test12.add(test23);
// System.out.println(value);
// // BigNum test744 = new BigNum("6006");
// BigNum test744 = new BigNum("9223372036854775808");
// // BigNum test744 = new BigNum("22222222222222222222");
// BigNum test2744 = new BigNum("9223372036854775808");
// System.out.println(test744.divide(test2744, 0, 0));
}
}
源代码:http://git.oschina.net/dubenju/encv/blob/master/src/java/javay/math/BigNum.java