Java Math subtractExact(long x, long y) 方法


我們將討論 Java 語言中的 Java Math subtractExact(long x, long y) 方法,並瞭解其功能和工作原理。

subtractExact()是 Java Math 庫中的一個內建函式。該函式返回作為引數傳遞給函式的兩個引數之間的差值。當返回值超出特定資料型別的取值範圍時,該函式將返回一個異常。

subtractExact() 函式的語法

long a;
long b;

long subtractExact(long a, long b);

傳遞給函式的引數是 a 和 b,它們都是 long 資料型別。該函式返回 long 型別的值,該值等於傳遞給函式的引數的差值,即 a-b。當函式返回的值超過傳遞引數的資料型別的取值範圍時,該函式將返回一個異常,指示特定資料型別發生溢位。

在這篇文章中,我們將看到 subtractExact() 函式在 Java 程式碼中的實現以及它如何返回值。我們將編寫一段 Java 程式碼,其中兩個數字將作為輸入給出,輸入將為 long 資料型別,我們需要使用 Java Math 庫中提供的 subtractExact() 函式來找到這兩個數字的差值。

讓我們透過以下示例瞭解問題。

輸入

a=30 , b=10

輸出

20

說明 - 此處給出的輸入數字為 30 和 10。我們需要使用 subtractExact() 函式找到這兩個數字的差值,即 a-b。因此,輸出將為 20,等於 a-b。

輸入

a= -2  , b=-3

輸出

1

說明 - 輸入數字為 -2 和 -3。使用 subtractExact(a,b) 並將這兩個數字作為引數傳遞給函式,該函式將返回等於 a-b 的值,即 -2-(-3)=-2+3=1。因此,1 是所需的輸出。

讓我們瞭解在 Java 程式碼中 subtractExact() 函式的使用,以檢視該函式的實現。

讓我們瞭解在 Java 程式碼中 subtractExact() 函式的使用,以檢視該函式的實現

我們將遵循以下步驟在我們的 Java 程式碼中實現 subtractExact() 函式

  • 我們將使用 java.lang.Math 匯入 Java Math 庫。

  • 匯入 Math 庫後,我們可以使用庫中的所有函式,包括 subtractExact() 函式。

  • 初始化兩個 long 資料型別的變數。

  • 現在,我們將建立另一個變數來儲存函式返回的值。

  • 使用 subtractExact() 函式,我們將值儲存在變數中。該函式從第一個引數中減去傳遞給函式的第二個引數。

  • 列印函式的輸出,該輸出將等於第一個引數和第二個引數的差值。

示例

//Java Code to see the implementation of the subtractExact() function

import java.lang.Math; //to import the java Math library to use the subtractExact() function

public class Main
{
	public static void main(String[] args) {
		//initialise two variable of long data type
		long a;
		long b;
		
		//store the values in the variable
		a=56240389;
		b=846270;
		
		long ans; //to store the  output of the function
		//subtracts the second parameter from the first one i.e. a-b
		ans=Math.subtractExact(a,b); // passing a and b as parameter in the function
		
		//print the output
		System.out.println("The value returned by the function : "+ans);
		
		ans=Math.subtractExact(b,a); //will equal to b-a
		
		System.out.println("The value returned by the function : "+ans);

	}
}

輸出

The value returned by the function : 55394119
The value returned by the function : -55394119

時間複雜度 - O(1),因為 subtractExact() 函式在常數時間內返回輸出。

空間複雜度 - O(1),因為我們沒有使用任何額外的空間來實現該函式。

檢視 subtractExact() 函式中溢位的 Java 程式碼

實現該函式的步驟如下

  • 我們將使用 java.lang.Math 匯入 Java Math 庫

  • 現在建立兩個 long 資料型別的變數。

  • 我們將 long 資料型別的最小值儲存在一個變數中,並將任何值儲存在第二個變數中。

  • 現在初始化另一個 long 資料型別的變數來儲存函式返回的值。我們將 long 資料型別的最小值作為第一個引數傳遞,因為該函式從第一個引數中減去傳遞給函式的第二個引數。

  • 從 long 資料型別的最小值中減去數字將導致 long 資料型別發生溢位。因此,該函式將顯示 long 溢位的異常。

示例

//Java Code to see the implementation of the subtractExact() function

import java.lang.Math; //to import the java Math library to use the subtractExact() function

public class Main
{
	public static void main(String[] args) {
		//initialise two variable of long data type
		long a;
		long b;
		
		//store the values in the variable
		a=Long.MIN_VALUE;
		b=7;
		
		long ans; //to store the  output of the function
		//subtracts the second parameter from the first one i.e. a-b
		//which will cause overflow in long as a is the minimum value of long data type
		ans=Math.subtractExact(a,b); // passing a and b as parameter in the function
		
		//print the output
		System.out.println("The value returned by the function : "+ans);
		

	}
}

輸出

由於函式中發生溢位,程式碼將無法執行,從而顯示如下所示的 long 溢位異常

Exception in thread "main" java.lang.ArithmeticException: long overflow
        at java.base/java.lang.Math.subtractExact(Math.java:887)
        at Main.main(Main.java:19)

結論

本文討論了 Java Math subtractExact(long x, long y) 方法以及該函式在 Java 中的實現及其功能。我們透過編寫 Java 程式碼來了解 subtractExact() 函式的實現,並嘗試瞭解函式中值的溢位。

我希望您在閱讀本文後已經瞭解了 Java Math subtractExact() 方法及其在 Java 中的實現。

更新於:2023年8月28日

瀏覽量:276

啟動您的職業生涯

透過完成課程獲得認證

開始學習
廣告