Python 尝试运行具有更高整数值(n>;20)的代码时出错
我是一个初级程序员,试图编写一个程序,请求一个限定范围内的整数n,然后计算某些数学函数,并在格式化表中显示它们 目前,我已经设置了限制,以便(0 10}'.格式(标题2), “{0:>18}”。格式(标题3), “{0:>20}”格式(标题4), “{0:>10}”。格式(标题5)) #使用numpy设置求和和和阶乘函数的运算符: def阶乘(n):阶乘算子的函数 如果n==1: 返回n elif n==0: 返回1 其他: 返回n*阶乘(n-1) def nsum(n):#这是n上的求和函数 如果n==0: 返回0 其他: returnint(n*(n+1)/2)#必须将字符串转换为整数格式 #参考:https://brilliant.org/wiki/sum-of-n-n2-or-n3/ #计算并打印for循环 对于范围(0,n+1)内的i: n_int=0+i 求和n=nsum(n_int) exp\u n=np.exp(n\u int) 事实n=阶乘(n_int) logfact\u n=np.log(阶乘(n\u int)) 打印({0:>8d})。格式(n_int), “{0:>10d}”。格式(总和), “{0:>18.3f}”。格式(exp_n), “{0:>20d}”。格式(事实), “{0:>10.3f}”。格式(logfact_n)) #--------------------------主程序结束----------------------------------# 代码运行良好,能够处理异常,但当我输入n=21时,我得到了AttributeErrorPython 尝试运行具有更高整数值(n>;20)的代码时出错,python,numpy,for-loop,Python,Numpy,For Loop,我是一个初级程序员,试图编写一个程序,请求一个限定范围内的整数n,然后计算某些数学函数,并在格式化表中显示它们 目前,我已经设置了限制,以便(0 10}'.格式(标题2), “{0:>18}”。格式(标题3), “{0:>20}”格式(标题4), “{0:>10}”。格式(标题5)) #使用numpy设置求和和和阶乘函数的运算符: def阶乘(n):阶乘算子的函数 如果n==1: 返回n elif n==0: 返回1 其他: 返回n*阶乘(n-1) def nsum(n):#这是n上的求和函数
AttributeError: 'int' object has no attribute 'log'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\pjjan\OneDrive\Documents\Python\forLoop3_19318421.py", line 79, in <module>
logfact_n = np.log(factorial(n_int))
TypeError: loop of ufunc does not support argument 0 of type int which has no callable log method
AttributeError:'int'对象没有属性'log'
上述异常是以下异常的直接原因:
回溯(最近一次呼叫最后一次):
文件“C:\Users\pjjan\OneDrive\Documents\Python\forLoop3_19318421.py”,第79行,在
logfact\u n=np.log(阶乘(n\u int))
TypeError:ufunc的循环不支持int类型的参数0,该参数没有可调用的日志方法
以下是完整的输出:
This program asks the user to enter an integer value n and computes
sum(n), exp(n), n! and ln(n). It then displays the results in a table
Please enter an integer (0<=n<=10000): 21
n sum(1..n) exp(n) n! ln(n!)
0 0 1.000 1 0.000
1 1 2.718 1 0.000
2 3 7.389 2 0.693
3 6 20.086 6 1.792
4 10 54.598 24 3.178
5 15 148.413 120 4.787
6 21 403.429 720 6.579
7 28 1096.633 5040 8.525
8 36 2980.958 40320 10.605
9 45 8103.084 362880 12.802
10 55 22026.466 3628800 15.104
11 66 59874.142 39916800 17.502
12 78 162754.791 479001600 19.987
13 91 442413.392 6227020800 22.552
14 105 1202604.284 87178291200 25.191
15 120 3269017.372 1307674368000 27.899
16 136 8886110.521 20922789888000 30.672
17 153 24154952.754 355687428096000 33.505
18 171 65659969.137 6402373705728000 36.395
19 190 178482300.963 121645100408832000 39.340
20 210 485165195.410 2432902008176640000 42.336
AttributeError: 'int' object has no attribute 'log'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\pjjan\OneDrive\Documents\Python\forLoop3_19318421.py", line 79, in <module>
logfact_n = np.log(factorial(n_int))
TypeError: loop of ufunc does not support argument 0 of type int which has no callable log method
此程序要求用户输入整数值n并计算
求和(n),经验(n),n!和ln(n)。然后将结果显示在表格中
请输入一个整数(0问题是您的“int”增长太快,导致溢出。请尝试使用long或double格式:
# evaluate and print in for loop
for i in range(0, n+1):
n_int = np.longdouble(0 + i)
sum_n = nsum(n_int)
exp_n = np.exp(n_int)
fact_n = factorial(n_int)
logfact_n = np.log(factorial(n_int))
print('{0:>8f}'.format(n_int),
'{0:>10f}'.format(sum_n),
'{0:>18.3f}'.format(exp_n),
'{0:>20f}'.format(fact_n),
'{0:>10.3f}'.format(logfact_n))
阶乘变得非常大,非常快。在20(20!~=2e18)之后,numpy
切换到没有log
方法的不同整数公式
您可以使用更高的精度,也可以利用相乘log
与在其外部添加相同的事实:
logfact_n += np.log(i)
(由于0的日志未定义,因此必须伪造第一个术语)
证明:
np.cumsum(np.log(np.arange(20)+1))
Out[]: #same as your results
array([ 0. , 0.69314718, 1.79175947, 3.17805383, 4.78749174,
6.57925121, 8.52516136, 10.6046029 , 12.80182748, 15.10441257,
17.50230785, 19.9872145 , 22.55216385, 25.19122118, 27.89927138,
30.67186011, 33.50507345, 36.39544521, 39.33988419, 42.33561646])
谢谢你的回答。我只是对这种方法很谨慎,因为即使没有“捏造第一个术语”,我仍然会得到相同的错误值n>20。你能澄清我应该如何编辑代码以使其运行超过20吗?否则,我可以限制用户的值为0,因为你(显然)如果您只对log
函数使用numpy
,您应该能够切换到使用math
包。它将处理Python long的日志。@hpaulj嗨,我在这个作业中只允许使用numpy
,因为我为阶乘运算符创建了一个uffunction。“uffunction”是什么?你的factorial
和nsum
函数不使用numpy
。它们是纯Python。math
是标准Python模块。在任何情况下,你都不能对非常大的整数使用np.log
。要么接受20的限制,要么使用math
@hpaulj好的,谢谢。是的,对不起,我这么说了ng.在这个特殊的“家庭作业”中,我不能使用数学
,只使用numpy
是其中一个要求,其他什么都不能使用。numpy
最终实现了,因为我需要取阶乘数(ln(n!)的自然日志。谢谢你的帮助。
np.cumsum(np.log(np.arange(20)+1))
Out[]: #same as your results
array([ 0. , 0.69314718, 1.79175947, 3.17805383, 4.78749174,
6.57925121, 8.52516136, 10.6046029 , 12.80182748, 15.10441257,
17.50230785, 19.9872145 , 22.55216385, 25.19122118, 27.89927138,
30.67186011, 33.50507345, 36.39544521, 39.33988419, 42.33561646])