Java signum() 方法带示例
java.lang.Math.signum(float f) 返回参数的 signum 函数;如果参数为零,返回零;如果参数大于零,返回 1.0f;如果参数小于零,返回 -1.0f。
现在让我们看一个示例 −
示例
import java.security.*; import java.util.*; public class Main { public static void main(String[] argv) { // get two float numbers float x = 50.14f; float y = -4f; // call signum for both floats and print the result System.out.println("Math.signum(" + x + ")=" + Math.signum(x)); System.out.println("Math.signum(" + y + ")=" + Math.signum(y)); } }
输出
Math.signum(50.14)=1.0 Math.signum(-4.0)=-1.0
现在让我们看另一个示例 −
示例
import java.security.*; import java.util.*; public class Main { public static void main(String[] argv) { double a = 5; double nanVal = Double.NaN; System.out.println(Math.signum(a)); a = -10; System.out.println(Math.signum(a)); a = 0; System.out.println(Math.signum(a)); System.out.println(Math.signum(nanVal)); } }
输出
1.0 -1.0 0.0 NaN
广告