绝对值是指与参数中传递的数字相对应的正值。极客,你一定想知道它到底是什么意思,所以不管传递给计算的是正数还是负数,在这两种情况下,计算都会发生在对应的正数上。因此,为了计算任何数字的绝对值,我们在Java中有一个指定的方法,称为 abs() 在里面 数学课 呈现在java内部。朗包。
null
这个 JAVA朗,数学。abs() 返回给定参数的绝对值。
- 如果参数不是负的,则返回参数。
- 如果参数是否定的,则返回参数的否定。
语法:
public static DataType abs(DataType a)
参数: Int、long、float或double值,其绝对值有待确定
返回类型: 此方法返回参数的绝对值。
引发异常: 算术异常
提示: 必须了解以下通用返回类型:
- 如果参数为双精度或浮点型:
- 如果参数为正零或负零,则结果为正零。
- 如果参数为无穷大,则结果为正无穷大。
- 如果参数为NaN,则结果为NaN。
- 如果参数为int或long类型: 如果参数等于整数的值。最小值或长值。MIN_值,最负的int或long值,结果是相同的值,为负。
例1:
JAVA
// Java Program to Illustrate Absolute Method // of Math Class // Importing all Math classes // from java.lang package import java.lang.Math; // Main class class GFG { // Main driver method public static void main(String[] args) { // Custom integer input received from user int n = - 7 ; // Printing value before applying absolute function System.out.println( "Without applying Math.abs() method : " + n); // Applying absolute math function and // storing it in integer variable int value = Math.abs(n); // Printing value after applying absolute function System.out.println( "With applying Math.abs() method : " + value); } } |
输出
Without applying Math.abs() method : -7With applying Math.abs() method : 7
例2:
JAVA
// Java Program to Demonstrate Working of abs() method // of Math class inside java.lang package // Importing Math class // from java.lang package import java.lang.Math; // Main class class GFG { // Main driver method public static void main(String args[]) { // Customly declaring and initializing all // arguments that ans() function takes // Float float a = 123 .0f; float b = - 34 .2323f; // Double double c = - 0.0 ; double d = - 999.3456 ; // Integer int e = - 123 ; int f = - 0 ; // Long long g = - 12345678 ; long h = 98765433 ; // abs() method taking float type as input System.out.println(Math.abs(a)); System.out.println(Math.abs(b)); // abs() method taking double type as input System.out.println(Math.abs( 1.0 / 0 )); System.out.println(Math.abs(c)); System.out.println(Math.abs(d)); // abs() method taking int type as input System.out.println(Math.abs(e)); System.out.println(Math.abs(f)); System.out.println(Math.abs(Integer.MIN_VALUE)); // abs() method taking long type as input System.out.println(Math.abs(g)); System.out.println(Math.abs(h)); System.out.println(Math.abs(Long.MIN_VALUE)); } } |
输出
123.034.2323Infinity0.0999.34561230-21474836481234567898765433-9223372036854775808
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END