1. ## 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;

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. Turn on your compiler warnings. 'findpi' is used uninitialized.

Quzah.

3. I see that but does that matter that it isn't. Not relatively speaking, but for this program.

4. 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.

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/

5. Yea I guess you are completely correct. I cant believe I overlooked the algebra problem. :P

6. Originally Posted by xniinja
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).

7. But you never know. Perhaps the programming exercise was to teach iterative methods to arrive at an approximate or ever improving result.

8. Originally Posted by nonoob
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.