why does math.pow yields OverflowError (while python itself cancalculate that large number)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tzury Bar Yochay

    why does math.pow yields OverflowError (while python itself cancalculate that large number)

    What is the reason math.pow yields OverflowError while python itself
    can
    calculate these large numbers. e.g:
    >>import math
    >>math.pow(10 0, 154)
    1e+308
    >>math.pow(10 0, 155)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    OverflowError: math range error
    >>eval(('100* '* 155)[:-1])
    100000000000000 000000000000000 000000000000000 00000000000000
    000000000000000 000000000000000 000000000000000 00000000000000
    000000000000000 000000000000000 000000000000000 00000000000000
    000000000000000 000000000000000 000000000000000 00000000000000
    000000000000000 000000000000000 000000000000000 00000000000000
    000000000000000 0L
    >>>
  • John Machin

    #2
    Re: why does math.pow yields OverflowError (while python itself cancalculate that large number)

    On Oct 23, 8:21 pm, Tzury Bar Yochay <Afro.Syst...@g mail.comwrote:
    What is the reason math.pow yields OverflowError while python itself
    can
    calculate these large numbers. e.g:
    >
    >import math
    >math.pow(100 , 154)
    1e+308
    >math.pow(100 , 155)
    >
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    OverflowError: math range error>>eval(('1 00*'* 155)[:-1])
    >
    100000000000000 000000000000000 000000000000000 00000000000000
    000000000000000 000000000000000 000000000000000 00000000000000
    000000000000000 000000000000000 000000000000000 00000000000000
    000000000000000 000000000000000 000000000000000 00000000000000
    000000000000000 000000000000000 000000000000000 00000000000000
    000000000000000 0L
    Because math.pow returns a float; 100 ** 155 won't fit in a float.

    Comment

    • Tzury Bar Yochay

      #3
      Re: why does math.pow yields OverflowError (while python itself cancalculate that large number)

      Because math.pow returns a float; 100 ** 155 won't fit in a float.

      Sure that is the reason.
      May I rephrase, my question:
      Why not returning another type as long as we can calculate it?
      After all, math module is likely to be used on large numbers as well.

      Comment

      • Christian Heimes

        #4
        Re: why does math.pow yields OverflowError (while python itself cancalculate that large number)

        Tzury Bar Yochay wrote:
        >Because math.pow returns a float; 100 ** 155 won't fit in a float.
        >
        Sure that is the reason.
        May I rephrase, my question:
        Why not returning another type as long as we can calculate it?
        After all, math module is likely to be used on large numbers as well.
        Because it's very complicated to get it right. The math module is a thin
        wrapper around the platform math library. It took Mark and me very long
        to get cmath and math right for floats (C doubles). Arbitrary precision
        and arbitrary length numbers are a different story. You have to use 3rd
        party tools for those. The standard library aims to solve common
        problems, not special problems.

        Christian

        Comment

        • John Machin

          #5
          Re: why does math.pow yields OverflowError (while python itself cancalculate that large number)

          On Oct 23, 9:24 pm, Tzury Bar Yochay <Afro.Syst...@g mail.comwrote:
          Because math.pow returns a float; 100 ** 155 won't fit in a float.
          >
          Sure that is the reason.
          May I rephrase, my question:
          Why not returning another type as long as we can calculate it?
          After all, math module is likely to be used on large numbers as well.
          The math module is intended to replicate the functionality found in
          math.h in the C Standard Library; that's it, no more, no less. There
          are other libraries if you want more-than-float precision.

          Comment

          • Marc 'BlackJack' Rintsch

            #6
            Re: why does math.pow yields OverflowError (while python itself cancalculate that large number)

            On Thu, 23 Oct 2008 02:21:36 -0700, Tzury Bar Yochay wrote:
            >>>eval(('100*' * 155)[:-1])
            100000000000000 000000000000000 000000000000000 00000000000000
            000000000000000 000000000000000 000000000000000 00000000000000
            000000000000000 000000000000000 000000000000000 00000000000000
            000000000000000 000000000000000 000000000000000 00000000000000
            000000000000000 000000000000000 000000000000000 00000000000000
            000000000000000 0L
            >>>>
            This can be written more straigth forward as ``100**155`` or
            ``pow(100, 155)``. No need for `eval()`\ing a string.

            Ciao,
            Marc 'BlackJack' Rintsch

            Comment

            Working...