long int太大而无法转换为float

vau*_*uge 2 python casting

假设我有一个带有该功能的程序

def fakultaet(x):
    if x>1:
        return(x* fakultaet(x-1)) 
    else:
        return(1)
Run Code Online (Sandbox Code Playgroud)

返回给定数字的阶乘,我需要计算

1.0/fakultaet(200)
Run Code Online (Sandbox Code Playgroud)

但我得到一个溢出错误:long int too large to convert to float.

我怎么解决这个问题?

ars*_*jii 6

你可以试试这个:

from decimal import Decimal

def fakultaet(x):  # as you have it currently
    if x>1:
        return(x * fakultaet(x-1)) 
    else:
        return(1)

print Decimal(1.0) / fakultaet(200)
Run Code Online (Sandbox Code Playgroud)

输出:

1.267976953480962421753016371E-375
Run Code Online (Sandbox Code Playgroud)

哦,而且,模块中已经有一个factorial功能math,只需包含from math import factorial在文件的顶部即可获取对它的访问权限.