Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ pi

  1. Jul 1, 2004 #1
    How would I go about defining pi (and using) as 3.1415926535897932384626433832795?

    I've tried using double, long double and #define pi 3.1415926535897932384626433832795 but it, instead, takes 3.14159.
    Is there any way to get it more accurate?
  2. jcsd
  3. Jul 1, 2004 #2


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    I believe the default precision for printf and cout is to only display 6 digits; try changing the precision in your display routines.

    (Oh, and I see M_PI in my math.h include file; that might be standard)
  4. Jul 1, 2004 #3


    User Avatar
    Staff Emeritus

    If you want to hold such a precise number your going to have to write your own function. A long double will hold about 18 digits with an error of +- 2e-15 depending on the system your using. cout will be less precise with a define macro.
  5. Jul 1, 2004 #4
    Many thanks. I didn't even know the precision thing exist(fairly new to all this) but a quick google search sorted all that out. Thanks again Hurkyl.

    edit:dduardo, I figured I don't really need it to be so accurate, so I settled with just 15 digits. Thanks for your reply.
    Last edited: Jul 1, 2004
  6. Jul 1, 2004 #5


    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

Share this great discussion with others via Reddit, Google+, Twitter, or Facebook