Python epsilon 不是最小的数字 [英] Python epsilon is not the smallest number

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

问题描述

sys.float_info.epsilon 返回什么?

在我的系统上我得到:

<预><代码>>>>sys.float_info.epsilon2.220446049250313e-16>>>sys.float_info.epsilon/21.1102230246251565e-16>>>0<sys.float_info.epsilon/2

这怎么可能?

你没问题,我认为 epsilon 能做 min 能做的事.所以我的意思是 sys.float_info.min.

EDIT2

大家,尤其是约翰·库格尔曼,感谢您的回答!

为了向自己澄清事情,我做了一些玩弄:

<预><代码>>>>float.hex(sys.float_info.epsilon)'0x1.0000000000000p-52'>>>float.hex(sys.float_info.min)'0x1.0000000000000p-1022'>>>float.hex(1 + a)'0x1.0000000000001p+0'>>>float.fromhex('0x0.0000000000001p+0') == sys.float_info.epsilon真的>>>float.hex(sys.float_info.epsilon * sys.float_info.min)'0x0.0000000000001p-1022'

所以 epsilon * min 给出具有最小正有效数(或尾数)和最小指数的数.

解决方案

epsilon1 和下一个可表示的浮点数的区别.这与最小浮点数不同,后者是最接近 0 的数字,而不是 1.

有两个最小的浮点数,具体取决于您的标准.min 是最小的标准化浮点数.最小的次正常浮点数是min * epsilon.

<预><代码>>>>sys.float_info.min2.2250738585072014e-308>>>sys.float_info.min * sys.float_info.epsilon5e-324

注意规范化和次规范浮点数​​之间的区别:min 实际上并不是最小的浮点数,它只是具有全精度的最小浮点数.次正规数涵盖了 0min 之间的范围,但它们失去了很多精度.请注意,5e-324 只有一位有效数字.Subnormals 的处理速度也慢得多,最多比标准化浮点数慢 100 倍.

<预><代码>>>>(sys.float_info.min * sys.float_info.epsilon)/20.0>>>4e-3245e-324>>>5e-3250.0

这些测试证实 5e-324 确实是最小的浮点数.除以两次下溢为 0.

另见:浮点数在 Python 中的取值范围是多少?

What does sys.float_info.epsilon return?

On my system I get:

>>> sys.float_info.epsilon
2.220446049250313e-16
>>> sys.float_info.epsilon / 2
1.1102230246251565e-16
>>> 0 < sys.float_info.epsilon / 2 < sys.float_info.epsilon
True

How is this possible?

EDIT:

You are all right, I thought epsilon does what min does. So I actually meant sys.float_info.min.

EDIT2

Everybody and especially John Kugelman, thanks for your answers!

Some playing around I did to clarify things to myself:

>>> float.hex(sys.float_info.epsilon)
'0x1.0000000000000p-52'
>>> float.hex(sys.float_info.min)
'0x1.0000000000000p-1022'
>>> float.hex(1 + a)
'0x1.0000000000001p+0'
>>> float.fromhex('0x0.0000000000001p+0') == sys.float_info.epsilon
True
>>> float.hex(sys.float_info.epsilon * sys.float_info.min)
'0x0.0000000000001p-1022'

So epsilon * min gives the number with the smallest positive significand (or mantissa) and the smallest exponent.

解决方案

epsilon is the difference between 1 and the next representable float. That's not the same as the smallest float, which would be the closest number to 0, not 1.

There are two smallest floats, depending on your criteria. min is the smallest normalized float. The smallest subnormal float is min * epsilon.

>>> sys.float_info.min
2.2250738585072014e-308
>>> sys.float_info.min * sys.float_info.epsilon
5e-324

Note the distinction between normalized and subnormal floats: min is not actually the smallest float, it's just the smallest one with full precision. Subnormal numbers cover the range between 0 and min, but they lose a lot of precision. Notice that 5e-324 has only one significant digit. Subnormals are also much slower to work with, up to 100x slower than normalized floats.

>>> (sys.float_info.min * sys.float_info.epsilon) / 2
0.0
>>> 4e-324
5e-324
>>> 5e-325
0.0

These tests confirm that 5e-324 truly is the smallest float. Dividing by two underflows to 0.

See also: What is the range of values a float can have in Python?

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

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