Python+decimal完成精度计算的示例详解
作者:Sir 发布时间:2022-09-18 11:07:35
在进行小数计算的时候使用float,经常会出现小数位不精确的情况。在python编程中,推荐使用decimal来完成小数位的精度计算。
decimal是python中的标准库,直接将Decimal导入到代码块中使用。
decimal意思为十进制,这个模块提供了十进制浮点运算支持。通过几个常见的实战用例来说明一下其用法。
1. 浮点数转Decimal
使用Decimal.from_float函数将随便一个float类型的小数转换成Decimal的数据类型,结果float类型数据就显出原形了。
# It imports the Decimal class from the decimal module.
import decimal
from decimal import Decimal
# It converts the float 10.245 to a Decimal object.
decimal_ = Decimal.from_float(10.245)
print('浮点数转为Decimal后:{0}'.format(decimal_))
# 浮点数转为Decimal后:10.2449999999999992184029906638897955417633056640625
从结果来看,float浮点数转换完成以后精度位数就变得很长不是原先的三位小数了,这是因为float浮点数本身就不精确转换之后才会出现上面的效果。
2. Decimal除法设置
随机选两个整数将其定义为Decimal类型的数据,之后对这两个整个做除法通过保留相应的结果位数对其返回结果进行优化。
# It imports all the names from the decimal module into the current namespace.
from decimal import *
# It sets the precision of the decimal module to 8.
getcontext().prec = 8
# Dividing 1 by 6 and storing the result in the variable `decimal_`.
decimal_ = Decimal(1)/Decimal(6)
print('精确到8位小数后的结果是:{0}'.format(decimal_))
# 精确到8位小数后的结果是:0.16666667
很明显做除法以后的结果应该是一个无限小数,设置保留8位小数之后自动进行了四舍五入的计算得到0.16666667的结果。
3. Quantize设置结果
同样是保留了两位小数,使用quantize函数能完成同样的效果,默认结果也是经过了四舍五入的计算,若是想要固定小数位数使用此方法比较靠谱。
# It imports all the names from the decimal module into the current namespace.
from decimal import *
# Rounding the number 3.7829 to two decimal places.
decimal_ = Decimal('3.7829').quantize(Decimal('0.00'))
print('quantize设置保留两位小数后的结果:{0}'.format(decimal_))
# quantize设置保留两位小数后的结果:3.78
4. Decimal精度设置
这里还是做一个结果为无限小数的除法,分别使用向上取整、向下取整的方式保留一定位数的小数来说明问题。
一般情况下可能使用的都是向上取整,但是在一些领域比较金融、证券行业就必须采取向下取整的方式,首先来看一下常用的向上取整的方式来保留小数。
# It imports all the names from the decimal module into the current namespace.
from decimal import *
# It sets the rounding mode to ROUND_CEILING.
getcontext().rounding = getattr(decimal, 'ROUND_CEILING')
# It sets the precision of the decimal module to 10.
getcontext().prec = 10
# Converting the integer 9 to a string and then converting it to a Decimal object.
decimal_ = Decimal(1) / Decimal(str(9))
print('向上取整保留10位小数:{0}'.format(decimal_.quantize(Decimal('0.0000000000'))))
# 向上取整保留10位小数:0.1111111112
这里有个问题就是,如果getcontext().prec已经设置小数位是10,那么在使用quantize函数固定小数位的时候就必须不超过10位才行,也就是不能超过有效位数否则就会报错。
接下来看一下向下取整,向下取整的小数保留方式只需要修改getcontext().rounding的属性为向下取整即可,为了对比结果我们还是采用同样的数据来看看效果。
# It imports all the names from the decimal module into the current namespace.
from decimal import *
# It sets the rounding mode to ROUND_CEILING.
getcontext().rounding = getattr(decimal, 'ROUND_FLOOR')
# It sets the precision of the decimal module to 10.
getcontext().prec = 10
# Converting the integer 9 to a string and then converting it to a Decimal object.
decimal_ = Decimal(1) / Decimal(str(9))
print('向下取整保留10位小数:{0}'.format(decimal_.quantize(Decimal('0.0000000000'))))
# 向下取整保留10位小数:0.1111111111
可以发现同样的数据做除法,向下取整时结果是0.1111111111,向上取整时结果是0.1111111112。
同样,还是很多的其他保留小数的方式可以使用比如四舍五入的方式,这也是很多很多行业会采取的一种小数保留的方式,再演示一下四舍五入时的保留小数。
# It imports all the names from the decimal module into the current namespace.
from decimal import *
# It sets the rounding mode to ROUND_HALF_UP.
getcontext().rounding = getattr(decimal, 'ROUND_HALF_UP')
# It sets the precision of the decimal module to 4.
getcontext().prec = 5
# It converts the string '3.14159' to a Decimal object.
decimal_ = Decimal('3.14159')
print('四舍五入保留4位小数:{0}'.format(decimal_.quantize(Decimal('0.0000'))))
# 四舍五入保留4位小数:3.1416
将3.14159通过四舍五入的方式保留4位小数之后就变成了3.1416,和我们预想的结果一样。
来源:https://mp.weixin.qq.com/s/P1wapmw96-aFG1iULR9VlA


