舍入小数的问题(python) [英] Problems with Rounding Decimals (python)

查看:82
本文介绍了舍入小数的问题(python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序中,十进制精度非常重要。

我的许多计算必须精确到许多小数位(例如50)。

In my program, decimal accuracy is very important.
A lot of my calculations must be accurate to many decimal places (such as 50).

因为我使用的是python,所以我一直在使用十进制模块
(带有context()。prec =99。即; 设置为具有实例化小数对象时,精度为99个小数位

,因为pythonic浮点数不允许这种精度附近的任何地方。

Because I am using python, I have been using the decimal module (with context().prec = 99. ie; Set to have 99 decimal places of accuracy when instantiating a decimal object)
as pythonic floats don't allow anywhere near such accuracy.

我希望用户指定计算精度的小数位,我不得不在代码中实现几个round()函数。

不幸的是,内置的round函数和小数对象没有

Since I wish for the User to specify the decimal places of accuracy of the calculations, I've had to implement several round() functions in my code.
Unfortuneately, the inbuilt round function and the decimal object do not interact well.

round(decimal.Decimal('2.000000000000000000001'),50)

# Number is 1e-21.   There are 21 decimal places, much less than 50.

结果为 2.0 而不是 2.000000000000000000001

舍入功能不能四舍五入到50。少得多!

Yet the result is 2.0 instead of 2.000000000000000000001
The round function is not rounding to 50. Much less!

是的,我有确保不会在实例化Decimal对象时发生过度舍入,而是在调用round之后。

我总是将表示浮点数的字符串传递给Decimal构造函数,而不是pythonic浮点数。

Yes, I have made sure that the over-rounding does not occur on instantiation of the Decimal object, but after calling round.
I always pass strings representing floats to the Decimal constructor, never pythonic floats.

为什么圆函数对我这样做?

(我意识到它可能最初是为pythonic浮点数设计的,永远不能有那么多小数位,但是文档声称Decimal对象可以完美地集成到python代码中,并且与内置的python函数兼容!)

Why is the round function doing this to me?
(I realise that it was probably originally designed for pythonic floats which can never have so many decimal places, but the documentation claims that the Decimal object integrates perfectly into python code and is compatible with the inbuilt python functions!)

非常感谢!

(这对我来说相当紧张,因为此问题破坏了整个程序的使用)

Thanks profusely!
(This has me quite unnerved, since this problem undermines the use of the entire program)

Specs:

python 2.7.1

Windows 7

十进制模块(内置)

Specs:
python 2.7.1
Windows 7
decimal module (inbuilt)

推荐答案

问题出在我附近可以确定,是 round()返回的是Python float 类型,而不是十进制类型。因此,在十进制模块上设置的精度并不重要,因为一旦调用 round(),您就不会不再使用十进制

The problem, as near as I can determine it, is that round() is returning a Python float type, and not a Decimal type. Thus it doesn't matter what precision you set on the decimal module, because once you call round() you no longer have a Decimal.

要解决此问题,您可能必须想出另一种方法舍入不依赖 round()的数字。例如Raymond的建议。

To work around this, you'll probably have to come up with an alternative way to round your numbers that doesn't rely on round(). Such as Raymond's suggestion.

您可能会发现以下简短的ideone示例说明: http:// ideone .com / xgPL9

You may find this short ideone example illustrative: http://ideone.com/xgPL9

这篇关于舍入小数的问题(python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