- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
使用以下(几乎是最小的)示例:
import numpy as np
for x in np.arange(0,2,0.1):
print(x)
我们得到:
0.0
0.1
0.2
0.30000000000000004
0.4
0.5
0.6000000000000001
0.7000000000000001
0.8
0.9
1.0
1.1
1.2000000000000002
1.3
1.4000000000000001
1.5
1.6
1.7000000000000002
1.8
1.9000000000000001
作为输出。
我知道 X.X000001 输出应该归咎于“ float 精度问题”,但我不明白为什么它有时会起作用。显然 0.3 不能用 float 以 2 为底数精确表示,而且我没有看到数字中没有仅用一位小数显示的任何模式。
Python 怎么知道 0.1 足以显示数字?什么样的魔法告诉它截断剩余的数字?为什么它只是有时有效?
最佳答案
您正在打印 numpy.float64
对象,而不是 Python 内置的 float
类型,它使用 David Gay's dtoa algorithm .
截至version 1.14 , numpy 使用 dragon4 algorithm to print floating point values , 调整为接近与用于 Python float
类型的 David Gay 算法相同的输出:
Numpy scalars use the dragon4 algorithm in "unique" mode (see below) for str/repr, in a way that tries to match python float output.
numpy.format_float_positional()
function对此进行更详细的记录:
unique
: boolean, optionalIf
True
, use a digit-generation strategy which gives the shortest representation which uniquely identifies the floating-point number from other values of the same type, by judicious rounding. If precision was omitted, print out all necessary digits, otherwise digit generation is cut off after precision digits and the remaining value is rounded.
因此 0.2
可以通过仅打印 0.2
来唯一地呈现,但是系列中的下一个值 (0.30000000000000004
) 不能,你必须包含额外的数字以唯一地表示确切的值。
这如何实际上非常复杂;您可以在 Bungie 的 Destiny 游戏工程师 Ryan Juckett 的 Printing Floating-Point Numbers series 中阅读关于此的完整报告。 .
但基本上,输出字符串的代码需要确定所有围绕可能的 float 聚类的十进制数字的最短表示形式,这些数字不能被解释为下一个或之前的可能 float :
本图来自The Shortest Decimal String That Round-Trips: Examples通过 Rick Regan ,其中也涵盖了其他一些情况。 蓝色 中的数字是可能的 float64
值,绿色 中的数字是十进制数的可能表示形式。请注意灰色的中点标记,任何适合围绕浮点值的两个中点之间的表示都是公平游戏,因为所有这些表示都会产生相同的值。
David Gay 和 Dragon4 算法的目标都是找到能够再次产生完全相同的浮点值的最短十进制字符串输出。来自Python 3.1 What's New section on the David Gay approach :
Python now uses David Gay’s algorithm for finding the shortest floating point representation that doesn’t change its value. This should help mitigate some of the confusion surrounding binary floating point numbers.
The significance is easily seen with a number like
1.1
which does not have an exact equivalent in binary floating point. Since there is no exact equivalent, an expression likefloat('1.1')
evaluates to the nearest representable value which is0x1.199999999999ap+0
in hex or1.100000000000000088817841970012523233890533447265625
in decimal. That nearest value was and still is used in subsequent floating point calculations.What is new is how the number gets displayed. Formerly, Python used a simple approach. The value of
repr(1.1)
was computed asformat(1.1, '.17g')
which evaluated to'1.1000000000000001'
. The advantage of using 17 digits was that it relied on IEEE-754 guarantees to assure thateval(repr(1.1))
would round-trip exactly to its original value. The disadvantage is that many people found the output to be confusing (mistaking intrinsic limitations of binary floating point representation as being a problem with Python itself).The new algorithm for
repr(1.1)
is smarter and returns'1.1'
. Effectively, it searches all equivalent string representations (ones that get stored with the same underlying float value) and returns the shortest representation.The new algorithm tends to emit cleaner representations when possible, but it does not change the underlying values. So, it is still the case that
1.1 + 2.2 != 3.3
even though the representations may suggest otherwise.
关于python - float 打印不一致。为什么它有时会起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55727214/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!