ITeye博客

BigInteger、BigDecimal详解

AutomaticThoughts · 2012-07-26 17:22 · Java · 编程语言 · Markdown

BigInteger、BigDecimal详解

JavaUPBlog 

原文地址 http://lavasoft.blog.51cto.com/62575/228705
从Java4到Java5,Java对BigInteger、BigDecimal两个类功能一直再做扩展与改进。主要原因是这两个数据类型很重要,在高精度的计算中全靠这两个数据类型了。BigInteger和BigDecimal分别表示任意精度的整数与浮点数。

本文中不在追溯各个版本的变化,只看Java5中两个类的使用。

一、java.math.BigInteger

不可变的任意精度的整数。 此类的用法比较简单些,也不存在舍入等操作。

Java代码 复制代码 收藏代码

  1. package lavasoft;
  2. import java.math.BigInteger;
  3. import java.util.Random;
  4. /**
  5. * 测试BigInteger
  6. *
  7. * @author leizhimin 2009-11-17 12:49:41
  8. */
  9. public class TestBigInteger {
  10. public static void main(String[] args) {
  11. System.out.println("-------------------构造BigInteger---------------------");
  12. //通过byte数组来创建BigInteger
  13. BigInteger bi1 = new BigInteger(new byte[]{1, 1});
  14. System.out.println("bi1=" + bi1.toString());
  15. //创建带符号的BigInteger
  16. BigInteger bi2 = new BigInteger(-1, new byte[]{1, 1});
  17. System.out.println("bi2=" + bi2.toString());
  18. //创建带符号的BigInteger随机数
  19. BigInteger bi3 = new BigInteger(128, 20, new Random());
  20. System.out.println("bi3=" + bi3.toString());
  21. //通过10进制字符串创建带符号的BigInteger
  22. BigInteger bi4 = new BigInteger("12342342342342123423423412341");
  23. System.out.println("bi4=" + bi4.toString());
  24. //通过10进制字符串创建带符号的BigInteger
  25. BigInteger bi5 = new BigInteger("88888888888888888888888888888", Character.digit('a', 33));
  26. System.out.println("bi5=" + bi5.toString());
  27. System.out.println("BigInteger的常量:");
  28. System.out.println("BigInteger.ZERO=" + BigInteger.ZERO);
  29. System.out.println("BigInteger.ONE=" + BigInteger.ONE);
  30. System.out.println("BigInteger.TEN=" + BigInteger.TEN);
  31. System.out.println("-------------------使用BigInteger---------------------");
  32. System.out.println("bi1的相反数=" + bi1.negate());
  33. System.out.println("bi1的相反数=" + bi1.negate());
  34. System.out.println("bi1+bi2=" + bi1.add(bi2));
  35. System.out.println("bi1-bi2=" + bi1.subtract(bi2));
  36. System.out.println("bi1*bi2=" + bi1.multiply(bi2));
  37. System.out.println("bi1/bi2=" + bi1.divide(bi2));
  38. System.out.println("bi1的10次方=" + bi1.pow(10));
  39. System.out.println("bi1的10次方=" + bi1.pow(1));
  40. BigInteger[] bx = bi4.divideAndRemainder(bi1);
  41. System.out.println(">>>:bx[0]=" + bx[0] + ",bx[1]=" + bx[1]);
  42. System.out.println("bi2的绝对值=" + bi2.abs());
  43. }
  44. }
  45. 运行结果:
  46. -------------------构造BigInteger---------------------
  47. bi1=257
  48. bi2=-257
  49. bi3=175952079487573456985958549621373190227
  50. bi4=12342342342342123423423412341
  51. bi5=88888888888888888888888888888
  52. BigInteger的常量:
  53. BigInteger.ZERO=0
  54. BigInteger.ONE=1
  55. BigInteger.TEN=10
  56. -------------------使用BigInteger---------------------
  57. bi1的相反数=-257
  58. bi1的相反数=-257
  59. bi1+bi2=0
  60. bi1-bi2=514
  61. bi1*bi2=-66049
  62. bi1/bi2=-1
  63. bi1的10次方=1256988294225653106805249
  64. bi1的10次方=257
  65. >>>:bx[0]=48024678374872075577523005,bx[1]=56
  66. bi2的绝对值=257
  67. Process finished with exit code 0
