Thread: What do I use for Pi?

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    6

    What do I use for Pi?

    I am working on a homework assignment and I have to use Pi as part of a formula. Is there something built in to stdio for Pi or should I just make a float variable out to 4 or 5 decimals and call it a night?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Just define a constant... #define pi = 3.1415 ... to whatever depth you need.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Include the "math.h" header file and use the symbol M_PI.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    6
    I used this... good?

    Code:
    const float PI=3.141593

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ssnapier View Post
    I used this... good?

    Code:
    const float PI-3.141593
    Ummm no ... you don't want a - sign in there, you want an = sign. Otherwise, no problem.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    6
    I realized the typo as soon as I posted it, and edited... I also used the semicolon at the end, but thanks!

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by itCbitC View Post
    Include the "math.h" header file and use the symbol M_PI.
    While that works on a lot of compilers, it's actually isn't standard.

    Yes, I was also amazed upon learning it.

    The old-school way of getting pi is to compute it at runtime by taking the arctan of 1 then multiplying by 4:

    Code:
    const double pi = 4.0 * atan(1.0);
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed