java math.signum,Java signum()用法及代码示例

java.lang.Math.signum()返回作为参数传递给它的值的Sign函数。 signum()函数根据传递给它的参数返回以下值:

如果传递的参数大于零,则signum()函数将返回1.0。

如果传递的参数等于零,则signum()函数将返回0。

如果传递的参数小于零,则signum()函数将返回-1.0。

注意:如果传递的参数为NaN,则结果为NaN。如果传递的参数为正零或负零,则结果将与参数相同。

用法:

public static double signum(double d)

Parameter:

a:the value whose signum function is to be returned.

Return:

This method returns the signum function value of

the argument passed to it.

例子1:显示java.lang.Math.signum()方法的用法。

// Java program to demonstrate working

// of java.lang.Math.signum() method

import java.lang.Math;

class Gfg {

// driver code

public static void main(String args[])

{

// when the argument is greater than zero

double a = 30;

System.out.println(Math.signum(a));

// when the argument is equals to zero

a = 0;

System.out.println(Math.signum(a));

// when the argument is less than zero

a = -30;

System.out.println(Math.signum(a));

}

}

输出:

1.0

0

-1.0

例子2:当参数为NaN时,显示java.lang.Math.signum()方法的用法。

// Java program to demonstrate working

// of java.lang.Math.signum() method

import java.lang.Math; // importing java.lang package

public class GFG {

public static void main(String[] args)

{

double nan = Double.NaN;

double result;

// Here argument is NaN, output will be NaN

result = Math.signum(nan);

System.out.println(result);

// Here argument is positive zero

result = Math.signum(0);

System.out.println(result);

// Here argument is negative zero

result = Math.signum(-0);

System.out.println(result);

}

}

输出:

NaN

0.0

0.0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值