package lavasoft;

import java.math.BigInteger;
import java.util.Random;

/**
* 测试BigInteger
*
* @author leizhimin 2009-11-17 12:49:41
*/
public class TestBigInteger {
        public static void main(String[] args) {
                System.out.println("-------------------构造BigInteger---------------------");
                //通过byte数组来创建BigInteger
                BigInteger bi1 = new BigInteger(new byte[]{1, 1});
                System.out.println("bi1=" + bi1.toString());
                //创建带符号的BigInteger
                BigInteger bi2 = new BigInteger(-1, new byte[]{1, 1});
                System.out.println("bi2=" + bi2.toString());
                //创建带符号的BigInteger随机数
                BigInteger bi3 = new BigInteger(128, 20, new Random());
                System.out.println("bi3=" + bi3.toString());
                //通过10进制字符串创建带符号的BigInteger
                BigInteger bi4 = new BigInteger("12342342342342123423423412341");
                System.out.println("bi4=" + bi4.toString());
                //通过10进制字符串创建带符号的BigInteger
                BigInteger bi5 = new BigInteger("88888888888888888888888888888", Character.digit('a', 33));
                System.out.println("bi5=" + bi5.toString());
                System.out.println("BigInteger的常量:");
                System.out.println("BigInteger.ZERO=" + BigInteger.ZERO);
                System.out.println("BigInteger.ONE=" + BigInteger.ONE);
                System.out.println("BigInteger.TEN=" + BigInteger.TEN);

                System.out.println("-------------------使用BigInteger---------------------");
                System.out.println("bi1的相反数=" + bi1.negate());
                System.out.println("bi1的相反数=" + bi1.negate());
                System.out.println("bi1+bi2=" + bi1.add(bi2));
                System.out.println("bi1-bi2=" + bi1.subtract(bi2));
                System.out.println("bi1*bi2=" + bi1.multiply(bi2));
                System.out.println("bi1/bi2=" + bi1.divide(bi2));
                System.out.println("bi1的10次方=" + bi1.pow(10));
                System.out.println("bi1的10次方=" + bi1.pow(1));
                BigInteger[] bx = bi4.divideAndRemainder(bi1);
                System.out.println(">>>:bx[0]=" + bx[0] + ",bx[1]=" + bx[1]);
                System.out.println("bi2的绝对值=" + bi2.abs());
        }
}
 
运行结果:
-------------------构造BigInteger---------------------
bi1=257
bi2=-257
bi3=175952079487573456985958549621373190227
bi4=12342342342342123423423412341
bi5=88888888888888888888888888888
BigInteger的常量:
BigInteger.ZERO=0
BigInteger.ONE=1
BigInteger.TEN=10
-------------------使用BigInteger---------------------
bi1的相反数=-257
bi1的相反数=-257
bi1+bi2=0
bi1-bi2=514
bi1*bi2=-66049
bi1/bi2=-1
bi1的10次方=1256988294225653106805249
bi1的10次方=257
>>>:bx[0]=48024678374872075577523005,bx[1]=56
bi2的绝对值=257

Process finished with exit code 0 

二、java.math.BigDecimal

不可变的、任意精度的有符号十进制数。与之相关的还有两个类:
java.math.MathContext:
该对象是封装上下文设置的不可变对象,它描述数字运算符的某些规则,如数据的精度,舍入方式等。
java.math.RoundingMode:这是一种枚举类型,定义了很多常用的数据舍入方式。

这个类用起来还是很比较复杂的,原因在于舍入模式,数据运算规则太多太多,不是数学专业出身的人看着中文API都难以理解,这些规则在实际中使用的时候在翻阅都来得及。

