CodeGym/Java 博客/随机的/Java 数学 abs() 方法
John Squirrels
第 41 级
San Francisco

Java 数学 abs() 方法

已在 随机的 群组中发布
个会员

数学中的绝对值函数是什么?

在数学中,一个数的绝对值等于传递的数的正值。绝对值函数忽略符号并返回没有它的值。例如,+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()方法。您可以在不同的数值数据类型上使用它。您可以遇到此方法的多个日常应用程序。一如既往,我们鼓励您在实践中学习。到那时,继续学习并不断成长!
评论
  • 受欢迎
你必须先登录才能发表评论
此页面还没有任何评论