Java - StrictMath scalb(float a, int b) 方法



描述

Java StrictMath scalb(float f,int scaleFactor) 方法返回 f x 2scaleFactor,其舍入方式如同透過一次正確舍入的浮點數乘法運算到 double 值集的成員。有關浮點數值集的討論,請參閱 Java 語言規範。如果結果的指數介於 Float.MIN_EXPONENT 和 Float.MAX_EXPONENT 之間,則答案將精確計算。如果結果的指數大於 Float.MAX_EXPONENT,則返回無窮大。請注意,如果結果是次正規數,則可能會丟失精度;也就是說,當 scalb(x, n) 是次正規數時,scalb(scalb(x, n), -n) 可能不等於 x。當結果是非 NaN 時,結果與 f 具有相同的符號。

  • 如果第一個引數是 NaN,則返回 NaN。

  • 如果第一個引數是無窮大,則返回相同符號的無窮大。

  • 如果第一個引數是零,則返回相同符號的零。

宣告

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

public static float scalb(float f, int scaleFactor)

引數

  • f − 要按 2 的冪縮放的數字。

  • scaleFactor − 用於縮放 d 的 2 的冪

返回值

此方法返回 f x 2scaleFactor

異常

示例:獲取正浮點數的 scalb

以下示例顯示了對正值使用 StrictMath scalb() 方法。

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

      // get a float number
      float x = 2.0f;
      int y = 5;
   
      // print x raised by y and then y raised by x
      System.out.println("StrictMath.scalb(" + x + "," + y + ")=" + StrictMath.scalb(x, y));
   }
}

輸出

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

StrictMath.scalb(2.0,5)=64.0

示例:獲取負浮點數的 scalb

以下示例顯示了對負值使用 StrictMath scalb() 方法。

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

      // get a float number
      float x = -2.0f;
      int y = 5;
   
      // print x raised by y and then y raised by x
      System.out.println("StrictMath.scalb(" + x + "," + y + ")=" + StrictMath.scalb(x, y));
   }
}

輸出

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

StrictMath.scalb(-2.0,5)=-64.0

示例:獲取負浮點數的 scalb

以下示例顯示了對負縮放因子使用 StrictMath scalb() 方法。

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

      // get a float number
      float x = 2.0f;
      int y = -5;
   
      // print x raised by y and then y raised by x
      System.out.println("StrictMath.scalb(" + x + "," + y + ")=" + StrictMath.scalb(x, y));
   }
}

輸出

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

StrictMath.scalb(2.0,-5)=0.0625
java_lang_strictmath.htm
廣告