Thread: Pi

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    28

    Post Pi

    hey i need help i have just started my programming class and need to make a program that calculates pi. can anyone help me?
    it would be real nice.

  2. #2
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Well, here is a hint...

    4 * atan(1)

  3. #3
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Google returned this webpage: http://www.cygnus-software.com/misc/pidigits.htm Try doing a search. Write some code, post any problems you're having back here and people will be happy to help you.

  4. #4
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    Hi loki

    First of all I'm sorry I havn'e greeted you yet, I'm trying to do that...so welcome heh.

    Secondly, what are you having problems with? Is it the algorithm that you're not getting? Generally people aren't going to write it all for you, but they're very happy to help you understand and find errors. What have you got so far? Where are you? Is it your code where's there's trouble?

    Let us know....

    Good Luck,
    Kermi3
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  5. #5
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    Search google for "compute pi" , plenty of stuff comes up.

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    28
    thanks all but the thing is i think im going for something less complicated than what u guys are think. i read the web page and it has a lot of stuff i dont understand like tan and cos. the reason i dont undertand that is because im only a ninth grader and im in geometry not trigonometry. anyway what i want to know is if its true that pi is a ratio of circumference and something else, and if it is what are the two thing and is their a way i can generalize them. so that i dont have to write to specific things to make the ratio. another problem i was having was finding a way to display all the numbers on the screen.

    one more thing kinda off the subject, are there any free compilers that i can get off teh net that are kinda like borlands turbo c++ that use console and not DOS. i think thats what i mean, im not to computer savy,i think thats how u spell it.

    thanks for all the help so far!

  7. #7
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Like I said, pi is 4 times atan(1). Now the atan of 1 is tricky to compute, which is where all the algos come in. But the math library of you compiler can do that, too, just not to amazing precision.

    Just #include <cmath> at the top, and then cout << 4*atan((long double)1). By casting the 1 to a long double you ensure that the call is resolved to the highest precision the library function supports.

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    11
    Carlos, I got the algorithm. I'll show it to you tommorow, it's really simple. Also, I figured out how to implement it, so no worries. It's late anyway, nother 30 minutes won't matter.

    Everyone go home, nothing to post here
    I'm not here. This isn't happening

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I fully understand the desire to do something superceding your desire to learn a concept necessary in order to achieve the desired result. However, tangents and cosines are fairly basic mathematical concepts. You are going to need to take a few more math classes. I know that isn't what you want to hear but that is the unfortunate truth.

  10. #10
    Registered User
    Join Date
    Jun 2002
    Posts
    82
    loki, if you want a direct answer to your question, pi can be expressed as the ratio of the circumference of a circle to its diameter; this comes from rearranging the well-known formula
    Code:
    circumference = 2 * PI * radius;
    therefore
    Code:
    PI = circumference / (2 * radius);
    // or
    PI = circumference / diameter;
    However, if you wanted to compute pi using this ratio you'd be screwed because to accurately compute the circumference of a circle you need to know pi. You're much better off (from a programming point of view) using Imperito's solution of
    Code:
    const double pi = 4.0 * atan(1.0);
    and just use that computed value of pi for all of your calculations.

    If you need a mathematical definition for why this works, here goes (I'll try to keep in mind that you haven't had trig yet). If nothing else this will help you when you do get to trig:

    Consider a right triangle (one angle is 90 degrees, the others are arbitrary but must add up to 90 degrees themselves). The tangent of an angle can be defined using this triangle; set one of the other angles equal to the angle that you are trying to calculate, the other to (90 - angle). The three sides of the triangle can be referred to as the adjacent, the opposite, and the hypotenuse:
    Code:
             |\
             | \
             |  \  hypotenuse
    opposite |   \
             |    \
             |_____\<-- angle of interest
            adjacent
    As you can see, the hypotenuse is the longest side and is across the triangle from the right angle, while the opposite is across from the angle you are interested in, and the adjacent is the third side. The tangent function can be defined as the ratio of the lengths of the opposite and the adjacent. Now, a 360 degree angle can also be referred to as 2*pi radians (take my word for it if you aren't familiar with radians, they're another way to talk about angles and I don't have the time to get into it here.) You can't have a right triangle with a 360-degree angle in it, but you can have a right triangle with a 45-degree angle in it (45 = 360 / 8). Since the angles have to add up to 180, that means that the two non-right angles must both be 45 degrees, which means that the opposite and the adjacent angles must be equal, so the tangent of 45 degrees is the ratio of two equal quantities, which is 1.

    Still with me? Good. Now, we have the expression tangent(45 degrees) = 1. Since 360 degrees = 2*pi, 45 degrees = pi / 4. We now have tangent(pi / 4) = 1. Here is where the atan function comes in; atan(x) returns "the angle for which the tangent is x". Since we are interested in the angle pi / 4, we take atan(1), and to get pi we multiply by 4. Thus, pi = 4 * atan(1).

    A bit long-winded, but I think it will help you considerably when you start studying these things in school.
    Claus Hetzer
    Compiler: Borland 5.5 (on Windows)
    Solaris CC (on Unix)
    Known Languages: C++, MATLAB, Perl, Java

  11. #11
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    oops, nm

    *edited out*
    I came up with a cool phrase to put down here, but i forgot it...

  12. #12
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Impressive, Geophyzzer!

    Nice post Geophyzzer!!! I'm going to print that out and study it when I have a few minuites to absorb it.

  13. #13
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378
    Originally posted by geophyzzer
    loki, if you want a direct answer to your question, pi can be expressed as the ratio of the circumference of a circle to its diameter; this comes from rearranging the well-known formula
    Code:
    circumference = 2 * PI * radius;
    therefore
    Code:
    PI = circumference / (2 * radius);
    // or
    PI = circumference / diameter;
    However, if you wanted to compute pi using this ratio you'd be screwed because to accurately compute the circumference of a circle you need to know pi. You're much better off (from a programming point of view) using Imperito's solution of
    Code:
    const double pi = 4.0 * atan(1.0);
    and just use that computed value of pi for all of your calculations.

    If you need a mathematical definition for why this works, here goes (I'll try to keep in mind that you haven't had trig yet). If nothing else this will help you when you do get to trig:

    Consider a right triangle (one angle is 90 degrees, the others are arbitrary but must add up to 90 degrees themselves). The tangent of an angle can be defined using this triangle; set one of the other angles equal to the angle that you are trying to calculate, the other to (90 - angle). The three sides of the triangle can be referred to as the adjacent, the opposite, and the hypotenuse:
    Code:
             |\
             | \
             |  \  hypotenuse
    opposite |   \
             |    \
             |_____\<-- angle of interest
            adjacent
    As you can see, the hypotenuse is the longest side and is across the triangle from the right angle, while the opposite is across from the angle you are interested in, and the adjacent is the third side. The tangent function can be defined as the ratio of the lengths of the opposite and the adjacent. Now, a 360 degree angle can also be referred to as 2*pi radians (take my word for it if you aren't familiar with radians, they're another way to talk about angles and I don't have the time to get into it here.) You can't have a right triangle with a 360-degree angle in it, but you can have a right triangle with a 45-degree angle in it (45 = 360 / 8). Since the angles have to add up to 180, that means that the two non-right angles must both be 45 degrees, which means that the opposite and the adjacent angles must be equal, so the tangent of 45 degrees is the ratio of two equal quantities, which is 1.

    Still with me? Good. Now, we have the expression tangent(45 degrees) = 1. Since 360 degrees = 2*pi, 45 degrees = pi / 4. We now have tangent(pi / 4) = 1. Here is where the atan function comes in; atan(x) returns "the angle for which the tangent is x". Since we are interested in the angle pi / 4, we take atan(1), and to get pi we multiply by 4. Thus, pi = 4 * atan(1).

    A bit long-winded, but I think it will help you considerably when you start studying these things in school.
    Don't forget to:

    #define PI 3.14159
    //place this just below your #inlcude<iostream>
    // another hint
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  14. #14
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Or there's always cout >> "3.14159" - I think you get the point.

  15. #15
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Sorry sean but isn't it like this...

    Originally posted by Sean
    Or there's always cout >> "3.14159" - I think you get the po
    int.
    Isn't this the correct way, sean?

    cout << "3.14159";

    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pi - Hm, somethign is not right here.
    By MadnessRed in forum C++ Programming
    Replies: 8
    Last Post: 09-12-2008, 01:07 PM
  2. PI Equation in C
    By wallysworld in forum C Programming
    Replies: 13
    Last Post: 10-23-2006, 08:12 PM
  3. Pi Calculation
    By EvilGuru in forum C Programming
    Replies: 2
    Last Post: 05-02-2005, 04:25 AM
  4. Pi and the standard library
    By FOOTOO in forum C Programming
    Replies: 7
    Last Post: 04-15-2005, 11:23 AM
  5. C for PI
    By Lynux-Penguin in forum C Programming
    Replies: 13
    Last Post: 04-28-2002, 07:37 PM