Ayu*_*hra 5 python python-2.7 python-3.x
我有两个大的数字a和b长度10000左右的,这样a <= b。现在,我必须找到c = a / b最多 10 位小数,如何在不失去精度的情况下做到这一点?
您可以使用该decimal模块:
from decimal import localcontext, Decimal
def foo(a, b):
with localcontext() as ctx:
ctx.prec = 10 # Sets precision to 10 places temporarily
c = Decimal(a) / Decimal(b) # Not sure if this is precise if a and b are floats,
# str(a) and str(b) instead'd ensure precision i think.
return float(c)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1206 次 |
| 最近记录: |