Long int too large to convert to float

浪子不回头ぞ 提交于 2019-12-20 03:21:59

问题


Assuming I have a program with the function

def fakultaet(x):
    if x>1:
        return(x* fakultaet(x-1)) 
    else:
        return(1)

that returns the factorial of a given number, I need to calculate

1.0/fakultaet(200)

but I get an overflow error: long int too large to convert to float.

How can I solve this problem?


回答1:


You could try this:

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)

Output:

1.267976953480962421753016371E-375

Oh, and also, there is a factorial function in the math module already, just include from math import factorial at the top of your file to obtain access to it.



来源:https://stackoverflow.com/questions/13316045/long-int-too-large-to-convert-to-float

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!