猜你喜欢
- 通过学习ASP明明白白你的If语句流程。If condition Then [statements1]E
- mysql存储过程delimiter $DROP FUNCTION IF EXISTS `fun_convert`$CREATE DEFIN
- 这篇文章主要介绍了使用python脚本自动创建pip.ini配置文件代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的
- 前面讲述了"专题一.函数的基础知识",而这篇文章讲述的Python的条件语句和循环语句的基础知识.主要内容包括:1.条件
- PHP的header函数 可以很少代码就能实现HTML代码中META 标签这里只说用 header函数来做页面的跳转1. HTML代码中页面
- 猜测下面这段程序的输出:class A(object): def __init__(self):
- Windows下的安装:下载地址:https://pypi.python.org/pypi/pyquery/#downloads下载后安装:
- 一:使用where少使用having;二:查两张以上表时,把记录少的放在右边;三:减少对表的访问次数;四:有where子查询时,子查询放在最
- 本次爬虫用到的网址是:http://www.netbian.com/index.htm: 彼岸桌面.里面有很多的好看壁纸,而且都是可以下载高
- 一般情况下,我们喜欢使用Session储存我们的变量。Asp.Net提供了下面一些方法储存Session的值: InProc&n
- 安装的方式很常规,直接使用pip安装就行了。pip install fpdf将需要使用的三方模块导入进来from fpdf import F
- 一、requests库requests是使用Apache2 licensed 许可证的HTTP库。比urllib模块更简洁。Request支
- 一、ROC与AUC很多学习器是为了测试样本产生的一个实值或概率预测,然后将这个预测值与一个分类阈值(threshold)进行比较,若大于阈值
- 这篇文章是为了对网络模型的权重输出,可以用来转换成其他框架的模型。import tensorflow as tffrom tensorflo
- 第一步:下载和安装python-3.4.4amd.msi可以去官方网站下载,也可以从网盘下载: 链接: https://pan.baidu.
- 本文实例为大家分享了利用python和OpenCV实现图像拼接,供大家参考,具体内容如下python+OpenCV实现image stitc
- tkinter是python的标准Tk GUI工具包的接口,在windows下如果你安装的python3,那在安装python的时候,就已经
- 不管是写自定义标签还是过滤器,第一件要做的事是创建模板库(Django能够导入的基本结构)。创建一个模板库分两步走:
- 目录概述针对同一类型问题的多种处理方式一、不使用策略模式二、策略模式UML总结示例概述定义一系列算法,将每个算法封装起来。并让它们能够相互替
- logging分为4个模块: loggers, handlers, filters, and formatters.●loggers: 提供