Java StrictMath signum()方法与示例
StrictMath类signum()方法
语法:
public static float signum(float fl);
public static double signum(double d);signum()方法在java.lang包中可用。
signum(floatfl)方法用于返回给定float参数类型方法的signum函数。这是提取实数符号的奇数数学函数。
signum(doubled)方法用于返回给定double参数类型的signum函数。这是提取实数符号的奇数数学函数。
这些方法不会引发异常。
这些是静态方法,可以使用类名进行访问,如果尝试使用类对象访问这些方法,则不会出现任何错误。
参数:
float/double-表示要查找其单数函数的值。
返回值:
该方法的返回类型为float/double,它返回给定参数的signum函数。
注意:
如果我们通过NaN,则该方法返回相同的值(即NaN)。
如果传递零,则该方法返回具有相同符号的相同值(oe0)。
如果传递的值小于0,则该方法返回-1.0。
如果传递的值大于0,则该方法返回1.0。
示例
//Java程序演示示例
//signum()StrictMath类的方法
public class Signum {
public static void main(String[] args) {
//变量声明
float f1 = -0.0f;
float f2 = 0.0f;
float f3 = -0.6f;
float f4 = 2.0f;
double d1 = -0.0;
double d2 = 0.0;
double d3 = -0.6;
double d4 = 2.0;
System.out.println("signum(float fl): ");
//在这里,我们得到(-0.0),因为我们通过
//参数值为(-0.0f)
System.out.println("StrictMath.signum(f1): " + StrictMath.signum(f1));
//在这里,我们将得到(0.0),我们正在传递
//值为(0.0f)
System.out.println("StrictMath.signum(f2): " + StrictMath.signum(f2));
//在这里,我们将得到(-1.0),我们正在通过
//参数值为(-0.6f)
System.out.println("StrictMath.signum( f3): " + StrictMath.signum(f3));
//在这里,我们将得到(1.0),我们正在传递
//值为(2.0f)
System.out.println("StrictMath.signum( f4): " + StrictMath.signum(f4));
System.out.println();
System.out.println("signum(double d): ");
//在这里,我们得到(-0.0),因为我们通过
//参数值为(-0.0)
System.out.println("StrictMath.signum(d1): " + StrictMath.signum(d1));
//在这里,我们将得到(0.0),我们正在传递
//值为(0.0)
System.out.println("StrictMath.signum(d2): " + StrictMath.signum(d2));
//在这里,我们将得到(-1.0),我们正在通过
//值为(-0.6)
System.out.println("StrictMath.signum(d3): " + StrictMath.signum(d3));
//在这里,我们将得到(1.0),我们正在传递
//参数的值为(2.0)
System.out.println("StrictMath.signum(d4): " + StrictMath.signum(d4));
}
}输出结果
signum(float fl): StrictMath.signum(f1): -0.0 StrictMath.signum(f2): 0.0 StrictMath.signum( f3): -1.0 StrictMath.signum( f4): 1.0 signum(double d): StrictMath.signum(d1): -0.0 StrictMath.signum(d2): 0.0 StrictMath.signum(d3): -1.0 StrictMath.signum(d4): 1.0