小数的 Python 算术 [英] Python arithmetic with small numbers

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

问题描述

当我在 Python 中使用小数进行算术运算时,我得到以下意想不到的结果:

<预><代码>>>>sys.float_infosys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_107,59t302012012012040402020201204040201402012040012040400010001007300738585072014,基数=2,轮数=1)>>>(1. - (1.e-17) ) <1.错误的

我知道浮点数没有无限精度,但它应该能够处理像 1e-17 这样的大"小数,不是吗?

解决方案

浮点数的精度在 0 附近比在 1 附近更高.

  • 在 0.0 和 0.1 之间有 4591870180066957722 个浮点数.
  • 在 0.9 和 1.0 之间有 900719925474099 个浮点数,目前数量较少.

浮动密度定期减半,看起来像这样:

这里是1.之后的下一个浮点数,在0.的方向:

<预><代码>>>>导入数学>>>math.nextafter(1., 0.)0.9999999999999999>>>format(math.nextafter(1., 0.), ".32f") # 让我们看看更多的小数位'0.99999999999999988897769753748435'

1 - 10-17 的数学正确值是 0.99999999999999999(有 17 个 9),我称这个数字为 n.与几乎所有数字一样,n 不能用浮点数精确表示.

0.999999999999999999 # n0.00000000000000001 # n 和 1 之间的距离,即 10^-170.00000000000000010102230246251565... # n 和 nextafter(1., 0.) 之间的距离

所以你看,1 - 10-17 距离 nextafter(1., 0.) 大约是它的 10 倍来自 1..当表达式 1.- 1.e-17 由解释器评估,它会返回最接近的可能结果,也就是 1..返回任何其他浮点数是没有意义的,这将与真实的"浮点数相去甚远.结果(请原谅双关语).

注意: math.nextafter 在 Python 3.9+ 中可用.在早期版本中,您可以类似地使用 numpy.nextafter.

相关问题->以尽可能小的方式增加 Python 浮点值金额

I am getting the following unexpected result when I do arithmetic with small numbers in Python:

>>> sys.float_info
sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)
>>> (1. - (1.e-17) ) < 1.
False

I know that floating point numbers do not have infinite precision, but it should be able to handle "large" small numbers like 1e-17, shouldn't it?

解决方案

The precision of floats is higher near 0 than it is near 1.

  • There are 4591870180066957722 floats between 0.0 and 0.1.
  • There are 900719925474099 floats between 0.9 and 1.0, fewer by far.

Float density halves at regular intervals, it looks something like this:

Here is the next float after 1., in the direction of 0.:

>>> import math
>>> math.nextafter(1., 0.)
0.9999999999999999
>>> format(math.nextafter(1., 0.), ".32f")  # let's see more decimal places
'0.99999999999999988897769753748435'

The mathematically correct value of 1 - 10-17 is 0.99999999999999999 (there are seventeen nines), I'll call this number n. Like almost all numbers, n can't be represented exactly with a float.

0.99999999999999999                    # n
0.00000000000000001                    # distance between n and 1, i.e. 10^-17
0.00000000000000010102230246251565...  # distance between n and nextafter(1., 0.)

So you see, 1 - 10-17 is about 10 times further from nextafter(1., 0.) than it is from 1.. When the expression 1. - 1.e-17 is evaluated by the interpreter it gives you back the closest possible result, which is 1. exactly. It wouldn't make sense to return any other float, that would be even further away from the "real" result (pardon the pun).

Note: math.nextafter is available in Python 3.9+. In earlier versions you can use numpy.nextafter similarly.

Related question -> Increment a Python floating point value by the smallest possible amount

这篇关于小数的 Python 算术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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