Euler #26,如何以更高的精度将有理数转换为字符串?

发布于 2024-08-11 03:02:43 字数 129 浏览 1 评论 0原文

我想获得更精确的 1/7 ,但它被截断了。有理数转换时如何获得更好的精度?

>>> str(1.0/7)[:50]
'0.142857142857'

I want to get 1/7 with better precision, but it got truncated. How can I get better precision when I convert a rational number?

>>> str(1.0/7)[:50]
'0.142857142857'

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

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

发布评论

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

评论(4

北凤男飞 2024-08-18 03:02:43

Python 有一个用于任意精度计算的内置库:Decimal。例如:

>>>from decimal import Decimal, getcontext
>>>getcontext().prec = 50
>>>x = Decimal(1)/Decimal(7)
>>>x
Decimal('0.14285714285714285714285714285714285714285714285714')
>>>str(x)
'0.14285714285714285714285714285714285714285714285714'

有关更多详细信息,请参阅 Python Decimal 文档。您可以将精度更改为您需要的最高值。

Python has a built-in library for arbitrary-precision calculations: Decimal. For example:

>>>from decimal import Decimal, getcontext
>>>getcontext().prec = 50
>>>x = Decimal(1)/Decimal(7)
>>>x
Decimal('0.14285714285714285714285714285714285714285714285714')
>>>str(x)
'0.14285714285714285714285714285714285714285714285714'

Look at the Python Decimal documentation for more details. You can change the precision to be as high as you need.

我是男神闪亮亮 2024-08-18 03:02:43

您可以将分子乘以一个大的 10^N 并坚持使用任意精度的整数。

编辑

我的意思是:

> def digits(a,b,n=50): return a*10**n/b
.
> digits(1,7)
14285714285714285714285714285714285714285714285714L

Python的整数是任意精度的。 Python 的浮点数从来都不是任意精度的。 (正如另一个答案所指出的那样,您必须使用十进制)

You could multiply the numerator by a large 10^N and stick with arbitrary-precision integers.

EDIT

i mean:

> def digits(a,b,n=50): return a*10**n/b
.
> digits(1,7)
14285714285714285714285714285714285714285714285714L

Python's integers are arbitrary precision. Python's floats are never arbitrary precision. (you'd have to use Decimal, as another answer has pointed out)

素染倾城色 2024-08-18 03:02:43

使用 Perl(因为我不会写 Python;-):

use strict; use warnings;

use integer;

my $x = 1;
my $y = 7;

for (1 .. 50) {
    $x *= 10 if $x < $y;
    my $q = $x / $y;
    $x -= $q * $y;
    print $q;
}

print "\n";
14285714285714285714285714285714285714285714285714

正如您可以手动验证的那样,数字是重复的。使用 "%.50f" 打印会给您带来更精确的错觉

Using Perl (because I can't write Python ;-):

use strict; use warnings;

use integer;

my $x = 1;
my $y = 7;

for (1 .. 50) {
    $x *= 10 if $x < $y;
    my $q = $x / $y;
    $x -= $q * $y;
    print $q;
}

print "\n";
14285714285714285714285714285714285714285714285714

As you can verify by hand, the digits repeat. Printing using "%.50f" will give you the illusion of more precision.

鹊巢 2024-08-18 03:02:43

使用 gmpy

>>> import gmpy
>>> thefraction = gmpy.mpq(1, 7)
>>> hiprecfloat = gmpy.mpf(thefraction, 256)
>>> hiprecfloat.digits(10, 50, -10, 10)
'0.14285714285714285714285714285714285714285714285714'
>>> 

您无法使用普通浮动来做到这一点 - 它们只是不这样做有足够的精度 50 位数字!我想有一种方法可以用 fractions.Fraction 来做到这一点(在 2.6 或更高版本中),但我不熟悉除 '1/7'< 以外的任何格式化方法/code> (对你的情况来说不是很有用!-)。

With gmpy:

>>> import gmpy
>>> thefraction = gmpy.mpq(1, 7)
>>> hiprecfloat = gmpy.mpf(thefraction, 256)
>>> hiprecfloat.digits(10, 50, -10, 10)
'0.14285714285714285714285714285714285714285714285714'
>>> 

You can't do it with normal floats -- they just don't have enough precision for 50 digits! I imagine there's a way to do it (in 2.6 or better) with fractions.Fraction, but I'm not familiar with any way to format it otherwise than as '1/7' (not very useful in your case!-).

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