提高浮点除法的精度(python) [英] Increase Accuracy of float division (python)

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

问题描述

我正在 PyCharm 中编写一些代码,我希望除法比现在更准确(40-50 个数字而不是大约 15 个数字).我怎样才能做到这一点?

谢谢.

解决方案

查看 decimal 模块:

<预><代码>>>>从十进制导入 *>>>getcontext().prec = 50>>>十进制(1)/十进制(7)十进制('0.14285714285714285714285714285714285714285714285714')

如果您对比 decimal 提供的更复杂的操作感兴趣,您还可以查看诸如 bigfloatmpmath(其中我用过,很喜欢.)

I'm writing a bit of code in PyCharm, and I want the division to be much more accurate than it currently is (40-50 numbers instead of about 15). How Can I accomplish this?

Thanks.

解决方案

Check out the decimal module:

>>> from decimal import *
>>> getcontext().prec = 50
>>> Decimal(1)/Decimal(7)
Decimal('0.14285714285714285714285714285714285714285714285714')

If you're interested in more sophisticated operations than decimal provides, you can also look at libraries like bigfloat, or mpmath (which I use, and like a lot.)

这篇关于提高浮点除法的精度(python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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