fractions.Fraction() returns 不同的名称,名称。解析浮点数或其字符串表示形式时对
fractions.Fraction() returns different nom., denom. pair when parsing a float or its string representation
我知道 floating point math 的性质,但我仍然发现以下内容令人惊讶:
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!
从 documentation 我找不到任何可以解释这一点的内容。它确实声明:
...In addition, any string that represents a finite value and is
accepted by the float constructor is also accepted by the Fraction
constructor...
但对我来说,这意味着与 float()
类似的行为,我只是没有看到,如上所示。
有什么解释吗?
请务必注意,上面显示的行为并非特定于值 (0.2
),而是一般情况;我尝试过的一切都以同样的方式表现。
有趣的是:
from fractions import Fraction
for x in range(1, 257):
if Fraction(str(1/x))==Fraction(1/x):
print(x)
仅打印小于所选上限的 2 的 次幂:
1
2
4
8
16
32
64
128
256
注意分母的最后一位。 fractions
模块在内部存储对象时似乎考虑到了这一点,但在操作中使用时 python 可以舍入。
from fractions import Fraction
Fraction(3602879701896397, 18014398509481985) == Fraction(1, 5) # True
Fraction(3602879701896397, 18014398509481984) == Fraction(1, 5) # False
3602879701896397 / 18014398509481985 == 0.2 # True
3602879701896397 / 18014398509481984 == 0.2 # True
现在 fractions
模块为什么选择一个近似值(即 18014398509481984
而不是正确的 18014398509481985
)这个问题我无法回答。
查看 fractions.py 中的 def __new__():
实现,如果给定字符串:
正则表达式 _RATIONAL_FORMAT
(如果您对解析部分感兴趣,请参阅 link)将 numerator
输出为 0
,将 decimal
输出为 2
Start quote from fractions.py source, with comments by me
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")
end quote from source
所以 '0.2'
被准确地解析为 2 / 10 = 0.2
,而不是我的计算器在 0,20000000000000001110223024625157
[=33 处输出的最接近的浮点数近似值=]
精髓:它们不是简单地使用 float( yourstring )
,而是解析和计算字符串本身,这就是两者不同的原因。
如果您使用相同的构造函数并提供 float
或 decimal
,则构造函数使用内置 as_integer_ratio()
获取分子和分母作为该数字的表示。
浮点表示最接近 0.2 的是 0,20000000000000001110223024625157,这正是 as_integer_ratio()
方法 returns 的分母和分母。
作为eric-postpischil
mark-dickinson 指出,此浮点值受其二进制表示的限制为 "close to 0.2"。当放入 str()
时将被截断为精确的 '0.2'
- 因此
之间存在差异
print(Fraction(0.2)) # -> 3602879701896397/18014398509481984
print(Fraction(str(0.2))) # -> 1/5
在print(Fraction(0.2))
中,源文本0.2
被转换为浮点值。此转换的结果正好是 0.200000000000000011102230246251565404236316680908203125,或 3602879701896397/18014398509481984。然后将该值传递给 Fraction
,它会产生相同的值,表示为有理数。
在print(Fraction(str(0.2)))
中,0.2
再次转换为浮点值,产生上面的数字。然后 str
将其转换为字符串。在当前的 Python 版本中,当浮点值转换为字符串时,Python 通常不会产生精确的数学值。相反,它只生成足够的数字,以便将字符串转换回浮点数生成输入数字。在这种情况下,结果为“0.2”。所以字符串“0.2”被传递给Fraction
。然后Fraction
分析“0.2”,确定是1/5。
我知道 floating point math 的性质,但我仍然发现以下内容令人惊讶:
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!
从 documentation 我找不到任何可以解释这一点的内容。它确实声明:
...In addition, any string that represents a finite value and is accepted by the float constructor is also accepted by the Fraction constructor...
但对我来说,这意味着与 float()
类似的行为,我只是没有看到,如上所示。
有什么解释吗?
请务必注意,上面显示的行为并非特定于值 (0.2
),而是一般情况;我尝试过的一切都以同样的方式表现。
有趣的是:
from fractions import Fraction
for x in range(1, 257):
if Fraction(str(1/x))==Fraction(1/x):
print(x)
仅打印小于所选上限的 2 的 次幂:
1
2
4
8
16
32
64
128
256
注意分母的最后一位。 fractions
模块在内部存储对象时似乎考虑到了这一点,但在操作中使用时 python 可以舍入。
from fractions import Fraction
Fraction(3602879701896397, 18014398509481985) == Fraction(1, 5) # True
Fraction(3602879701896397, 18014398509481984) == Fraction(1, 5) # False
3602879701896397 / 18014398509481985 == 0.2 # True
3602879701896397 / 18014398509481984 == 0.2 # True
现在 fractions
模块为什么选择一个近似值(即 18014398509481984
而不是正确的 18014398509481985
)这个问题我无法回答。
查看 fractions.py 中的 def __new__():
实现,如果给定字符串:
正则表达式 _RATIONAL_FORMAT
(如果您对解析部分感兴趣,请参阅 link)将 numerator
输出为 0
,将 decimal
输出为 2
Start quote from fractions.py source, with comments by me
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")
end quote from source
所以 '0.2'
被准确地解析为 2 / 10 = 0.2
,而不是我的计算器在 0,20000000000000001110223024625157
[=33 处输出的最接近的浮点数近似值=]
精髓:它们不是简单地使用 float( yourstring )
,而是解析和计算字符串本身,这就是两者不同的原因。
如果您使用相同的构造函数并提供 float
或 decimal
,则构造函数使用内置 as_integer_ratio()
获取分子和分母作为该数字的表示。
浮点表示最接近 0.2 的是 0,20000000000000001110223024625157,这正是 as_integer_ratio()
方法 returns 的分母和分母。
作为eric-postpischil
mark-dickinson 指出,此浮点值受其二进制表示的限制为 "close to 0.2"。当放入 str()
时将被截断为精确的 '0.2'
- 因此
print(Fraction(0.2)) # -> 3602879701896397/18014398509481984
print(Fraction(str(0.2))) # -> 1/5
在print(Fraction(0.2))
中,源文本0.2
被转换为浮点值。此转换的结果正好是 0.200000000000000011102230246251565404236316680908203125,或 3602879701896397/18014398509481984。然后将该值传递给 Fraction
,它会产生相同的值,表示为有理数。
在print(Fraction(str(0.2)))
中,0.2
再次转换为浮点值,产生上面的数字。然后 str
将其转换为字符串。在当前的 Python 版本中,当浮点值转换为字符串时,Python 通常不会产生精确的数学值。相反,它只生成足够的数字,以便将字符串转换回浮点数生成输入数字。在这种情况下,结果为“0.2”。所以字符串“0.2”被传递给Fraction
。然后Fraction
分析“0.2”,确定是1/5。