Java - StrictMath signum(float x) 方法



描述

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

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

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

宣告

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

public static float signum(float f)

引數

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

返回值

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

異常

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

以下示例演示瞭如何使用 StrictMath signum() 方法為正浮點值獲取長整數值。

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

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

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

輸出

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

StrictMath.signum(1654.9874)=1.0

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

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

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

      // get a float number
      float x = -9765.134f;

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

輸出

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

StrictMath.signum(-9765.134)=-1.0

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

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

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

      // get float numbers
      float x = 0.0f;	  

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

輸出

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

StrictMath.signum(0.0)=0.0
java_lang_strictmath.htm
廣告