Ev.*_*nis 9 python floating-point fractions
我知道浮点数学的本质,但我仍然发现以下令人惊讶的:
from fractions import Fraction
print(Fraction(0.2)) # -> 3602879701896397/18014398509481984
print(Fraction(str(0.2))) # -> 1/5
print(Fraction(0.2)==Fraction(str(0.2))) # returns False
print(0.2 == float(str(0.2))) # but this returns True!
Run Code Online (Sandbox Code Playgroud)
从文档中我找不到任何可以解释的东西.它确实说明:
...此外,Fraction构造函数也接受任何表示有限值且被float构造函数接受的字符串...
但对我来说,这意味着一种类似的行为float(),我只是看不到如上所示.
重要的是要注意,上面显示的行为并不特定于值(0.2),而是一般的; 我试过的一切都表现得一样.
有趣的是:
from fractions import Fraction
for x in range(1, 257):
if Fraction(str(1/x))==Fraction(1/x):
print(x)
Run Code Online (Sandbox Code Playgroud)
仅打印小于所选上限的2的幂:
1
2
4
8
16
32
64
128
256
Run Code Online (Sandbox Code Playgroud)
如果给出了一个字符串,请查看fractions.py中的def __new__():实现:
正则表达式_RATIONAL_FORMAT(见链接,如果您有兴趣的解析部分)推出numerator的0,并decimal作为2
从fractions.py源开始引用,由我发表评论
elif isinstance(numerator, str):
# Handle construction from strings.
m = _RATIONAL_FORMAT.match(numerator)
if m is None:
raise ValueError('Invalid literal for Fraction: %r' %
numerator)
numerator = int(m.group('num') or '0') # 0
denom = m.group('denom')
if denom: # not true for your case
denominator = int(denom)
else: # we are here
denominator = 1
decimal = m.group('decimal') # yep: 2
if decimal:
scale = 10**len(decimal) # thats 10^1
numerator = numerator * scale + int(decimal) # thats 0 * 10^1+0 = 10
denominator *= scale # thats 1*2
exp = m.group('exp')
if exp: # false
exp = int(exp)
if exp >= 0:
numerator *= 10**exp
else:
denominator *= 10**-exp
if m.group('sign') == '-': # false
numerator = -numerator
else:
raise TypeError("argument should be a string "
"or a Rational instance")
Run Code Online (Sandbox Code Playgroud)
从源头结束报价
所以'0.2'被解析为2 / 10 = 0.2 精确,而不是我的计算器发出的最接近的浮点近似值0,20000000000000001110223024625157
Quintessential:它们不是简单地使用,float( yourstring )而是解析和计算字符串本身,这就是两者不同的原因.
如果使用相同的构造函数并提供float或decimal构造函数使用内置函数as_integer_ratio()来获取分子和分母作为该数字的表示.
浮点表示最接近0.2的是0,20000000000000001110223024625157,这正是该as_integer_ratio()方法返回的指数和分母.
正如eric-postpischil
和Mark-dickinson指出的那样,这个浮动值受其二进制表示限制为"接近0.2".当投入str()将被截断到精确'0.2'- 因此之间的差异
print(Fraction(0.2)) # -> 3602879701896397/18014398509481984
print(Fraction(str(0.2))) # -> 1/5
Run Code Online (Sandbox Code Playgroud)