當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java BigInteger subtract()用法及代碼示例


java.math.BigInteger.subtract(BigInteger val)用於計算兩個BigInteger的算術差。此方法適用於比Java最大數據類型double的範圍大得多的範圍的大數值,而不會影響結果的精度,但是由於BigInteger類內部使用整數數組進行處理,因此不對BigIntegers的對象進行運算像在圖元上一樣快。此方法對當前的BigInteger執行操作,調用該方法並將BigInteger作為參數傳遞。

用法:

public BigInteger subtract(BigInteger val)

參數:此方法接受參數val,該參數是要從此BigInteger中減去的值。


返回值:此方法返回一個保存差異(this – val)的BigInteger。

下麵的程序用於說明BigInteger的subtract()方法。

示例1:

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

示例2:

// Java program to demonstrate 
// subtract() method of BigInteger 
  
import java.math.BigInteger; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
        // BigInteger object to store result 
        BigInteger diff; 
  
        // For user input 
        // Use Scanner or BufferedReader 
  
        // Two objects of String created 
        // Holds the values to calculate the difference 
        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 subtract() method 
        diff = a.subtract(b); 
  
        // Display the result in BigInteger 
        System.out.println("The difference of\n"
                           + a + " \nand\n" + b + " "
                           + "\nis\n" + diff); 
  
        // Using double to hold  the result 
        double d = Double.parseDouble(diff.toString()); 
  
        // Display the result in double 
        System.out.println("Using double, difference is "
                           + d); 
    } 
}
輸出:

The difference of
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234554324324362432767463726478326478326784637264783264736274673864736473246354635463546325645326456325463546536453654632546354653463254653453264564635463263453264654632498739473
and
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234587327189371829748971463784817489127847281478217487124872184787483274637564756745781641263981283972189743897432865743657645762376914689217817436247362472164761247612748612746
is
-33002865009396981507737306491010801062644213434222388597510922746801391210121282235315618654827646726197360979211111111291108913660035764552871611899208711496592980249873273
Using double, difference is -3.300286500939698E172

從上麵的示例中可以明顯看出,使用BigInteger時數據是完全精確的。

參考: https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/math/BigInteger.html#subtract(java.math.BigInteger)



相關用法


注:本文由純淨天空篩選整理自Rajnis09大神的英文原創作品 BigInteger subtract() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。