长整型太大,无法转换为浮点型 [英] Long int too large to convert to float

查看:270
本文介绍了长整型太大,无法转换为浮点型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个函数函数

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)

但我收到溢出错误: long int太大,无法转换为float

如何解决此问题?

推荐答案

您可以尝试以下操作:

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)

输出:

1.267976953480962421753016371E-375






哦,还有一个数学模块中的阶乘函数已经在其中,只需包括来自数学导入阶乘 $ c>在文件顶部以获取对该文件的访问权限。


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.

这篇关于长整型太大,无法转换为浮点型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