整数和浮点精度 [英] Integers and float precision

查看:423
本文介绍了整数和浮点精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这更多是数字分析而不是编程问题,但我想你们中的一些人将能够回答它.

This is more of a numerical analysis rather than programming question, but I suppose some of you will be able to answer it.

在两个浮点数的总和中,是否有任何精度损失?为什么?

In the sum two floats, is there any precision lost? Why?

在浮点数和整数的总和中,是否有任何精度损失?为什么?

In the sum of a float and a integer, is there any precision lost? Why?

谢谢.

推荐答案

两个浮点数之和是否有任何精度损失?

In the sum two floats, is there any precision lost?

如果两个浮点的大小都不同,并且都使用完整的精度范围(大约7个十进制数字),那么可以,您会在最后一位看到损失.

If both floats have differing magnitude and both are using the complete precision range (of about 7 decimal digits) then yes, you will see some loss in the last places.

为什么?

这是因为浮点数以(符号)(尾数)×2 (指数)的形式存储.如果两个值具有不同的指数并且将它们相加,则较小的值将在尾数中减少为更少的数字(因为它必须适应较大的指数):

This is because floats are stored in the form of (sign) (mantissa) × 2(exponent). If two values have differing exponents and you add them, then the smaller value will get reduced to less digits in the mantissa (because it has to adapt to the larger exponent):

PS> [float]([float]0.0000001 + [float]1)
1

在浮点数和整数的总和中,会丢失任何精度吗?

In the sum of a float and a integer, is there any precision lost?

是的,普通的32位整数能够准确表示不完全适合浮点数的值.浮点数仍然可以大约存储相同的数字,但不再完全相同.当然,这仅适用于足够大的数字,即. e.超过24位.

Yes, a normal 32-bit integer is capable of representing values exactly which do not fit exactly into a float. A float can still store approximately the same number, but no longer exactly. Of course, this only applies to numbers that are large enough, i. e. longer than 24 bits.

为什么?

因为浮点数的精度为24位,而(32位)整数的精度为32.浮点数仍将保留幅度和大多数有效数字,但最后一位可能会有所不同:

Because float has 24 bits of precision and (32-bit) integers have 32. float will still be able to retain the magnitude and most of the significant digits, but the last places may likely differ:

PS> [float]2100000050 + [float]100
2100000100

这篇关于整数和浮点精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