📜  Java中的双 doubleToLongBits() 方法及示例

📅  最后修改于: 2022-05-13 01:55:44.358000             🧑  作者: Mango

Java中的双 doubleToLongBits() 方法及示例

Java Double 类的Java .lang.Double.doubleToLongBits()方法是Java中的一个内置函数,它根据 IEEE 754 浮点“双格式”位布局返回指定浮点值的表示。

句法:

public static long doubleToLongBits(double val)

参数:该方法只接受一个参数val ,它指定一个双精度浮点数。

返回值:该函数返回表示浮点数的位。以下是特殊情况:

  • 如果参数为正无穷大,则结果为7ff0000000000000L
  • 如果参数为负无穷大,则结果为0xfff0000000000000L
  • 如果参数为NaN ,则结果为0x7ff8000000000000L

下面的程序说明了Java.lang.Double.doubleToLongBits()方法的使用:

方案一:

// 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

方案二:

// 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: Java/lang/Double.html#doubleToLongBits(double)