当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java BigInteger add()用法及代码示例



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

用法:

public BigInteger add(BigInteger val)

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


返回值:此方法返回一个保存有sum(this + val)的BigInteger。

下面的程序用于说明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"); 
    } 
}

输出:

The sum of
5454564684456454684646454545
and
4256456484464684864864864864
is
9711021168921139549511319409

从上面的示例中可以看到,即使在添加了非常大的长度的数字之后,数据也将被完全精确化。

示例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); 
    } 
}

输出:

The sum of
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234554324324362432767463726478326478326784637264783264736274673864736473246354635463546325645326456325463546536453654632546354653463254653453264564635463263453264654632498739473
and
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234587327189371829748971463784817489127847281478217487124872184787483274637564756745781641263981283972189743897432865743657645762376914689217817436247362472164761247612748612746
is
2469135780246913578024691357802469135780246913578024691357802469135780246913578024691357802469135780246913578024691357802469135780246913578024691357802469135780246913578024691357802469135780246913578024691357802469135780246913578024691357802469135780246913578024691357802469141651513734262516435190263143967454631918743000751861146858652219747883919392209327966909307740297653290433886520376204000415840169342671082000882825735618025902245247352219
Using double, Sum is Infinity

从上面的输出中可以明显看出,对于较大的整数使用double并不是一个好选择。
参考: https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/math/BigInteger.html#add(java.math.BigInteger)



相关用法


注:本文由纯净天空筛选整理自Rajnis09大神的英文原创作品 BigInteger add() Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。