`
AutomaticThoughts
  • 浏览: 162639 次
社区版块
存档分类
最新评论

BigInteger、BigDecimal详解

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

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

一、java.math.BigInteger

不可变的任意精度的整数。 此类的用法比较简单些,也不存在舍入等操作。
Java代码 复制代码 收藏代码
  1. package lavasoft;   
  2.   
  3. import java.math.BigInteger;   
  4. import java.util.Random;   
  5.   
  6. /**  
  7. * 测试BigInteger  
  8. *  
  9. * @author leizhimin 2009-11-17 12:49:41  
  10. */  
  11. public class TestBigInteger {   
  12.         public static void main(String[] args) {   
  13.                 System.out.println("-------------------构造BigInteger---------------------");   
  14.                 //通过byte数组来创建BigInteger   
  15.                 BigInteger bi1 = new BigInteger(new byte[]{11});   
  16.                 System.out.println("bi1=" + bi1.toString());   
  17.                 //创建带符号的BigInteger   
  18.                 BigInteger bi2 = new BigInteger(-1new byte[]{11});   
  19.                 System.out.println("bi2=" + bi2.toString());   
  20.                 //创建带符号的BigInteger随机数   
  21.                 BigInteger bi3 = new BigInteger(12820new Random());   
  22.                 System.out.println("bi3=" + bi3.toString());   
  23.                 //通过10进制字符串创建带符号的BigInteger   
  24.                 BigInteger bi4 = new BigInteger("12342342342342123423423412341");   
  25.                 System.out.println("bi4=" + bi4.toString());   
  26.                 //通过10进制字符串创建带符号的BigInteger   
  27.                 BigInteger bi5 = new BigInteger("88888888888888888888888888888", Character.digit('a'33));   
  28.                 System.out.println("bi5=" + bi5.toString());   
  29.                 System.out.println("BigInteger的常量:");   
  30.                 System.out.println("BigInteger.ZERO=" + BigInteger.ZERO);   
  31.                 System.out.println("BigInteger.ONE=" + BigInteger.ONE);   
  32.                 System.out.println("BigInteger.TEN=" + BigInteger.TEN);   
  33.   
  34.                 System.out.println("-------------------使用BigInteger---------------------");   
  35.                 System.out.println("bi1的相反数=" + bi1.negate());   
  36.                 System.out.println("bi1的相反数=" + bi1.negate());   
  37.                 System.out.println("bi1+bi2=" + bi1.add(bi2));   
  38.                 System.out.println("bi1-bi2=" + bi1.subtract(bi2));   
  39.                 System.out.println("bi1*bi2=" + bi1.multiply(bi2));   
  40.                 System.out.println("bi1/bi2=" + bi1.divide(bi2));   
  41.                 System.out.println("bi1的10次方=" + bi1.pow(10));   
  42.                 System.out.println("bi1的10次方=" + bi1.pow(1));   
  43.                 BigInteger[] bx = bi4.divideAndRemainder(bi1);   
  44.                 System.out.println(">>>:bx[0]=" + bx[0] + ",bx[1]=" + bx[1]);   
  45.                 System.out.println("bi2的绝对值=" + bi2.abs());   
  46.         }   
  47. }   
  48.     
  49. 运行结果:   
  50. -------------------构造BigInteger---------------------   
  51. bi1=257  
  52. bi2=-257  
  53. bi3=175952079487573456985958549621373190227  
  54. bi4=12342342342342123423423412341  
  55. bi5=88888888888888888888888888888  
  56. BigInteger的常量:   
  57. BigInteger.ZERO=0  
  58. BigInteger.ONE=1  
  59. BigInteger.TEN=10  
  60. -------------------使用BigInteger---------------------   
  61. bi1的相反数=-257  
  62. bi1的相反数=-257  
  63. bi1+bi2=0  
  64. bi1-bi2=514  
  65. bi1*bi2=-66049  
  66. bi1/bi2=-1  
  67. bi1的10次方=1256988294225653106805249  
  68. bi1的10次方=257  
  69. >>>:bx[0]=48024678374872075577523005,bx[1]=56  
  70. bi2的绝对值=257  
  71.   
  72. 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.   
  3. import java.math.BigDecimal;   
  4. import java.math.MathContext;   
  5. import java.math.RoundingMode;   
  6.   
  7. /**  
  8. * 测试BigDecimal  
  9. *  
  10. * @author leizhimin 2009-11-17 12:50:03  
  11. */  
  12. public class TestBigDecimal {   
  13.   
  14.         public static void main(String[] args) {   
  15.                 System.out.println("------------构造BigDecimal-------------");   
  16.                 //从char[]数组来创建BigDecimal   
  17.                 BigDecimal bd1 = new BigDecimal("123456789.123456888".toCharArray(), 412);   
  18.                 System.out.println("bd1=" + bd1);   
  19.                 //从char[]数组来创建BigDecimal   
  20.                 BigDecimal bd2 = new BigDecimal("123456789.123456111133333213".toCharArray(), 418, MathContext.DECIMAL128);   
  21.                 System.out.println("bd2=" + bd2);   
  22.                 //从字符串创建BigDecimal   
  23.                 BigDecimal bd3 = new BigDecimal("123456789.123456111133333213");   
  24.                 System.out.println("bd3=" + bd3);   
  25.                 //从字符串创建BigDecimal,3是有效数字个数   
  26.                 BigDecimal bd4 = new BigDecimal("88.456"new MathContext(3, RoundingMode.UP));   
  27.                 System.out.println("bd4=" + bd4);   
  28.                 System.out.println("------------使用BigDecimal-------------");   
  29.                 System.out.println("bd1+bd2=" + bd1.add(bd2));   
  30.                 System.out.println("bd1+bd2=" + bd1.add(bd2, new MathContext(24, RoundingMode.UP)));   
  31.                 System.out.println("bd1-bd2=" + bd1.subtract(bd2).toPlainString());   
  32.                 System.out.println("bd1-bd2=" + bd1.subtract(bd2, new MathContext(24, RoundingMode.UP)).toPlainString());   
  33.                 System.out.println("bd1*bd2=" + bd1.multiply(bd2));   
  34.                 System.out.println("bd1*bd2=" + bd1.multiply(bd2, new MathContext(24, RoundingMode.UP)));   
  35.                 System.out.println("bd1/bd4=" + bd1.divideToIntegralValue(bd4));   
  36.                 System.out.println("bd1/bd4=" + bd1.divideToIntegralValue(bd4, new MathContext(24, RoundingMode.UP)));   
  37.                 System.out.println("bd1末位数据精度=" + bd1.ulp());   
  38.                 System.out.println("bd2末位数据精度=" + bd2.ulp());   
  39.                 System.out.println("bd2末位数据精度=" + bd2.ulp().toPlainString());   
  40.                 System.out.println("bd1符号:" + bd1.signum());   
  41.                 System.out.println("bd4的标度:" + bd4.scale());   
  42.         }   
  43. }   
  44.     
  45. 运行结果:   
  46. ------------构造BigDecimal-------------   
  47. bd1=56789.123456  
  48. bd2=56789.123456111133  
  49. bd3=123456789.123456111133333213  
  50. bd4=88.5  
  51. ------------使用BigDecimal-------------   
  52. bd1+bd2=113578.246912111133  
  53. bd1+bd2=113578.246912111133  
  54. bd1-bd2=-0.000000111133  
  55. bd1-bd2=-0.000000111133  
  56. bd1*bd2=3225004542.907120529593035648  
  57. bd1*bd2=3225004542.90712052959304  
  58. bd1/bd4=641.00000  
  59. bd1/bd4=641.00000  
  60. bd1末位数据精度=0.000001  
  61. bd2末位数据精度=1E-12  
  62. bd2末位数据精度=0.000000000001  
  63. bd1符号:1  
  64. bd4的标度:1  
  65.   
  66. Process finished with exit code 0  
  67.     
  68. 最后回顾下本文所涉及的API范围:   
  69.     
  70. java.math包:   
  71. 类    
  72. BigDecimal   
  73. BigInteger   
  74. MathContext   
  75. 枚举    
  76. RoundingMode   
  77.     
  78. 掌握了构造方式,和常见的数学运算,在开发中一般够用了。  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics