【问题标题】:OverflowError: long int too large to convert to float [duplicate]溢出错误:long int 太大而无法转换为浮点数 [重复]
【发布时间】:2014-01-08 11:49:51
【问题描述】:

我试图得到一个非常大的数字的平方根,但我得到了错误: deltaSqrt = pow(delta,0.5) OverflowError: long int 太大而无法转换为浮点数

在我的例子中,delta 等于:

5097524159124305711208346976972093994517918559319839193986818402316359809127198287961957143680580475665158537123211669238507145109614915183501090991258372348911567096198391700545859284651871243167548321047645673131690445736385731455226353155143585522960326625070327122610654962530056330418391386124854577090206480385789275416714631025155369128530489779489101162403615113670950177532664946764525175541382065187304866582420329863524912760301704277886453413147449455323732476653550495366827445013669840800229684474814585992820804300231060966713580804079322252173910482245551821723868004571663524727449944378683955667216 P>

我应该怎么做才能得到这个数字的平方根?

【问题讨论】:

  • 您的建议非常有效。非常感谢!

标签: python python-2.7 square-root


【解决方案1】:

使用decimal:

import decimal
>>> d = decimal.Decimal('5097524159124305711208346976972093994517918559319839193986818402316359809127198287961957143680580475665158537123211669238507145109614915183501090991258372348911567096198391700545859284651871243167548321047645673131690445736385731455226353155143585522960326625070327122610654962530056330418391386124854577090206480385789275416714631025155369128530489779489101162403615113670950177532664946764525175541382065187304866582420329863524912760301704277886453413147449455323732476653550495366827445013669840800229684474814585992820804300231060966713580804079322252173910482245551821723868004571663524727449944378683955667216')
>>> d.sqrt()
Decimal('7.139694782779097001143800270E+307')

【讨论】:

    【解决方案2】:

    如果没有其他方法,试试这个: http://code.google.com/p/gmpy/

    【讨论】:

    • gmpy2.isqrt_rem() 将给出整数平方根和余数。对于给定的值,余数为 0,因此返回精确的平方根。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-22
    相关资源
    最近更新 更多