Java - Float intBitsToFloat() 方法



描述

Java Float intBitsToFloat() 方法返回與給定位表示對應的浮點值。該引數被認為是根據 IEEE 754 浮點“單精度格式”位佈局表示的浮點值。它包含以下重要點:

  • 如果引數是 0x7f800000,則結果是正無窮大。
  • 如果引數是 0xff800000,則結果是負無窮大。
  • 如果引數是 0x7f800001 到 0x7fffffff 範圍內的任何值,或者 0xff800001 到 0xffffffff 範圍內的任何值,則結果是 NaN。

宣告

以下是 java.lang.Float.intBitsToFloat() 方法的宣告

public static float intBitsToFloat(int bits)

引數

bits − 這是一個整數。

返回值

此方法返回具有相同位模式的浮點值。

異常

從給定整數獲取浮點值示例

以下示例演示瞭如何使用 Float intBitsToFloat() 方法從給定的位表示中獲取浮點值。我們根據給定的位表示列印了三個浮點值。

package com.tutorialspoint;
public class FloatDemo {
   public static void main(String[] args) {
  
      /* returns the float value corresponding to a given bit representation */
      System.out.println(Float.intBitsToFloat(6757689));
      System.out.println(Float.intBitsToFloat(0x7f800000));  
      System.out.println(Float.intBitsToFloat(0xff800000));  
   }
} 

輸出

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

9.469539E-39
Infinity
-Infinity

從給定的正整數獲取浮點值示例

以下示例演示了另一個使用 Float intBitsToFloat() 方法從給定的位表示中獲取浮點值的方法。我們根據給定的位表示列印了一個浮點值。

package com.tutorialspoint;
public class FloatDemo {
   public static void main(String[] args) {
  
      /* returns the float value corresponding to a given bit representation */
      System.out.println(Float.intBitsToFloat(1098173645)); 
   }
} 

輸出

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

15.3

從給定的負整數獲取浮點值示例

以下示例演示瞭如何使用 Float intBitsToFloat() 方法從給定的位表示中獲取浮點值。我們根據給定的位表示列印了一個浮點值。

package com.tutorialspoint;
public class FloatDemo {
   public static void main(String[] args) {
  
      /* returns the float value corresponding to a given bit representation */
      System.out.println(Float.intBitsToFloat(-1049310003)); 
   }
} 

輸出

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

-15.3
java_lang_double.htm
廣告