在 IronPython 应用程序中设置精度?

发布于 2024-10-06 20:42:26 字数 750 浏览 0 评论 0原文

我已经阅读了足够多的内容,发现 getcontext().prec 是如何在普通 python 文件中设置精度的。

getcontext().prec = 100;
for i in range(1, 100):
    print 1.0 / i;

但是当我在 IronPython 中执行此操作时,我得到:

NameError: global name 'getcontext' is not Defined

是否有其他方法可以更改此设置,或者 IronPython 没有这种奢侈?

这是我的 VS 和 IronPython 版本信息:

Microsoft Visual Studio 2010
Version 10.0.30319.1 RTMRel
Microsoft .NET Framework
Version 4.0.30319 RTMRel

IronPython Tools for Visual Studio   1.0
IronPython Tools for Visual Studio provides intellisense, project support, project and item templates, as well as a REPL window for IronPython development.

编辑: 愚蠢的我,我在发布这篇文章后立即发现了答案(仍在学习 python 的一些基础知识)。我不会回答这个问题,但我会选择第一个给出正确答案的人。

I've read around enough to see that getcontext().prec is how to set the precision in a normal python file.

getcontext().prec = 100;
for i in range(1, 100):
    print 1.0 / i;

But when I execute this in IronPython, I get:

NameError: global name 'getcontext' is not defined

Is there an alternative way to change this, or does IronPython not get this luxury?

Here's my version info for VS and IronPython:

Microsoft Visual Studio 2010
Version 10.0.30319.1 RTMRel
Microsoft .NET Framework
Version 4.0.30319 RTMRel

IronPython Tools for Visual Studio   1.0
IronPython Tools for Visual Studio provides intellisense, project support, project and item templates, as well as a REPL window for IronPython development.

EDIT:
Stupid me, I discovered the answer right after posting this (still learning some of the basics of python). I'm not going to answer this, but I'll pick the first person who has the right answer.

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

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

发布评论

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

评论(1

东京女 2024-10-13 20:42:26

getcontext() 似乎是在decimal 模块中定义的函数。假设您将使用十进制模块进行数学计算,那么您可以这样做:

fromdecimal import getcontext
getcontext().prec = 10

这对我来说适用于 IronPython 2.6 和 2.7B1。

但是,如果您不遗余力地使用 .NET Decimal 类(通过 from System import Decimal),这将不会产生任何效果。

getcontext() would appear to be a function defined in the decimal module. Assuming you're going to be using the decimal module for math then you can just do:

from decimal import getcontext
getcontext().prec = 10

That works for me on both IronPython 2.6 and 2.7B1.

But if you were going out of your way to use the .NET decimal class (via from System import Decimal) this would have no effect.

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