Thread: pow: OVERFLOW error

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    14

    pow: OVERFLOW error

    Hi, when i was running my excutable file, i got this error

    pow: OVERFLOW error
    here is the power function i used to calculate the varible:

    Code:
    int i;
    			for(i = 1; i <= approximation; i++){
    				pi += pow(12, 0.5)*pow(-1, i+1)/((2*i - 1)*(pow(3, i-1)));
    			}
    any ideas?

    the error only occur when i pass a large number (over 1000) for approximation, and it runs for a few seconds and in the end it still will print out the correct answer, im just wondering should this be a big deal?
    Last edited by skyt; 11-04-2008 at 02:46 PM. Reason: more info

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I would imagine an overflow took place. What value is approximation?

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    14
    Quote Originally Posted by master5001 View Post
    I would imagine an overflow took place. What value is approximation?
    when i enter 100 or 200 etc the error wouldnt occur, but when i use anything over 1000, it will occur.

    however, for the other method of mine,

    Code:
    int i;
    			for(i = 1; i <= approximation; i++){
    				pi += 4*pow(-1, i+1)/(2*i-1);
    			}
    i passed 40million for approximation and no such error occured

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I suspect that is caused by the fact that pow(3, n) is much more "growing" than pow(-1, n).

    Do you understand what "overflow" and "pow" actually do?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    14
    Quote Originally Posted by matsp View Post
    I suspect that is caused by the fact that pow(3, n) is much more "growing" than pow(-1, n).

    Do you understand what "overflow" and "pow" actually do?

    --
    Mats
    yea, does it mean the numbers are getting too large for the memory to handle via the power function? is there anything i can do to fix it?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Not for "memory to handle", but rather "for the math processing unit" - it is essentially the same as the E you get on a regular calculator if your result is too large (or you divide by zero or some such).

    There is really no easy solution to the problem, other than using a different method - on the other hand, I suspect by the time you are even CLOSE to overflow, the calculation isn't making any changes to the result (because square root of 12 divided by 2 * i-1 * 3 to the power of i will be something like 1E-300 in the calculation leading up to the overflow, and with a double precision float, you only get about 15 or so digits precision, and PI is around 3, so to change the 15th decimal place, the number needs to be bigger than 1E-16 - so you have about 284 too many zeros at the beginnin of the number.

    Once your sqrt(12)/X calculation becomes smaller than about 1E-20, you can stop, because it's not going to change the result. You can check it if you like, by printing the PI value and the sqrt(12)/X value in parallel. Once the number gets small enough, it's not going to make any changes to PI at all - no matter how many times you do the calculation.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    14
    Quote Originally Posted by matsp View Post
    Not for "memory to handle", but rather "for the math processing unit" - it is essentially the same as the E you get on a regular calculator if your result is too large (or you divide by zero or some such).

    There is really no easy solution to the problem, other than using a different method - on the other hand, I suspect by the time you are even CLOSE to overflow, the calculation isn't making any changes to the result (because square root of 12 divided by 2 * i-1 * 3 to the power of i will be something like 1E-300 in the calculation leading up to the overflow, and with a double precision float, you only get about 15 or so digits precision, and PI is around 3, so to change the 15th decimal place, the number needs to be bigger than 1E-16 - so you have about 284 too many zeros at the beginnin of the number.

    Once your sqrt(12)/X calculation becomes smaller than about 1E-20, you can stop, because it's not going to change the result. You can check it if you like, by printing the PI value and the sqrt(12)/X value in parallel. Once the number gets small enough, it's not going to make any changes to PI at all - no matter how many times you do the calculation.

    --
    Mats

    that makes perfect sense, in the instruction i will just have to indicate that larger approximation will not help make pi more accurate when it gets too big. i really should have looked into the series itself before i post this problem, it's purely a math problem rather than a programming problem. You are great!

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by skyt View Post
    that makes perfect sense, in the instruction i will just have to indicate that larger approximation will not help make pi more accurate when it gets too big. i really should have looked into the series itself before i post this problem, it's purely a math problem rather than a programming problem. You are great!
    No, if it was PURELY a math problem, then you have infinite (or relatively huge) precision. Computers limit the precision for the benefit of small size (a double takes up 8 bytes). You can get libraries (or write your own) that does "nearly infinite precission" math, e.g. GMP - limited only by available memory in the machine (or memory available to the process if the machine has HUGE amount of memory). You could quite easily use this to calculate several million digits of PI or some such - but of course, some others have been doing that already... :-)

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM