python超高精度除法_Python十进制-除法,舍入,精度

更新时间:2023-07-13 00:17:37 阅读: 评论:0

python超⾼精度除法_Python⼗进制-除法,舍⼊,精度
小猴子下山的故事python 超⾼精度除法
Python decimal module helps us in division with proper precision and rounding of .
Python⼗进制模块可以帮助我们进⾏适当的精度划分和舍⼊。
Python⼗进制模块 (Python decimal module)
In this lesson on decimal module in , we will e how we can manage decimal numbers in our programs for precision and formatting and making calculations as well.
在⼗进制模块的这⼀课中,我们将了解如何在程序中管理⼗进制数字,以进⾏精度和格式化以及计算。
The precision with decimal numbers is very easy to lo if numbers are not handled correctly. Let’s e how the decimal and its available functions help in the areas.
如果数字处理不正确,⼗进制数字的精度很容易丢失。 让我们看看⼩数及其可⽤功能如何在这些⽅⾯提供帮助。
使⽤⼗进制数字 (Working with decimal numbers)
Decimal numbers are just the floating-point numbers with fixed decimal points. We must round off the numbers correctly bad on our need, otherwi, the results can be unexpected. Python’s decimal module helps us to be more preci with decimal numbers.
⼩数只是具有固定⼩数点的浮点数。 我们必须根据需要正确地四舍五⼊数字,否则结果可能会出乎意料。 Python的⼗进制模块可帮助我们更精确地使⽤⼗进制数字。
需要⼗进制模块 (Need for decimal module)
Before actually putting this module to u, let’s e 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 e the output for this program:
让我们看⼀下该程序的输出:
Well, the answer was not actually accurate and there were no decimal points at all! Let’s e 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 ca 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⼗进制模块⽰例 (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 e the output for this program:
To notice, the division is correct now and preci as well, or maybe it is too preci?
让我们看⼀下该程序的输出:
注意,该划分现在是正确且精确的,还是太精确了?
单次操作的控制精度 (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 e the output for this program:
Noticed something? The precision we t 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
division = decimal.Decimal(72) / decimal.Decimal(7)
print(division)
again = decimal.Decimal(72) / decimal.Decimal(7)
print(again)
Let’s e the output for this program:
让我们看⼀下该程序的输出:
四舍五⼊ (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 e the output for this program:
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 e the output for this program:
让我们看⼀下该程序的输出:
获取Python⼗进制上下⽂ (Getting Python Decimal Context)
If you are interested looking at default context t by default for decimal module, you can u the following script:
如果有兴趣查看默认为⼗进制模块设置的默认上下⽂,则可以使⽤以下脚本:
from decimal import *
print(getcontext())
Let’s e the output for this program:
耽美文小说让我们看⼀下该程序的输出:
That’s all for python decimal module, it’s very uful when working with float point numbers.
麻雀电视剧剧情介绍
这就是python⼗进制模块的全部内容,在处理浮点数时⾮常有⽤。
equestrian
翻译⾃:
python 超⾼精度除法

本文发布于:2023-07-13 00:17:37,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1093385.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:进制   程序   模块   精度
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图