BigInteger错误答案

我使用java计算一个简单的组合,当我定义结果为双精度时,答案似乎是正确的,只是失去了一些精度,但是当我使用BigInteger类时,答案看起来根本就是错误的,我不明白为什么会这样,这是代码。

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


public class Test {

public static void main(String[] args){

    Scanner in = new Scanner(System.in);
    System.out.println("Please input the lottery sum");
    int num = in.nextInt();
    System.out.println("Please input the available sum");
    int avail = in.nextInt();

    //double sum = 1;
    BigInteger sum = BigInteger.ONE;

    for (int i = avail - 1; i >= 1;i--){
        //sum = sum*(num - i) / i;
        sum = sum.multiply(BigInteger.valueOf(num - i)).divide(BigInteger.valueOf(i));
    }

    //sum = sum * num / avail;
    sum = sum.multiply(BigInteger.valueOf(num)).divide(BigInteger.valueOf(avail));
    System.out.println(sum);

    in.close();
}
}

转载请注明出处:http://www.cntdm.com/article/20230511/1071558.html