Thread: Pi check

  1. #1
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231

    Pi check

    hello everyone. So here is the story behind this program. I was in math class and I was using my calculator to see what I could divide 360 by to get pi (as close as I can get). But it was taking forever so I made a program that should do it for me but the while loop never stops. That is my problem so can you help.

    Code:
    #include <stdio.h>
    
    main()
    {
          
          double getpi = 115;
          double findpi;
          double pi = 3.141592;
          
          
          printf("finding please wait...");
          
    
          
         while (findpi != pi)
          {
                printf("\ngetpi = %f findpi = %f",getpi,findpi);
                
                getpi = getpi - .000001;
                
                findpi = 360/getpi;
                printf(" findpicheck %f",findpi);
          }
    
          printf("found your pie :D findpi = %f getpi = %f",findpi,getpi);
          
          getch();
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Turn on your compiler warnings. 'findpi' is used uninitialized.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231
    I see that but does that matter that it isn't. Not relatively speaking, but for this program.

  4. #4
    THANK YOU KINDLY SIR Phenax's Avatar
    Join Date
    Mar 2011
    Posts
    74
    This can be solved using simple algebra much easier than writing a program, especially if you have access to a calculator.
    360/x = 3.141592
    360 = 3.141592x
    x = 360/3.14592
    x = 114.433933...

    Initialize findpi to something before you compare it, because you are comparing something that's uninitialized which isn't good. It could make for an unlikely case where findpi is equal to pi and the while loop never executes (although extremely unlikely) Also, %lf for doubles, %f for floats. Also, main should be an int, and return 0 usually.

    Answer to your actual question: read this (doubles are just an increased precision float):
    http://en.wikipedia.org/wiki/Binary_...ions_in_binary
    http://www.cprogramming.com/tutorial...ing_point.html

    You could use an epsilon, or perhaps a library/compiler's implementation of floating-points using decimal representations http://gcc.gnu.org/onlinedocs/gcc/De...#Decimal-Float || http://gmplib.org/
    Last edited by Phenax; 03-08-2011 at 09:27 PM.
    Quote Originally Posted by Plato
    Never discourage anyone...who continually makes progress, no matter how slow.

  5. #5
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231
    Yea I guess you are completely correct. I cant believe I overlooked the algebra problem. :P

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by xniinja View Post
    Yea I guess you are completely correct. I cant believe I overlooked the algebra problem. :P
    You're not alone.

    A significant percentage of people who ask for help in these forums overlook simple algebraic solutions that give precise (or even exact) results quickly. Instead they persist in using expensive brute-force computational methods to find an inaccurate solution slowly, on the basis that algebra is "too hard" (i.e. requires understanding).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    But you never know. Perhaps the programming exercise was to teach iterative methods to arrive at an approximate or ever improving result.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by nonoob View Post
    But you never know. Perhaps the programming exercise was to teach iterative methods to arrive at an approximate or ever improving result.
    Read the first post. He was playing with his calculator, and decided to see if he could do the same thing in code.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. How can i check a directory for certain files?
    By patrioticpar883 in forum C++ Programming
    Replies: 13
    Last Post: 02-01-2008, 05:27 PM
  3. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 07:07 PM
  4. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 01:52 AM
  5. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2002, 11:21 PM