Java代码 复制代码 收藏代码

  1. package lavasoft;
  2. import java.math.BigDecimal;
  3. import java.math.MathContext;
  4. import java.math.RoundingMode;
  5. /**
  6. * 测试BigDecimal
  7. *
  8. * @author leizhimin 2009-11-17 12:50:03
  9. */
  10. public class TestBigDecimal {
  11. public static void main(String[] args) {
  12. System.out.println("------------构造BigDecimal-------------");
  13. //从char[]数组来创建BigDecimal
  14. BigDecimal bd1 = new BigDecimal("123456789.123456888".toCharArray(), 4, 12);
  15. System.out.println("bd1=" + bd1);
  16. //从char[]数组来创建BigDecimal
  17. BigDecimal bd2 = new BigDecimal("123456789.123456111133333213".toCharArray(), 4, 18, MathContext.DECIMAL128);
  18. System.out.println("bd2=" + bd2);
  19. //从字符串创建BigDecimal
  20. BigDecimal bd3 = new BigDecimal("123456789.123456111133333213");
  21. System.out.println("bd3=" + bd3);
  22. //从字符串创建BigDecimal,3是有效数字个数
  23. BigDecimal bd4 = new BigDecimal("88.456", new MathContext(3, RoundingMode.UP));
  24. System.out.println("bd4=" + bd4);
  25. System.out.println("------------使用BigDecimal-------------");
  26. System.out.println("bd1+bd2=" + bd1.add(bd2));
  27. System.out.println("bd1+bd2=" + bd1.add(bd2, new MathContext(24, RoundingMode.UP)));
  28. System.out.println("bd1-bd2=" + bd1.subtract(bd2).toPlainString());
  29. System.out.println("bd1-bd2=" + bd1.subtract(bd2, new MathContext(24, RoundingMode.UP)).toPlainString());
  30. System.out.println("bd1*bd2=" + bd1.multiply(bd2));
  31. System.out.println("bd1*bd2=" + bd1.multiply(bd2, new MathContext(24, RoundingMode.UP)));
  32. System.out.println("bd1/bd4=" + bd1.divideToIntegralValue(bd4));
  33. System.out.println("bd1/bd4=" + bd1.divideToIntegralValue(bd4, new MathContext(24, RoundingMode.UP)));
  34. System.out.println("bd1末位数据精度=" + bd1.ulp());
  35. System.out.println("bd2末位数据精度=" + bd2.ulp());
  36. System.out.println("bd2末位数据精度=" + bd2.ulp().toPlainString());
  37. System.out.println("bd1符号:" + bd1.signum());
  38. System.out.println("bd4的标度:" + bd4.scale());
  39. }
  40. }
  41. 运行结果:
  42. ------------构造BigDecimal-------------
  43. bd1=56789.123456
  44. bd2=56789.123456111133
  45. bd3=123456789.123456111133333213
  46. bd4=88.5
  47. ------------使用BigDecimal-------------
  48. bd1+bd2=113578.246912111133
  49. bd1+bd2=113578.246912111133
  50. bd1-bd2=-0.000000111133
  51. bd1-bd2=-0.000000111133
  52. bd1*bd2=3225004542.907120529593035648
  53. bd1*bd2=3225004542.90712052959304
  54. bd1/bd4=641.00000
  55. bd1/bd4=641.00000
  56. bd1末位数据精度=0.000001
  57. bd2末位数据精度=1E-12
  58. bd2末位数据精度=0.000000000001
  59. bd1符号:1
  60. bd4的标度:1
  61. Process finished with exit code 0
  62. 最后回顾下本文所涉及的API范围:
  63. java.math包:
  64. 类
  65. BigDecimal
  66. BigInteger
  67. MathContext
  68. 枚举
  69. RoundingMode
  70. 掌握了构造方式,和常见的数学运算,在开发中一般够用了。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。