Thread: Standard PI constant

  1. #1
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879

    Standard PI constant

    After googling for a while, I found this:
    However, it should be noted that the math constants are actually not part of the C++ standard. A standard's conforming way to get pi is to use an expression such as const double PI(4.0 * std::atan2(1.0, 1.0));.
    Is this true, that there is no predefined PI constant that is part of the C++ standard?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    I think the M_PI macro in <cmath> is standard, but I'm not sure.

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ahhh, just what I was thinking of.. that's actually what I was googling and boardsearching for, lol! But I'd still like to hear from the gurus if this is really standard, or if it's an unofficial extension to the <cmath> header.

    [self-hijack]
    I like the new sig, it has a mystic aura about it
    [/self-hijack]
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    My only doubt would be that M_PI may may be one the the "math constants" that was referred to in your quote. But as far as I can remember, it has been there on every compiler I have used(maybe because they are part of C's standard but not C++?). At any rate, I think the M_ macros are pretty surefire.

  5. #5
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    "Don't complain. Everything is perfect here and you have no right."

    lol, that's so awesome

  6. #6
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    i think the floating point version of ( 22 / 7 ) might also be another way

    Code:
    float pi = 22/7 ;
    Last edited by The Brain; 09-20-2004 at 11:00 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Get an arbitrary-precision floating point number class and then just load in the first few thousand digits of pi... Who needs standards.

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    >i think the floating point version of ( 22 / 7 ) might also be another way

    22/7 is kind of like pseudo-PI. If you type that into a calculator, you'll get 3.142857, and the first few digits of PI are 3.1415927. It's close, but for many(probably most) situations, not close enough.

  9. #9
    ---
    Join Date
    May 2004
    Posts
    1,379
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        float pi = 22/7;
        printf("%f",pi);
        return 0;
    }
    i got 3.000000

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        float pi = 22;
        pi /= 7;
        printf("%f",pi);
        return 0;
    }
    i got 3.142857
    Last edited by sand_man; 09-21-2004 at 12:08 AM.

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Thats because you are doing integer arithmetic on the first version. Change it to:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        float pi = 22.f/7;
        printf("%f",pi);
        return 0;
    }

  11. #11
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Hunter2, it's true.

    There is no M_PI constant defined in the <cmath> header according to the standard. The only macro defined in that header is HUGE_VAL.


    EDIT:
    What would be the advantage of using the atan2 function and a multiplication to calculate pi?
    I'd just use acos to calculate pi
    Code:
    const float pi = acos(-1);
    Although I really would prefer
    Code:
    const float pi = 3.141592...;
    Last edited by Sang-drax; 09-21-2004 at 02:36 AM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  12. #12
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Just define it manually. Nobody except for a very limited few cases needs more than, say, the first 25-30 digits of PI anyway.

    http://theory.cs.iitm.ernet.in/~arvindn/pi/

    That's the first 1,000 digits after the decimal point. If you're really that desperate, just use a const float with those 1,000 digits. And prepare for muchos accuracy.

  13. #13
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    it is a pity the Pi is not a integer

    blow me ... ...

  14. #14
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>Just define it manually. Nobody except for a very limited few cases needs more than, say, the first 25-30 digits of PI anyway.
    I've actually memorized PI to about the 36'th digit or so, but where possible I prefer to not to rely on limited precision like that (whether the added precision is needed or not). And to hardcode 1000+ digits would add another 1kb to my source file, and if Joe Shmo decides to modify one digit then it's all off Problem with 22/7 is that while it's possibly very precise, it's certainly not accurate.

    Sang-Drax: After googling on M_PI (much more successful than PI C++ standard cmath ), I came to the same conclusion (everywhere said it was nonstandard).
    >>What would be the advantage of using the atan2 function and a multiplication to calculate pi?
    I'm not sure if there is one. Possibly the algorithm for finding acos is less accurate/precise than for atan? Anyway, the *4 multiplication would mean a potential loss of (I think) a digit or two of accuracy. I also noticed that in Visual Basic there's no acos/asin functions, you must calculate them by using atn (atan) and manipulating the result. Does anyone know more about this?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    355 / 113 is a better approximation.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Standard efficiency
    By Jorl17 in forum C Programming
    Replies: 3
    Last Post: 06-18-2009, 11:48 AM
  2. array initialization & C standard
    By jim mcnamara in forum C Programming
    Replies: 2
    Last Post: 09-12-2008, 03:25 PM
  3. Bug in iterator comparison in C++ standard?
    By steev in forum C++ Programming
    Replies: 14
    Last Post: 07-12-2008, 12:02 AM
  4. C/C++ standard
    By confuted in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 07-06-2003, 03:22 AM
  5. standard language, standard compiler?
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 09-03-2001, 04:21 AM