Float类中的floatToRawIntBits()方法是Java中的内置函数,该函数根据IEEE 754浮点“single format”位布局返回指定浮点值的表示形式,并保留Not-a-Number(NaN)值。
用法:
public static int floatToRawIntBits(float val)
参数:该方法仅接受一个参数val,该参数指定浮点数。
返回值:该函数返回代表浮点数的位。但是,有3种特殊情况:
如果参数为正无穷大,则结果为0x7f800000。
如果参数为负无穷大,则结果为0xff800000。
如果参数为NaN,则结果为0x7fc00000。
以下示例程序旨在说明Float.floatToRawIntBits()方法的使用:
程序1:
// Java program to demonstrate
// Float.floatToRawIntBits() method
import java.lang.*;
class Gfg1 {
public static void main(String args[])
{
float val = 1.5f;
// function call
int answer = Float.floatToRawIntBits(val);
System.out.println(val + " in raw int bits:"
+ answer);
}
}
输出:
1.5 in raw int bits:1069547520
程序2:
// Java program to demonstrate
// Float.floatToRawIntBits() method
import java.lang.*;
class Gfg1 {
public static void main(String args[])
{
float val = Float.POSITIVE_INFINITY;
float val1 = Float.NEGATIVE_INFINITY;
float val2 = Float.NaN;
// function call
int answer = Float.floatToRawIntBits(val);
System.out.println(val + " in raw int bits:"
+ answer);
// function call
answer = Float.floatToRawIntBits(val1);
System.out.println(val1 + " in raw int bits:"
+ answer);
// function call
answer = Float.floatToRawIntBits(val);
System.out.println(val2 + " in raw int bits:"
+ answer);
}
}
输出:
Infinity in raw int bits:2139095040
-Infinity in raw int bits:-8388608
NaN in raw int bits:2139095040