CodeGym /Java Blog /Toto sisi /Java 數學 abs() 方法
John Squirrels
等級 41
San Francisco

Java 數學 abs() 方法

在 Toto sisi 群組發布

數學中的絕對值函數是什麼?

在數學中,一個數的絕對值等於傳遞的數的正值。絕對值函數忽略符號並返回沒有符號的值。例如,+5 的絕對值是 5。而 -5 的絕對值也是 5。 Java 數學 abs() 方法 - 1

Java 中的 Math.abs() method() 是什麼?

java.lang.Math類提供了一個靜態方法Math.abs(parameter)來求參數的絕對值”。
因此,如果您傳遞任何正數,假設Math.abs(5)它將返回 5。對於負數 5,Math.abs(-5)結果將相同,即;5.

方法頭


public static dataType abs(dataType parameter)

允許的數據類型

Java 的abs ()方法針對各種數據類型進行了重載。允許的類型如下。
int float double long

示例 1


public class DriverClass {
    public static void main(String args[]) {
   
        int number = +5;
        // Print the original number
        System.out.println("Original Number = " + number);
 
        // Printing the absolute value
        // Calling the Math.abs() method
        System.out.println("Absolute Number = " + "Math.abs( " + number + " ) = " + Math.abs(number));
        
        
        number = -5;
        // Print the original number
        System.out.println("Original Number = " + number);
 
        // Printing the absolute value
        // Calling the Math.abs() method
        System.out.println("Absolute Number = " + "Math.abs( " + number + " ) = " + Math.abs(number));
        
    }
}

輸出

原始數 = 5 絕對數 = Math.abs( 5 ) = 5 原始數 = -5 絕對數 = Math.abs( -5 ) = 5

解釋

在上面的代碼片段中,我們取了兩個數字。第一個數字是正整數,即+5。第二個數字是負整數,即-5。我們將這兩個數字都傳遞給Math.abs(number)方法。該方法為忽略各自符號的兩個輸入返回 5。

示例 2


public class DriverClass {
    public static void main(String args[]) {
   
        int number = -0;
        System.out.println("Original Number = " + number);
        System.out.println("Math.abs( " + number + " ) = " + Math.abs(number) + "\n");
        
        long number1 = -4499990;
        System.out.println("Original Number = " + number1);
        System.out.println("Math.abs( " + number1 + " ) = " + Math.abs(number1) + "\n");
        
        float number2 = -92.45f;
        System.out.println("Original Number = " + number2);
        System.out.println("Math.abs( " + number2 + " ) = " + Math.abs(number2) + "\n");
        
        double number3 = -63.7777777777;
        System.out.println("Original Number = " + number3);
        System.out.println("Math.abs( " + number3 + " ) = " + Math.abs(number3) + "\n");
    }
}

輸出

原始數字 = 0 Math.abs( 0 ) = 0 原始數字 = -4499990 Math.abs( -4499990 ) = 4499990 原始數字 = -92.45 Math.abs( -92.45 ) = 92.45 原始數字 = -63.7777777777 Math.abs( - 63.7777777777 ) = 63.7777777777

解釋

在上面的代碼中,除了整數之外,我們還採用了 double、long 和 float 值作為 Math.abs ()方法的輸入。我們已經將所有各自的值一一 傳遞給Math.abs()方法,並將結果顯示在控制台上。

邊界情況

以下是使用Math.abs()方法時需要注意的一些例外情況。

對於 int 和 long 數據類型

如果參數為正零或負零,則結果為正零。
數學.abs(+0) = 0 數學.abs(-0) = 0
對於Integer.MIN_VALUELong.MIN_VALUE,Math.abs()的輸出仍然是最小的整數或長整數,即負數。
Math.abs(Integer.MIN_VALUE) = -2147483648 Math.abs(Long.MIN_VALUE) = -9223372036854775808

對於 float 和 double 數據類型

如果參數是無窮大,則結果是正無窮大。
Math.abs(Double.NEGATIVE_INFINITY) = 無窮大
如果參數為 NaN,則結果為 NaN。
Math.abs(Double.NaN) = NaN

結論

到本文結束時,您必須熟悉 Java Math.abs()方法。您可以在不同的數值數據類型上使用它。您可以遇到此方法的多個日常應用程序。一如既往,我們鼓勵您在實踐中學習。到那時,繼續學習並不斷成長!
留言
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION