Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python除法有许多小数位_Python_Python 3.x_Math_Decimal_Division - Fatal编程技术网

Python除法有许多小数位

Python除法有许多小数位,python,python-3.x,math,decimal,division,Python,Python 3.x,Math,Decimal,Division,嗨,我想知道如何让python输出一个比默认值多出许多小数位的答案 例如: 当前打印(1/7)输出:0.14285714285 我希望能够输出:0.142857142857142857142857142857142857142857142857142857您需要使用十进制模块: 如果您想要完全任意的精度(您将非常需要达到该精度级别),我建议您查看该模块 我想,如果您只想做非常简单的算术,那么内置的decimal模块就足够了。对于任何更复杂的问题,我仍然推荐mpmath。您可以尝试以下方法: &g

嗨,我想知道如何让python输出一个比默认值多出许多小数位的答案

例如: 当前打印(1/7)输出:0.14285714285


我希望能够输出:0.142857142857142857142857142857142857142857142857142857

您需要使用
十进制
模块:


如果您想要完全任意的精度(您将非常需要达到该精度级别),我建议您查看该模块

我想,如果您只想做非常简单的算术,那么内置的
decimal
模块就足够了。对于任何更复杂的问题,我仍然推荐
mpmath
。您可以尝试以下方法:

>>> import decimal
>>> decimal.setcontext(decimal.Context(prec=100))
>>> decimal.Decimal(1.0) / decimal.Decimal(7.0)
Decimal('0.1428571428571428571428571428571428571428571428571428571428571428571428571428571428571428571428571429')

您还可以在一定程度上使用格式选项:
print(“%.100f”%(1.0/7))
试试看。您会发现它不会产生
0.142857
重复。
>>> from mpmath import mp
>>> mp.dps = 100
>>> mp.fdiv(1.0,7.0)
mpf('0.1428571428571428571428571428571428571428571428571428571428571428571428571428571428571428571428571428579')
>>> import decimal
>>> decimal.setcontext(decimal.Context(prec=100))
>>> decimal.Decimal(1.0) / decimal.Decimal(7.0)
Decimal('0.1428571428571428571428571428571428571428571428571428571428571428571428571428571428571428571428571429')