當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Float intBitsToFloat()用法及代碼示例

Float類intBitsToFloat()方法

  • intBitsToFloat() 方法可在java.lang包。
  • intBitsToFloat() 方法遵循 IEEE 754 浮點標準,根據標準,它返回與表示整數位表示的給定參數相對應的浮點值。
  • intBitsToFloat() 方法是一個靜態方法,它也可以通過類名訪問,如果我們嘗試使用類對象訪問該方法,那麽我們也不會收到錯誤。
  • intBitsToFloat() 方法在將位表示轉換為浮點值時不會拋出異常。

用法:

    public static float intBitsToFloat(int bits_rep);

參數:

  • int bits_rep– 表示以位為單位的整數值。

返回值:

這個方法的返回類型是float,它返回以整數位表示給定參數的浮點值。

  • 如果我們通過 "0x7f800000",它會返回值 "positive infinity"。
  • 如果我們通過 "0xff800000",它會返回值 "negative infinity"。
  • 如果值在 "0x7f800001" 和 "0x7fffffff" 之間或值在 "0xff800001" 和 "0xffffffff" 之間。

例:

// Java program to demonstrate the example 
// of intBitsToFloat (int bits_rep)
// method of Float class

public class IntBitsToFloatOfFloatClass {
    public static void main(String[] args) {
        // Variables initialization
        int value1 = 20;
        int value2 = 0x7f800000;
        int value3 = 0xff800000;

        // Display value1,value2,value3 values
        System.out.println("value1:" + value1);
        System.out.println("value2:" + value2);
        System.out.println("value3:" + value3);


        // It returns the float value denoted by the given 
        // bit representation by calling Float.intBitsToFloat(value1)
        float result1 = Float.intBitsToFloat(value1);

        // It returns the float value denoted by the given 
        // bit representation by calling Float.intBitsToFloat(value2)
        float result2 = Float.intBitsToFloat(value2);

        // It returns the float value denoted by the given 
        // bit representation by calling Float.intBitsToFloat(value3)
        float result3 = Float.intBitsToFloat(value3);

        // Display result1,result2, result3 values
        System.out.println("Float.intBitsToFloat(value1):" + result1);
        System.out.println("Float.intBitsToFloat(value2):" + result2);
        System.out.println("Float.intBitsToFloat(value3):" + result3);
    }
}

輸出

value1:20
value2:2139095040
value3:-8388608
Float.intBitsToFloat(value1):2.8E-44
Float.intBitsToFloat(value2):Infinity
Float.intBitsToFloat(value3):-Infinity


相關用法


注:本文由純淨天空篩選整理自Preeti Jain大神的英文原創作品 Java Float class intBitsToFloat() method with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。