python 超高精度除法





Python decimal module helps us in division with proper precision and rounding of numbers.

Python十进制模块可以帮助我们进行适当的精度划分和数字舍入。

(Python decimal module)




python2 精确除法 python高精度除法_python2 精确除法


In this lesson on decimal module in Python, we will see how we can manage decimal numbers in our programs for precision and formatting and making calculations as well.

Python十进制模块的这一课中,我们将了解如何在程序中管理十进制数字,以进行精度和格式化以及计算。


The precision with decimal numbers is very easy to lose if numbers are not handled correctly. Let’s see how the decimal module and its available functions help in these areas.

如果数字处理不正确,十进制数字的精度很容易丢失。 让我们看看小数模块及其可用功能如何在这些方面提供帮助。

(Working with decimal numbers)

Decimal numbers are just the floating-point numbers with fixed decimal points. We must round off the numbers correctly based on our need, otherwise, the results can be unexpected. Python’s decimal module helps us to be more precise with decimal numbers.

小数只是具有固定小数点的浮点数。 我们必须根据需要正确地四舍五入数字,否则结果可能会出乎意料。 Python的十进制模块可帮助我们更精确地使用十进制数字。

(Need for decimal module)

Before actually putting this module to use, let’s see what precision are we talking about and establish why we need this module actually.

在实际使用此模块之前,让我们看看我们在谈论什么精度,并确定为什么实际上需要此模块。

Look at the following code snippet:

查看以下代码片段:


division = 72 / 7
print(division)

Let’s see the output for this program:


python2 精确除法 python高精度除法_python2 精确除法_02


让我们看一下该程序的输出:

Well, the answer was not actually accurate and there were no decimal points at all! Let’s see how we can correct this by using the decimal module.

好吧,答案实际上并不准确,根本没有小数点! 让我们看看如何使用十进制模块来纠正此问题。

(Using the decimal module)

In all the programs we make in this post, we will be importing the decimal module in all of them:

在本文中创建的所有程序中,我们将在所有程序中导入小数模块:

import decimal

It might be the case that we only import a specific function from this module. This can be done as:

可能是我们只从该模块导入特定功能的情况。 这可以通过以下方式完成:

from decimal import Decimal

Let’s start putting the decimal module into some programs.

让我们开始将十进制模块放入某些程序中。

(Python decimal module example)

We will start with examples related to the module now.

现在,我们将从与该模块有关的示例开始。


(Correcting Division with decimals)

Here, we will correct the program we wrote above to perform division which should have produced a floating-point result. Modified program with the decimal module will look like:

在这里,我们将更正上面编写的程序以执行应该产生浮点结果的除法。 带有十进制模块的修改程序如下所示:

import decimal

division = decimal.Decimal(72) / decimal.Decimal(7)
print(division)

Let’s see the output for this program:


python2 精确除法 python高精度除法_编程语言_03


To notice, the division is correct now and precise as well, or maybe it is too precise?

让我们看一下该程序的输出:

注意,该划分现在是正确且精确的,还是太精确了?

(Controllling Precision for single operation)

In the last program, there were 25 decimal places in the answer now. But what if we only wanted up to three places of decimal values? This can be controlled as well. Here, we will control the precision of the answer which will not reflect in other operations in our program:

在上一个程序中,答案现在有25个小数位。 但是,如果我们最多只需要三个小数位该怎么办? 这也是可以控制的。 在这里,我们将控制答案的精度,该精度不会反映在程序的其他操作中:

import decimal

with decimal.localcontext() as ctx:
    ctx.prec = 3
    division = decimal.Decimal(72) / decimal.Decimal(7)
    print(division)

again = decimal.Decimal(72) / decimal.Decimal(7)
print(again)

We did the division operation two times to prove a point. Let’s see the output for this program:


python2 精确除法 python高精度除法_python2 精确除法_04


Noticed something? The precision we set was only valid for a single time. Next time we did the division, we got back the same result.

我们做了两次除法运算以证明这一点。 让我们看一下该程序的输出:

注意到了什么? 我们设置的精度仅一次有效。 下次我们进行除法时,我们得到了相同的结果。

(Controllling Precision for complete program)

It is also possible to control precision globally in the program. This is not recommended all the time when you are dealing with a lot of numbers in your program. Here is an example:

也可以在程序中全局控制精度。 当您在程序中处理大量数字时,建议不要始终使用此方法。 这是一个例子:

import decimal

decimal.getcontext().prec = 3

division = decimal.Decimal(72) / decimal.Decimal(7)
print(division)

again = decimal.Decimal(72) / decimal.Decimal(7)
print(again)

Let’s see the output for this program:


python2 精确除法 python高精度除法_python2 精确除法_05


让我们看一下该程序的输出:

(Rounding the numbers)

It is possible to gracefully round the numbers with the round(...) function. Let’s try it:

可以使用round(...)函数优雅地将数字round(...) 。 让我们尝试一下:

import decimal

#Can be rounded to 13.48 or 13.49
rounded = round(13.485, 2)
print(rounded)

Let’s see the output for this program:


python2 精确除法 python高精度除法_python_06


The number in program can be rounded to 13.48 or 13.49. By default, the round(...) function rounds down. This can be changed as well:

让我们看一下该程序的输出:

程序中的数字可以四舍五入为13.48或13.49。 默认情况下, round(...)函数会向下round(...) 。 也可以更改:

import decimal

#Can be rounded to 13.48 or 13.49
rounded = round(13.485, 2)
print(decimal.Decimal(rounded).quantize(decimal.Decimal('0.00'), rounding=decimal.ROUND_UP))

Let’s see the output for this program:


python2 精确除法 python高精度除法_编程语言_07


让我们看一下该程序的输出:

(Getting Python Decimal Context)

If you are interested looking at default context set by default for decimal module, you can use the following script:

如果有兴趣查看默认为十进制模块设置的默认上下文,则可以使用以下脚本:

from decimal import *
print(getcontext())

Let’s see the output for this program:


python2 精确除法 python高精度除法_编程语言_08


让我们看一下该程序的输出:

That’s all for python decimal module, it’s very useful when working with float point numbers.

这就是python十进制模块的全部内容,在处理浮点数时非常有用。

翻译自: https://www.journaldev.com/18804/python-decimal-division-round-precision

python 超高精度除法