java math abs
数学类静态浮点数abs(float f) (Math Class static float abs(float f))
This method is available in java.lang package.
此方法在java.lang包中可用。
This method is used to return the absolute value of the given parameter in the method.
此方法用于返回方法中给定参数的绝对值。
This is a static method so this method is accessible with classname too.
这是一个静态方法,因此也可以使用类名访问此方法。
The return type of this method depends on given argument datatype and here we are passing float data type in the method that means the return type of this method is float.
此方法的返回类型取决于给定的参数数据类型,这里我们在方法中传递float数据类型,这意味着该方法的返回类型为float。
In this method, we pass only one parameter as an argument in the method of Math class.
在此方法中,我们仅将一个参数作为参数传递给Math类的方法。
This method does not throw any exception.
此方法不会引发任何异常。
This is an overridable method so various versions of this method are available but here we are looking float datatype argument so the following syntax is given below,
这是一个可重写的方法,因此可以使用该方法的各种版本,但是在这里我们正在寻找float数据类型参数,因此下面给出了以下语法:
Syntax:
句法:
public static float abs(float f){
}
Parameter(s):
参数:
float f – a float value whose absolute value to be found.
float f –一个浮点值,其绝对值将被找到。
Return value:
返回值:
The return type of this method is float, returns absolute value as a float.
该方法的返回类型为float ,以浮点数形式返回绝对值。
Note:
注意:
If we pass a positive float, it returns the same value.
如果我们传递一个正浮点数,它将返回相同的值。
If we pass a negative float, it returns the value without sign i.e. it returns the positive float.
如果我们传递一个负浮点数,它将返回不带符号的值,即它将返回正浮点数。
If we pass positive zero (0) or negative zero (-0), it returns zero without sign (0).
如果我们传递正零(0)或负零(-0),则它将返回零而没有符号(0)。
If we pass an infinity parameter, it returns the same infinity value without the sign.
如果我们传递一个无穷大参数,则它将返回相同的无穷大值,但不带符号。
If we pass "NaN" (Not A Number), it returns the same i.e. a "NaN".
如果我们传递“ NaN”(非数字),则返回相同的值,即“ NaN”。
演示abs(float f)方法示例的Java程序 (Java program to demonstrate example of abs(float f) method)
// Java program to demonstrate the example of
// abs(float f) method of Math class
class AbsFloatTypeMethod {
public static void main(String[] args) {
// We are declaring few variables
float a = 123.121f;
float b = -123.121f;
float c = 0.0f;
float d = -0.0f;
float e = 7.70f / 0.0f;
float f = -7.70f / 0.0f;
// By using abs(float f) method,
// we will find the absolute value of given parameter
System.out.println("The absolute value of a is : " + Math.abs(a));
System.out.println("The absolute value of b is : " + Math.abs(b));
System.out.println("The absolute value of c is : " + Math.abs(c));
System.out.println("The absolute value of d is : " + Math.abs(d));
System.out.println("The absolute value of e is : " + Math.abs(e));
System.out.println("The absolute value of f is : " + Math.abs(f));
}
}
Output
输出量
E:\Programs>javac AbsFloatTypeMetho.java
E:\Programs>java AbsFloatTypeMethod
The absolute value of a is : 123.121
The absolute value of b is : 123.121
The absolute value of c is : 0.0
The absolute value of d is : 0.0
The absolute value of e is : Infinity
The absolute value of f is : Infinity
翻译自: https://www.includehelp.com/java/math-class-static-float-abs-float-f-method-with-example.aspx
java math abs