PI 小数点后第 n 位
PI nth digit after decimal
我想编写一个程序,它将 return PI 编号中小数点后的第 n 位数字。
只要我不将 n 设置为高于 50,它就可以正常工作。我可以修复它还是 Python 不允许这样做?
这是我的代码:
pi = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270938521105559644622948954930381964428
def nth_pi_digit(n=input("How many digits after decimal you would like to see?(PI number): ")):
try:
n = int(n)
if n <= 204:
return print(format(pi, f".{n}f"))
else:
print("Sorry, we have pi with 204 digits after decimal, so we'll print that")
return print(format(pi, ".204f"))
except:
print("You need to put a integer digit")
nth_pi_digit()
您可以简单地使用文本:
pi = '3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270938521105559644622948954930381964428'
虽然使用十进制更好。
将 PI 实例化为字符串对象:
pi = '3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270938521105559644622948954930381964428'
然后您可以执行切片以显示任意数量的小数:
n = 10 # 10 decimals
print(pi[:2+n])
float
数据类型对您可以获得的精确度有限制。大约有 50 个数字。
我建议使用 Decimal
来表示如此高精度的数字:
>>> from decimal import Decimal
>>> d = Decimal('3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270938521105559644622948954930381964428')
>>> format(d, ".50f")
'3.14159265358979323846264338327950288419716939937511'
>>> format(d, ".204f")
'3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102709385211055596446229489549303819644280'
这样您就可以在不损失精度的情况下对其进行数学运算,"treating it as a string" 不允许您这样做。
我想编写一个程序,它将 return PI 编号中小数点后的第 n 位数字。 只要我不将 n 设置为高于 50,它就可以正常工作。我可以修复它还是 Python 不允许这样做?
这是我的代码:
pi = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270938521105559644622948954930381964428
def nth_pi_digit(n=input("How many digits after decimal you would like to see?(PI number): ")):
try:
n = int(n)
if n <= 204:
return print(format(pi, f".{n}f"))
else:
print("Sorry, we have pi with 204 digits after decimal, so we'll print that")
return print(format(pi, ".204f"))
except:
print("You need to put a integer digit")
nth_pi_digit()
您可以简单地使用文本:
pi = '3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270938521105559644622948954930381964428'
虽然使用十进制更好。
将 PI 实例化为字符串对象:
pi = '3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270938521105559644622948954930381964428'
然后您可以执行切片以显示任意数量的小数:
n = 10 # 10 decimals
print(pi[:2+n])
float
数据类型对您可以获得的精确度有限制。大约有 50 个数字。
我建议使用 Decimal
来表示如此高精度的数字:
>>> from decimal import Decimal
>>> d = Decimal('3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270938521105559644622948954930381964428')
>>> format(d, ".50f")
'3.14159265358979323846264338327950288419716939937511'
>>> format(d, ".204f")
'3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102709385211055596446229489549303819644280'
这样您就可以在不损失精度的情况下对其进行数学运算,"treating it as a string" 不允许您这样做。