当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。