📜  Java中的 BigInteger add() 方法及示例

📅  最后修改于: 2022-05-13 01:55:03.491000             🧑  作者: Mango

Java中的 BigInteger add() 方法及示例


Java.math.BigInteger.add(BigInteger val)用于计算两个 BigInteger 的算术和。该方法用于查找大量范围的算术加法,该范围远大于Java的最大数据类型 double 的范围,而不影响结果的精度。此方法对调用此方法的当前 BigInteger 执行操作,并将 BigInteger 作为参数传递。

句法:

public BigInteger add(BigInteger val)

参数:此方法接受参数val ,该参数是要添加到此 BigInteger 的值。

返回值:此方法返回一个 BigInteger,其中包含 sum (this + val)。

下面的程序用于说明 BigInteger 的 add() 方法。

示例 1:

// Java program to demonstrate
// add() method of BigInteger
  
import java.math.BigInteger;
  
public class GFG {
    public static void main(String[] args)
    {
        // BigInteger object to store result
        BigInteger sum;
  
        // For user input
        // Use Scanner or BufferedReader
  
        // Two objects of String created
        // Holds the values to calculate the sum
        String input1
            = "5454564684456454684646454545";
        String input2
            = "4256456484464684864864864864";
  
        // Convert the string input to BigInteger
        BigInteger a
            = new BigInteger(input1);
        BigInteger b
            = new BigInteger(input2);
  
        // Using add() method
        sum = a.add(b);
  
        // Display the result in BigInteger
        System.out.println("The sum of\n"
                           + a + " \nand\n" + b + " "
                           + "\nis\n" + sum + "\n");
    }
}

输出:

正如您从上面的示例中看到的那样,即使添加了非常大的长度,数据也是完全精确的。

示例 2:

// Java program to demonstrate
// add() method of BigInteger
  
import java.math.BigInteger;
  
public class GFG {
    public static void main(String[] args)
    {
        // BigInteger object to store result
        BigInteger sum;
  
        // double variable
        // To store result as double
        double d;
  
        // For user input
        // Use Scanner or BufferedReader
  
        // Two objects of String
        // Holds the values to sum
  
        // The number's length is of 400 digits
        // Which is out of range of double
        String input1 = "012345678901234567"
                        + "8901234567890123"
                        + "4567890123456789"
                        + "0123456789012345"
                        + "6789012345678901"
                        + "2345678901234567"
                        + "8901234567890123"
                        + "4567890123456789"
                        + "0123456789012345"
                        + "6789012345678901"
                        + "2345678901234567"
                        + "8901234567890123"
                        + "4567890123456789"
                        + "0123456789012345"
                        + "6789012345678901"
                        + "2345678901234567"
                        + "8901234567890123"
                        + "4554324324362432"
                        + "7674637264783264"
                        + "7832678463726478"
                        + "3264736274673864"
                        + "7364732463546354"
                        + "6354632564532645"
                        + "6325463546536453"
                        + "6546325463546534"
                        + "6325465345326456"
                        + "4635463263453264"
                        + "654632498739473";
        String input2 = "0123456789012345"
                        + "6789012345678901"
                        + "2345678901234567"
                        + "8901234567890123"
                        + "4567890123456789"
                        + "0123456789012345"
                        + "6789012345678901"
                        + "2345678901234567"
                        + "8901234567890123"
                        + "4567890123456789"
                        + "0123456789012345"
                        + "6789012345678901"
                        + "2345678901234567"
                        + "8901234567890123"
                        + "4567890123456789"
                        + "0123456789012345"
                        + "6789012345678901"
                        + "2345873271893718"
                        + "2974897146378481"
                        + "7489127847281478"
                        + "2174871248721847"
                        + "8748327463756475"
                        + "6745781641263981"
                        + "2839721897438974"
                        + "3286574365764576"
                        + "2376914689217817"
                        + "4362473624721647"
                        + "61247612748612746";
  
        // convert the string input to BigInteger
        BigInteger a
            = new BigInteger(input1);
        BigInteger b
            = new BigInteger(input2);
  
        // Using add() method
        sum = a.add(b);
  
        // Display the result in BigInteger
        System.out.println("The sum of\n"
                           + a + " \nand\n" + b + " "
                           + "\nis\n" + sum);
  
        // Using double to hold  the result
        d = Double.parseDouble(sum.toString());
  
        // Display the result in double
        System.out.println("Using double, Sum is "
                           + d);
    }
}

输出:

从上面的输出中可以清楚地看出,对更大的整数使用双精度并不是一个好的选择。
参考: Java : Java(Java )