Java - Math signum(float x) 方法



描述

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

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

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

宣告

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

public static float signum(float f)

引數

f - 要返回其符號的浮點值

返回值

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

異常

示例:獲取正浮點值的符號

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

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

      // get a float number
      float x = 1654.9874f;

      // 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 float number
      float x = -9765.134f;

      // 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 float numbers
      float x = 0.0f;	  

      // 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
廣告