Java - Math signum(double x) 方法



描述

Java Math signum(double d) 方法返回引數的符號函式;如果引數為零,則返回零;如果引數大於零,則返回 1.0;如果引數小於零,則返回 -1.0。特殊情況:

  • 如果引數是 NaN,則結果是 NaN。

  • 如果引數是正零或負零,則結果與引數相同。

宣告

以下是 java.lang.Math.signum() 方法的宣告

public static double signum(double d)

引數

d − 需要返回符號的浮點值

返回值

此方法返回引數的符號函式

異常

示例:獲取正雙精度值的符號

以下示例演示瞭如何使用 Math signum() 方法獲取正雙精度值的符號。

package com.tutorialspoint;
public class MathDemo {
   public static void main(String[] args) {

      // get a double number
      double x = 1654.9874;

      // find the signum of the value
      System.out.println("Math.signum(" + x + ")=" + Math.signum(x));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Math.signum(1654.9874)=1.0

示例:獲取負雙精度值的符號

以下示例演示瞭如何使用 Math signum() 方法獲取負雙精度值的符號。

package com.tutorialspoint;
public class MathDemo {
   public static void main(String[] args) {

      // get a double number
      double x = -9765.134;

      // find the signum of the value
      System.out.println("Math.signum(" + x + ")=" + Math.signum(x));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Math.signum(-9765.134)=-1.0

示例:獲取零雙精度值的符號

以下示例演示瞭如何使用 Math signum() 方法獲取零雙精度值的符號。

package com.tutorialspoint;
public class MathDemo {
   public static void main(String[] args) {

      // get double numbers
      double x = 0.0;	  

      // find the signum of the value
      System.out.println("Math.signum(" + x + ")=" + Math.signum(x));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Math.signum(0.0)=0.0
java_lang_math.htm
廣告