我是靠谱客的博主 平常小虾米,这篇文章主要介绍解决python的OverflowError: int too large to convert to float解决python的OverflowError: int too large to convert to float,现在分享给大家,希望可以做个参考。

解决python的OverflowError: int too large to convert to float

测试样例为:1000
错误代码:

def  main(): 
        num  =  eval(input()) 
        calculate_e(num) 
def calculate_e(n):
    t=1
    s=1
    for i in range(1,n+1):
        t=t*i
        s=s+1.0/t
    print('%.6f'%(s))
main() 

上述代码会出现报错:OverflowError: int too large to convert to float
应该改成如下,才正确:

def  main(): 
        num  =  eval(input()) 
        calculate_e(num) 
def calculate_e(n):
    t=1
    s=1
    for i in range(1,n+1):
        t=t*i
        s=s+1/t
    print('%.6f'%(s))
main() 

正确输出:
在这里插入图片描述

最后

以上就是平常小虾米最近收集整理的关于解决python的OverflowError: int too large to convert to float解决python的OverflowError: int too large to convert to float的全部内容,更多相关解决python的OverflowError:内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(47)

评论列表共有 0 条评论

立即
投稿
返回
顶部