当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Java Java.lang.StrictMath.signum()用法及代码示例


描述

这个java.lang.StrictMath.signum(double d) 方法返回参数的符号函数,即如果参数为零,则为零,如果参数大于零,则为 1.0,如果参数小于零,则为 -1.0。它包括这些情况

  • 如果第一个参数是 NaN,则返回 NaN。 .
  • 如果参数为正零或负零,则结果与参数相同。

声明

以下是声明java.lang.StrictMath.signum()方法

public static double signum(double d)

参数

d─ 这是要返回其符号的浮点值。

返回值

此方法返回参数的符号函数。

异常

NA

示例

下面的例子展示了 java.lang.StrictMath.signum() 方法的用法。

package com.tutorialspoint;

import java.lang.*;

public class StrictMathDemo {

   public static void main(String[] args) {

      double d1 = 102.20d, d2 = 0.0d, d3 = -0.0d;
   
      // returns 1.0 if the argument is greater than zero
      double retval = StrictMath.signum(d1);
      System.out.println("Value = " + retval);

      /* if the argument is positive zero, then the result is the
         same as the argument */
      retval = StrictMath.signum(d2);
      System.out.println("Value = " + retval);
    
      /* if the argument is negative zero, then the result is the 
         same as the argument */
      retval = StrictMath.signum(d3);
      System.out.println("Value = " + retval);
   }
}

让我们编译并运行上面的程序,这将产生以下结果——

Value = 1.0
Value = 0.0
Value = -0.0

相关用法


注:本文由纯净天空筛选整理自 Java.lang.StrictMath.signum() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。