Python浮点数字表示 [英] Python float numbers representation
问题描述
可能重复:
浮点限制
您好,
也许这个答案很简单。我正在玩Python解释器。
>>> 1
1
>>> 1.1
1.1000000000000001
>>> 1.2
1.2
为什么 1.1
被解释为 1.1000000000000001
?
From 浮点指南:
为什么我的数字,比如0.1 + 0.2加起来好一轮0.3,而
反而得到一个奇怪的结果,比如
0.30000000000000004?
由于内部计算机使用
格式(二进制浮点),所以
不能准确地表示一个数字
,如0.1,0.2或0.3 。
在编译代码或解释
时,您的0.1已经被
四舍五入到
格式的最接近的数字,即使在
计算发生之前,这也会导致一个很小的
舍入误差。
Possible Duplicate:
Floating Point Limitations
Hi there,
maybe this has a very simple answer. I was playing around with the Python interpreter.
>>> 1
1
>>> 1.1
1.1000000000000001
>>> 1.2
1.2
Why 1.1
was interpreted as 1.1000000000000001
?
From The Floating-Point Guide:
Why don’t my numbers, like 0.1 + 0.2 add up to a nice round 0.3, and instead I get a weird result like 0.30000000000000004?
Because internally, computers use a format (binary floating-point) that cannot accurately represent a number like 0.1, 0.2 or 0.3 at all.
When the code is compiled or interpreted, your "0.1" is already rounded to the nearest number in that format, which results in a small rounding error even before the calculation happens.
这篇关于Python浮点数字表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!