java 123456792_Why there is loss of value when converting from int to float in the below code?

问题

int value1 = 123456789;

float value2 = value1;

System.out.println(value1);

System.out.println(value2);

Output:

123456789

123456792

回答1:

The float type uses the same number of bits as int (32 bits) to represent floating point numbers in the larger range than int uses to represent only integers.

This causes a loss of precision, since not every int number can be represented accurately by a float. Only 24 bits are used to represent the fraction part of the number (including the sign bit), while the other 8 are used to represent the exponent.

If you assign this int value to a double, there won't be any loss of precision, since double has 64 bits, and more than 32 of them are used to represent the fraction.

Here's a more detailed explanation:

The binary representation of 123456789 as an int is :

00000111 01011011 11001101 0001 0101

A single precision floating point number is constructed from its 32 bits using the following formula :

(-1)^sign * 1.b22 b21 ... b0 * 2^(e-127)

Where sign is the left most bit (b31). b22 to b0 are the fraction bits, and bits b30 to b23 make the exponent e.

Therefore, when you convert the int 123456789 to float, you can only use the following 25 bits :

00000111 01011011 11001101 00010101

- --- -------- -------- -----

We can safely get rid of any leading zeroes (except of the sign bit) and any trailing zeroes. This leaves you with the 3 least significant bits, which we must drop. We can either subtract 5 to get 123456784:

00000111 01011011 11001101 00010000

- --- -------- -------- -----

or add 3 to get 123456792:

00000111 01011011 11001101 00011000

- --- -------- -------- -----

Obviously adding 3 gives a better approximation.

来源:https://stackoverflow.com/questions/31903174/why-there-is-loss-of-value-when-converting-from-int-to-float-in-the-below-code

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值