java floattorawintbits,Java Float floatToRawIntBits()用法及代码示例

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);

// print

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);

// print

System.out.println(val + " in raw int bits:"

+ answer);

// function call

answer = Float.floatToRawIntBits(val1);

// print

System.out.println(val1 + " in raw int bits:"

+ answer);

// function call

answer = Float.floatToRawIntBits(val);

// print

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值