1.#INF00 problem while trying to calculate pi

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • valenwalen
    New Member
    • Sep 2013
    • 3

    1.#INF00 problem while trying to calculate pi

    I did my program to calculate pi but for some reason, every single time I run the program, screen says "1.#INF00":

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main (void)
    {
        float a;
        int i, x, b, c, d, e, f;
        x=3;
        a=1.0;
        b=1;
        c=4;
        d=5;
        e=1;
        f=3;
        for (i=1; i<=9999; i=i+1)
        {
            a=a*(x+((e*b)/(c*d*f)));
            x=x+5;
            b=b+2;
            c=c+3;
            d=d+3;
            e=e+1;
        }
        printf("%f", a);
        return 0;
    }
    Last edited by Banfa; Sep 20 '13, 08:31 AM. Reason: Added [code] [/code] tags round the code please use them yourself in the future
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    1.#INF00 is an IEEE floating point exception. There are several kinds of these. This one is a positive infinity and it means your float variable is not in floating point format.

    That suggests your loop doesn't work.

    I suggest you set a breakpoint at the start of your loop and step the loop to see if the variable a is still in floating point format. If you see a decimal number in your debugger, then a is still in float format. Otherwise, you will see the 1.#INF00.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      I'm not sure where you got the formula from but it is very wrong

      Consider ((e*b)/(c*d*f))

      All variables are integers so arithmetic is integer based and they have the following characteristics

      Initial
      Variable Value Increment
      b 1 2
      c 4 3
      d 5 3
      e 1 1
      f 3 0

      Then for these values
      c > b is always true
      d > e is always true

      Therefore (e*b) < (c*d*f) is always true* and since integer division is in operation ((e*b)/(c*d*f)) is always 0.


      * Actually this isn't quite true because towards the end of the iteration (c*d*f) actually wraps (gets bigger than an integer can hold) and on my machine this results in a negative number but the absolute value of this number is always bigger than (e*b) but that is a fluke.

      Since ((e*b)/(c*d*f)) is always 0 your calculation becaomes

      Code:
          x=3;
          a=1.0;
          
          for (i=1; i<=9999; i=i+1)
          {
              a=a*x;
              x=x+5;
          }
      x increments by 5 every time it is getting larger all the time a is getting exponentially larger all the time. It calculates

      3 * 8 * 13 * 18 * ... * 49983 * 49988 * 49993 * 49998

      This is neither PI nor does it fit into a float.


      In fact a takes the value 1.#INF on the 130th iteration of the loop, on the 129th iteration it had the value 7.01924e306, at this point x had a value of 648.
      Last edited by Banfa; Sep 20 '13, 09:04 AM. Reason: Added last sentence

      Comment

      • valenwalen
        New Member
        • Sep 2013
        • 3

        #4
        How could I change my program in order to obtain an aproximation to pi? because I need to do 10 similar programs and I got most of the ideas from the webpage http://mathworld.wolfram.com/PiFormulas.html, but so far I've only been able to program Leibniz's

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Well you need to make sure you get the logic of your implementation correct. It is not clear to me which of those 130+ formulas you where trying to implement.

          Comment

          • valenwalen
            New Member
            • Sep 2013
            • 3

            #6
            You are absolutely right, my apologies. I was trying to do Beeler's, Sloane's and Wallis'. The program I previously posted was an attempt of Beeler's.

            Comment

            Working...