使用 Python 计算精确分数

发布于 2025-01-14 19:36:24 字数 723 浏览 4 评论 0原文

我没有看到以下代码所期望的数学结果,我相信它应该产生谐波级数:

from fractions import Fraction

def sum_fracs(n):
    if n == 1:
        return 1
    return 1/n + sum_fracs(n - 1)

for n in range(1, 6):
    print(sum_fracs(n).as_integer_ratio())
    
for n in range(1, 6):
    print(Fraction(sum_fracs(n)))

输出:

(1, 1)
(3, 2)
(8256599316845909, 4503599627370496)
(2345624805922133, 1125899906842624)
(1285402393645329, 562949953421312)
1
3/2
8256599316845909/4503599627370496
2345624805922133/1125899906842624
1285402393645329/562949953421312

两种方法都没有达到

1   

3/2

11/6    

25/12   

137/60

我的预期。我知道浮点数可能有舍入误差,但我希望这种基本的东西在 Python 中是可能的。

非常感谢任何帮助。

I'm not seeing the mathematical results I'm expecting from the following code, which I believe should produce the Harmonic Series:

from fractions import Fraction

def sum_fracs(n):
    if n == 1:
        return 1
    return 1/n + sum_fracs(n - 1)

for n in range(1, 6):
    print(sum_fracs(n).as_integer_ratio())
    
for n in range(1, 6):
    print(Fraction(sum_fracs(n)))

Output:

(1, 1)
(3, 2)
(8256599316845909, 4503599627370496)
(2345624805922133, 1125899906842624)
(1285402393645329, 562949953421312)
1
3/2
8256599316845909/4503599627370496
2345624805922133/1125899906842624
1285402393645329/562949953421312

Neither approach gives

1   

3/2

11/6    

25/12   

137/60

as I was hoping. I know floats can have rounding errors, but I would hope that something this basic would be possible in Python.

Any help much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我早已燃尽 2025-01-21 19:36:24

您运行 Fraction(x),其中 x 是浮点数。这已经太晚了,你已经失去了精度,所以你的分数的精度与浮点数的精度一样好。

在函数中使用Fraction

def sum_fracs(n):
    if n == 1:
        return 1
    return Fraction(1, n) + sum_fracs(n - 1)

for n in range(1, 6):
    print(sum_fracs(n).as_integer_ratio())

输出:

(1, 1)
(3, 2)
(11, 6)
(25, 12)
(137, 60)

NB。 fraction 文档中清楚地说明了这一点< /em>

请注意,由于二进制浮点的常见问题(请参阅浮点算术:问题和限制),Fraction(1.1) 的参数不完全等于 11/10,因此 Fraction(1.1) 不返回正如人们所期望的那样,Fraction(11, 10) 是这样的。 (但请参阅 limit_denominator()< 的文档/code>下面的方法。)


You run Fraction(x) where x is a float. This is too late, you already lost precision, so your fraction's precision is as good as that of the float.

Use Fraction in the function:

def sum_fracs(n):
    if n == 1:
        return 1
    return Fraction(1, n) + sum_fracs(n - 1)

for n in range(1, 6):
    print(sum_fracs(n).as_integer_ratio())

output:

(1, 1)
(3, 2)
(11, 6)
(25, 12)
(137, 60)

NB. this is clearly stated in the fraction documentation

Note that due to the usual issues with binary floating-point (see Floating Point Arithmetic: Issues and Limitations), the argument to Fraction(1.1) is not exactly equal to 11/10, and so Fraction(1.1) does not return Fraction(11, 10) as one might expect. (But see the documentation for the limit_denominator() method below.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文