Java核心技术卷1-3.9 大数值

156 阅读2分钟

    如果基本的整数和浮点数精度不能够满足需求,那么可以使用java.math包中的两个很有用的类:BigInteger和BigDecimal。这两个类可以处理包含任意长度数字序列的数值。BigInteger类实现了任意精度的整数运算,BigDecimal实现了任意精度的浮点数运算。

    使用静态的valueOf方法可以将普通的数值转换为大数值:

BigInteger a = BigInteger.valueOf(100);

    遗憾的是,不能使用人们熟悉的算术运算符(如:+和*)处理大数值。而需要使用大数值类中的add和multiply方法。

BigInteger c = a.add(b);//c = a + b
BigInteger d = c.multiply(b.add(BigInteger.valueOf(2)));//d = c * (b + 2)

C++注释:与C++不同,Java没有提供运算符重载功能。程序员无法重定义+和*运算符,使其应用于BigInteger类的add和multiply运算。Java语言的设计者确实为字符串的连接重载了+运算符,但没有重载其他的运算符,也没有给Java程序员在自己的类中重载运算符的机会

    程序清单3-6是对程序清单3-5中彩概率的改进,使其可以采用大数值进行运算。假设你被邀请参加抽奖活动,并从490个可能的数值中抽取60个,这个程序将会得到中彩概率1/716395843461995557415116222540092933411717612789263493493351013459481104668848。祝你好运。

程序清单 3-6 BigIntegerTest/BigIntegerTest.java

import java.math.BigInteger;
import java.util.Scanner;

public class BigIntegerTest {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        System.out.println("How much number do you need to draw?");
        int k = in.nextInt();

        System.out.println("What is the highest number you can draw?");
        int n = in.nextInt();
        /**
         * compute binomial coefficient n*(n-1)*(n-2)*...*(n-k+1)/(1*2*3*...*k)
         */
        BigInteger lotteryOdds = BigInteger.valueOf(1);
        for (int i = 1; i < k; i++) {
            lotteryOdds = lotteryOdds.multiply(BigInteger.valueOf(n - i + 1))
                    .divide(BigInteger.valueOf(i));

            System.out.println("Your odds are 1 in" + lotteryOdds + ".Good luck!");
        }
    }
}

   在程序清单3-5中,用于计算的语句是

lotteryOdds = lotteryOdds * (n-i+1) / i;

如果使用大数值,则相应的语句为:

lotteryOdds = lotteryOdds.multiply(BigInteger.valueOf(n - i + 1))
                    .divide(BigInteger.valueOf(i));

API java.math.BigInteger 1.1

  • BigInteger add(BigInteger val)
  • BigInteger subtract(BigInteger val)
  • BigInteger multiply(BigInteger val)
  • BigInteger divide(BigInteger val)
  • BigInteger mod(BigInteger m)

返回这个大整数和另一个大整数m的余数。

  • int compareTo(BigInteger val)

如果这个大整数与另一个大整数val相等,返回0;如果这个大整数小于另一个大整数val,返回负数;否则,返回正数。

  • static BigInteger valueOf(long val)

返回值等于val的大整数