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


Java Double doubleToLongBits()用法及代碼示例


Java Double類的java.lang.Double.doubleToLongBits()方法是Java中的內置函數,該函數根據IEEE 754浮點“double format”位布局返回指定浮點值的表示形式。

用法:

public static long doubleToLongBits(double val)

參數:該方法僅接受一個參數val,該參數指定雙精度浮點數。


返回值:該函數返回代表浮點數的位。以下是特殊情況:

  • 如果參數為正無窮大,則結果為7ff0000000000000L。
  • 如果參數為負無窮大,則結果為0xfff0000000000000L。
  • 如果參數為NaN,則結果為0x7ff8000000000000L。

以下示例程序旨在說明java.lang.Double.doubleToLongBits()方法的使用:

示例1:

// Java program to demonstrate 
// Double.doubleToLongBits() method 
  
import java.lang.*; 
  
class Gfg1 { 
  
    public static void main(String args[]) 
    { 
  
        double val = 1.5d; 
  
        // function call 
        long answer = Double.doubleToLongBits(val); 
  
        // print 
        System.out.println(val + " in long bits: "
                           + answer); 
    } 
}
輸出:
1.5 in long bits: 4609434218613702656

示例2:

// Java program to demonstrate 
// Double.doubleToLongBits() method 
  
import java.lang.*; 
  
class Gfg1 { 
  
    public static void main(String args[]) 
    { 
  
        double val = Double.POSITIVE_INFINITY; 
        double val1 = Double.NEGATIVE_INFINITY; 
        double val2 = Double.NaN; 
  
        // function call 
        long answer = Double.doubleToLongBits(val); 
  
        // print 
        System.out.println(val + " in long bits: "
                           + answer); 
  
        // function call 
        answer = Double.doubleToLongBits(val1); 
  
        // print 
        System.out.println(val1 + " in long bits: "
                           + answer); 
  
        // function call 
        answer = Double.doubleToLongBits(val2); 
  
        // print 
        System.out.println(val2 + " in long bits: "
                           + answer); 
    } 
}
輸出:
Infinity in long bits: 9218868437227405312
-Infinity in long bits: -4503599627370496
NaN in long bits: 9221120237041090560

參考: https://docs.oracle.com/javase/7/docs/api/java/lang/Double.html#doubleToLongBits(double)



相關用法


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