python float 精度 处理
way1:Decimal
示例代码:
from decimal import Decimal numStr = '1.13' print('====a', Decimal(numStr) * Decimal('1e8')) print('====b', Decimal(numStr) * Decimal(1e8)) print('====c', Decimal(float(numStr)) * Decimal('1e8')) print('====d', Decimal(float(numStr)) * Decimal(1e8))
输出结果:
示例代码输出结果:
====a 1.13E+8
====b 113000000.00
====c 112999999.9999999893418589636
====d 112999999.9999999893418589636
示例1说明: numStr 1.13 × 1e8期望值是113000000.00,而实际计算过程中因为float精度问题会导致计算结果出现偏差。具体原因可以看文末的参考。示例1是使用Decimal计算的结果对比,可以看出如果要得出期望值时,可以考虑使用b输出结果的计算方式。
way2:round
示例代码:
numStr = '1.13' a = float(numStr)*1e8 print('====a', a) print('====b',round(a)) print('====c', round(a,2)) print('====d', round(a,3)) print('====e', '%.3f'%a)
输出结果:
示例代码输出结果:
====a 112999999.99999999
====b 113000000
====c 113000000.0
====d 113000000.0
====e 113000000.000
示例2说明: 示例2是使用round() 方法返回浮点数a的四舍五入值。也可以得到期望值113000000.0,如果需要对计算结果进行小数点保留可以使用e输出结果的方式。
参考:
1、Python之☞float浮点数精度问题 - String-Lee - 博客园 (cnblogs.com)https://www.cnblogs.com/String-Lee/p/9840160.html
2、Asking for Help/How can I add or substract two to the last digit of a float? - Python Wikihttps://wiki.python.org/moin/Asking%20for%20Help/How%20can%20I%20add%20or%20substract%20two%20to%20the%20last%20digit%20of%20a%20float%3F?highlight=%28%5CbCategoryAskingForHelpAnswered%5Cb%29
3、
11. Brief Tour of the Standard Library — Part II — Python 3.10.4 documentationhttps://docs.python.org/3/tutorial/stdlib2.html#decimal-floating-point-arithmetic
4、Python3.中的浮点数运算(存在的.99999问题)_小白羊000的博客-CSDN博客https://blog.csdn.net/qq_41447652/article/details/123953908?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_title~default-1.pc_relevant_paycolumn_v3&spm=1001.2101.3001.4242.2&utm_relevant_index=4%E2%80%8B
python float 精度 处理相关推荐
- python float 精度_浅谈Python里面小数点精度的控制
要求较小的精度 round()内置方法 这个是使用最多的,刚看了round()的使用解释,也不是很容易懂.round()不是简单的四舍五入的处理方式. For the built-in types s ...
- python float精度问题_Python之☞float浮点数精度问题
(下面是精度漏洞的问题-转自其它) 从下面这段脚本体现出来: >>> x = 0.0 >>> for i in range(10): x += 0.1 print( ...
- python float 精度_谈谈关于Python里面小数点精度控制的问题
基础 浮点数是用机器上浮点数的本机双精度(64 bit)表示的.提供大约17位的精度和范围从-308到308的指数.和C语言里面的double类型相同.Python不支持32bit的单精度浮点数.如果 ...
- python float 精度_numpy.float128的内部精度是多少?
numpy.longdouble指的是C编译器调用的任何类型long double.当前,这是numpy支持的唯一扩展精度浮点类型. 在x86-32和x86-64上,这是一种80位浮点类型.在更特殊的 ...
- python float 精度_改变Float的精度并在Python中存储
我一直在寻找答案,只发现了我的问题. 我通过这个过程来评论代码,说明哪些有效,哪些无效 我为每一行得到了什么错误.提前致谢. # # list_of_numbers is a list with nu ...
- [转载] python 超高精度除法_Python十进制-除法,舍入,精度
参考链接: Python中的精度处理 python 超高精度除法 Python decimal module helps us in division with proper precision an ...
- python 超高精度除法_Python十进制-除法,舍入,精度
python 超高精度除法 Python decimal module helps us in division with proper precision and rounding of numbe ...
- double java 精度丢失_java中double和float精度丢失问题及解决方法
在讨论两位double数0.2和0.3相加时,毫无疑问他们相加的结果是0.5.但是问题总是如此吗? 下面我们让下面两个doubles数相加,然后看看输出结果: @Test public void te ...
- UE4角色Location远距离时动画抖动问题(float精度不够)解决方案
正题:关于UE4引擎当角色Location超过9999.999后,角色动画更新抖动问题的解决思路. 前提: 1.UE4引擎中距离单位是厘米(cm),也就说我们制作好1.8米的角色在UE4中为180个虚 ...
最新文章
- SiteMesh:一个优于Apache Tiles的Web页面布局、装饰框架
- pix4d怎么查看点云数据_python里怎么查看数据类型
- 在python中,用正则表达式提取多层括号中最外层括号包含的内容
- Appointment detail page loading never ends in Zeiss
- 函数中的指针分配的内存怎么释放
- 【SpringMVC】SpringMVC系列6之@CookieValue 映射请求Cookie 值
- 图像处理——DCT变换的学习笔记
- 夜间灯光数据dn值_NPPVIIRS年度夜间灯光数据的合成方法与验证
- linux黑域补丁,努比亚Z17mini+Z17+Z17S Nubia6.0典藏版 独家黑域 ROOT XP 特别推荐
- JavaWbe中文乱码解决方案
- Excel学习系列(1)--以文本存储的数字转换为数字
- Linux克隆后修改IP
- 《那些年啊,那些事——一个程序员的奋斗史》——47
- android自适应屏幕方向,Android 屏幕自适应方向尺寸与分辨率-Fun言
- MyBatis一发入魂(一)
- /usr/bin/ld:找不到 -lxxx
- SAP解决方案(典型业务场景)
- 标志寄存器的状态标志
- 将ES6的语法转为ES5(为了兼容 ES5) (Babel)
- 学会map、reduce、filter这三个函数,让你Python代码看起来更有逼格!
热门文章
- 阿里Java编码手册实战详解-命名规范篇
- 摩客串串(chinco)-UI设计师的交互演示利器
- 张维真:夫妻相处的人生哲学
- LCD1602教程(一)
- 中科大少年班,官方公布48名新生名单,有人因偶像曹原,有人只是不想经历高三...
- 英伟达的 VR 探索之路:图像渲染
- echarts图表之去掉折线图的折点圆圈,实测有效
- hibernate4.x自动建表时候创建外键失败ERROR: HHH000389: Unsuccessful: alter table ORDERS_NOPK1 add index
- 在VScode中配置@路径提示
- python listnode_Python,XML AttributeError:“NodeList”对象没有“firstChild”属性