math.pow(x,y)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fl1p-fl0p

    #1

    math.pow(x,y)

    import math
    math.pow(345643 23, 456356)

    will give math range error.

    how can i force python to process huge integers without math range
    error? Any modules i can use possibly?

  • Wojciech Mu³a

    #2
    Re: math.pow(x,y)

    fl1p-fl0p wrote:[color=blue]
    > import math
    > math.pow(345643 23, 456356)
    >
    > will give math range error.
    >
    > how can i force python to process huge integers without math range
    > error? Any modules i can use possibly?[/color]

    You have to use operator **, i.e. 34564323**45635 6

    Comment

    • Felipe Almeida Lessa

      #3
      Re: math.pow(x,y)

      Em Dom, 2006-06-11 às 11:19 -0700, fl1p-fl0p escreveu:[color=blue]
      > import math
      > math.pow(345643 23, 456356)
      >
      > will give math range error.
      >
      > how can i force python to process huge integers without math range
      > error? Any modules i can use possibly?[/color]

      34564323**45635 6 ?

      --
      Felipe.

      Comment

      • Gary Herron

        #4
        Re: math.pow(x,y)

        Wojciech Muła wrote:[color=blue]
        > fl1p-fl0p wrote:
        >[color=green]
        >> import math
        >> math.pow(345643 23, 456356)
        >>
        >> will give math range error.
        >>
        >> how can i force python to process huge integers without math range
        >> error? Any modules i can use possibly?
        >>[/color]
        >
        > You have to use operator **, i.e. 34564323**45635 6
        >[/color]
        That's not very practical. That computation will produce a value with
        more than 3.4 million digits. (That is, log10(34564323) *456356 =
        3440298.) Python will attempt this, but I was not patient enough to see
        if it could calculate an answer today (or even this week).

        I doubt that you really *want* all 3.4 million digits. So what is it you
        really want? A scientific or engineering result as a floating point
        number accurate to some reasonable number of digits? That integer value
        modulo some other integer (as used in various cryptology schemes)?

        Gary Herron

        Comment

        • Raymond L. Buvel

          #5
          Re: math.pow(x,y)

          Felipe Almeida Lessa wrote:[color=blue]
          > Em Dom, 2006-06-11 às 11:19 -0700, fl1p-fl0p escreveu:[color=green]
          >> import math
          >> math.pow(345643 23, 456356)
          >>
          >> will give math range error.
          >>
          >> how can i force python to process huge integers without math range
          >> error? Any modules i can use possibly?[/color]
          >
          > 34564323**45635 6 ?
          >[/color]

          I just tried this and it is taking an extremely long time even on a fast
          machine with 4 Gb of RAM. Killed it after a couple of minutes. This
          sort of calculation can be done with extended precision floating point
          (as long as you don't need an exact answer). For example (using
          defaults on a 64-bit machine),
          [color=blue][color=green][color=darkred]
          >>> from clnum import mpf
          >>> mpf(34564323)** 456356[/color][/color][/color]
          mpf('1.39518106 833639480699862 472257296396643 e3440298',36)

          compute time is about 160 microseconds.

          For more information see



          For calculations involving large powers, you may still be better off
          using logarithms.

          Comment

          • K.S.Sreeram

            #6
            Re: math.pow(x,y)

            Raymond L. Buvel wrote:[color=blue]
            > I just tried this and it is taking an extremely long time even on a fast
            > machine with 4 Gb of RAM. Killed it after a couple of minutes.[/color]

            Thats odd.
            34564323**45635 6 completed on my laptop in 28 seconds.
            [Python 2.4.3, Celeron-M 1.3GHz, WinXP], and max memory consumption
            during the whole process was about 11megs.

            What python version are you using?

            Regards
            Sreeram


            -----BEGIN PGP SIGNATURE-----
            Version: GnuPG v1.4.2.2 (MingW32)
            Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

            iD8DBQFEjHMhrgn 0plK5qqURApduAK C5GygYWWtQYTvEy ljR9nxW9n+rpACd F2qn
            vk5u0zzWoRnfLLk iangJd90=
            =zh3g
            -----END PGP SIGNATURE-----

            Comment

            • K.S.Sreeram

              #7
              Re: math.pow(x,y)

              Raymond L. Buvel wrote:[color=blue]
              > I just tried this and it is taking an extremely long time even on a fast
              > machine with 4 Gb of RAM. Killed it after a couple of minutes.[/color]

              You probably tried printing the value.

              a = 34564323**45635 6 (takes just 28 seconds)
              whereas
              b = str(a) takes forever!

              Regards
              Sreeram


              -----BEGIN PGP SIGNATURE-----
              Version: GnuPG v1.4.2.2 (MingW32)
              Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

              iD8DBQFEjHWxrgn 0plK5qqURAiJGAJ 9XYzqDi93YahIvV xqeCs/Ex9ikIQCgnSwu
              Eq0mIHCk8o3nvWO llFFXDag=
              =BFDh
              -----END PGP SIGNATURE-----

              Comment

              • Raymond L. Buvel

                #8
                Re: math.pow(x,y)

                K.S.Sreeram wrote:[color=blue]
                > Raymond L. Buvel wrote:[color=green]
                >> I just tried this and it is taking an extremely long time even on a fast
                >> machine with 4 Gb of RAM. Killed it after a couple of minutes.[/color]
                >
                > Thats odd.
                > 34564323**45635 6 completed on my laptop in 28 seconds.
                > [Python 2.4.3, Celeron-M 1.3GHz, WinXP], and max memory consumption
                > during the whole process was about 11megs.
                >
                > What python version are you using?
                >
                > Regards
                > Sreeram
                >[/color]

                Sorry, I tripped over the display problem that Tim Peters has repeatedly
                explained on this news group (decimal algorithm is quadratic in the
                number of digits) :-(

                On a 64-bit 2.2 GHz Opteron running Debian stable, Python 2.3 took 26
                seconds and Python 2.4 took 11 seconds.

                Comment

                • casevh@comcast.net

                  #9
                  Re: math.pow(x,y)


                  K.S.Sreeram wrote:[color=blue]
                  > Raymond L. Buvel wrote:[color=green]
                  > > I just tried this and it is taking an extremely long time even on a fast
                  > > machine with 4 Gb of RAM. Killed it after a couple of minutes.[/color]
                  >
                  > You probably tried printing the value.
                  >
                  > a = 34564323**45635 6 (takes just 28 seconds)
                  > whereas
                  > b = str(a) takes forever!
                  >
                  > Regards
                  > Sreeram
                  >[/color]

                  If you really want all 3,440,299 digits, use the DecInt module..



                  Using Python 2.4.3 on a 1.4Ghz Celeron, a=DecInt(345643 23) ** 456356
                  takes 20 seconds. astr=str(a) takes just over 1 second.

                  With DecInt and GMPY, the running times are 6.9 seconds and 0.4 seconds
                  respectively.

                  casevh

                  Comment

                  Working...