How would I go about defining pi in C++?

  • Thread starter #define
  • Start date
  • #1
3
0

Main Question or Discussion Point

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?
 

Answers and Replies

  • #2
Hurkyl
Staff Emeritus
Science Advisor
Gold Member
14,916
19
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)
 
  • #3
dduardo
Staff Emeritus
1,890
3
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.
 
  • #4
3
0
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:
  • #5
robphy
Science Advisor
Homework Helper
Insights Author
Gold Member
5,632
889
4.0*atan(1.0)
 

Related Threads on How would I go about defining pi in C++?

Replies
6
Views
2K
Replies
2
Views
2K
  • Last Post
Replies
3
Views
2K
Replies
6
Views
1K
  • Last Post
Replies
4
Views
3K
Replies
13
Views
2K
  • Last Post
Replies
2
Views
632
Replies
2
Views
2K
Top