Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Python large numbers

  1. #1
    Join Date
    Jun 2010
    Location
    /home/TX
    Beans
    225
    Distro
    Ubuntu 12.04 Precise Pangolin

    Python large numbers

    Hello Ubuntu Forums community,

    I am making a python program that requires the math.pow function. I need it to go up to 2^31 (2 to the 31st power) and preferably even farther. When I run the program in the terminal, it says "OverflowError: math range error"

    Any way to fix this?
    Thanks in advance.
    "The secret to creativity is knowing how to hide your sources." -Albert Einstein

    Ubuntu User #32977; Linux User #528876

  2. #2
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: Python large numbers

    What are you doing, exactly? math.pow(2, 32) doesn't raise an exception for me.
    「明後日の夕方には帰ってるからね。」


  3. #3
    Join Date
    Jul 2007
    Location
    Austin, TX
    Beans
    Hidden!
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Python large numbers

    Python supports arbitrary precision numbers out of the box. What version are you using? And can you post the code that seems to be troublesome?

  4. #4
    Join Date
    Dec 2004
    Location
    Manchester
    Beans
    2,086
    Distro
    Ubuntu Mate 15.10 Wily Werewolf

    Re: Python large numbers

    Quote Originally Posted by Queue29 View Post
    Python supports arbitrary precision numbers out of the box. What version are you using? And can you post the code that seems to be troublesome?
    thats not right. its just long ints that have infinite range.

    http://docs.python.org/library/stdty...t-long-complex

    Code:
    >>> math.sin(pow(2,1000))
    -0.15920170308624243
    >>> math.sin(pow(2,10000))
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    OverflowError: long int too large to convert to float

  5. #5
    Join Date
    Sep 2007
    Location
    Christchurch, New Zealand
    Beans
    1,328
    Distro
    Ubuntu

    Re: Python large numbers

    Quote Originally Posted by ssam View Post
    thats not right. its just long ints that have infinite range.

    http://docs.python.org/library/stdty...t-long-complex

    Code:
    >>> math.sin(pow(2,1000))
    -0.15920170308624243
    >>> math.sin(pow(2,10000))
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    OverflowError: long int too large to convert to float
    I thought it would calculate pow as floating point number anyway... what on Earth is Python doing... 10000 multiplications?

    Anyway as far as I can tell most languages use floating point as provided by their processor which is usually IEEE-754. I think it give about 16 digits of precision and exponent up to something like 300 in decimal.

  6. #6
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: Python large numbers

    Quote Originally Posted by worksofcraft View Post
    I thought it would calculate pow as floating point number anyway... what on Earth is Python doing... 10000 multiplications?
    pow is integer, math.pow is float.

    Code:
    >>> pow(2, 1000)
    10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376L
    >>> math.pow(2, 1000)
    1.0715086071862673e+301
    Incredible as it may sound, having integer powers is actually very useful. When doing arithmetic for example.
    「明後日の夕方には帰ってるからね。」


  7. #7
    Join Date
    Jun 2010
    Location
    /home/TX
    Beans
    225
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Python large numbers

    Thanks Bachstelze. I didn't know there was just pow() and that there was a difference between pow() and math.pow().

    Marking thread as solved.
    "The secret to creativity is knowing how to hide your sources." -Albert Einstein

    Ubuntu User #32977; Linux User #528876

  8. #8
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: Python large numbers

    Quote Originally Posted by kroq-gar78 View Post
    Thanks Bachstelze. I didn't know there was just pow() and that there was a difference between pow() and math.pow().

    Marking thread as solved.
    Yup, this is why you should never do

    Code:
    from math import *
    「明後日の夕方には帰ってるからね。」


  9. #9
    Join Date
    Aug 2006
    Location
    60°27'48"N 24°48'18"E
    Beans
    3,458

    Re: Python large numbers

    Quote Originally Posted by Bachstelze View Post
    pow is integer, math.pow is float.
    Well, unless you give pow float parameters.

    That math.pow is specifically a float is interesting; could be one of these rare cases where I might actually agree with worksofcraft's objections to Python's handling of number types.
    LambdaGrok. | #ubuntu-programming on FreeNode

  10. #10
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: Python large numbers

    Quote Originally Posted by CptPicard View Post
    Well, unless you give pow float parameters.

    That math.pow is specifically a float is interesting; could be one of these rare cases where I might actually agree with worksofcraft's objections to Python's handling of number types.
    It makes sense to me. As I said, in some applications you really want full-precision exponentiation of integers (e.g. when doing RSA crypto), but on the other hand, sometimes you don't need the precision, and having long integers is just a bother.

    Having both is a nice thing about Python IMO.
    「明後日の夕方には帰ってるからね。」


Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •