<分区>
我目前只是在学习 Python 的基础知识,我认为,作为一种学习练习,我会尝试编写一些近似于数字“e”的东西。无论如何,它总是给出小数点后 11 位的答案,我希望它给出更接近小数点后 1000 位的答案。我该怎么做?
<分区>
我目前只是在学习 Python 的基础知识,我认为,作为一种学习练习,我会尝试编写一些近似于数字“e”的东西。无论如何,它总是给出小数点后 11 位的答案,我希望它给出更接近小数点后 1000 位的答案。我该怎么做?
最佳答案
您确定需要让它们“更精确”吗?还是您只需要查看比 Python 默认显示更多的数字?
>>> import math
>>> math.pi
3.141592653589793
>>>
>>> '{0:0.2f}'.format(math.pi)
'3.14'
>>>
>>> '{0:0.30f}'.format(math.pi)
'3.141592653589793115997963468544'
>>>
>>> '{0:0.60f}'.format(math.pi)
'3.141592653589793115997963468544185161590576171875000000000000'
但是,请注意
Floating point numbers are usually implemented using double in C; information about the precision and internal representation of floating point numbers for the machine on which your program is running is available in
sys.float_info
我向你保证 pi 在 48 位数字后不会归零 :-)
关于python - 如何在 Python 中使数字更精确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19534743/