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

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

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

Java Double 类的doubleToRawLongBits()方法是Java中的内置函数,它根据 IEEE 754 浮点“双格式”位布局返回指定浮点值的表示,保留 Not-a-Number (NaN ) 值。

句法:

public static long doubleToRawLongBits(double val)

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

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

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

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

方案一:

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


Java
// Java program to demonstrate
// Double.doubleToRawLongBits() 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.doubleToRawLongBits(val);
 
        // print
        System.out.println(val + " in raw long bits: "
                           + answer);
 
        // function call
        answer = Double.doubleToRawLongBits(val1);
 
        // print
        System.out.println(val1 + " in raw long bits: "
                           + answer);
 
        // function call
        answer = Double.doubleToRawLongBits(val2);
 
        // print
        System.out.println(val2 + " in raw long bits: "
                           + answer);
    }
}


输出:
1.5 in raw long bits: 4609434218613702656



方案二:

Java

// Java program to demonstrate
// Double.doubleToRawLongBits() 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.doubleToRawLongBits(val);
 
        // print
        System.out.println(val + " in raw long bits: "
                           + answer);
 
        // function call
        answer = Double.doubleToRawLongBits(val1);
 
        // print
        System.out.println(val1 + " in raw long bits: "
                           + answer);
 
        // function call
        answer = Double.doubleToRawLongBits(val2);
 
        // print
        System.out.println(val2 + " in raw long bits: "
                           + answer);
    }
}
输出:
Infinity in raw long bits: 9218868437227405312
-Infinity in raw long bits: -4503599627370496
NaN in raw long bits: 9221120237041090560



参考: https: Java/lang/Double.html#doubleToRawLongBits(double)