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

  • Context: C/C++ 
  • Thread starter Thread starter #define
  • Start date Start date
  • Tags Tags
    Pi
Click For Summary

Discussion Overview

The discussion revolves around defining and using the mathematical constant pi in C++, focusing on achieving high precision in its representation and display. Participants explore different methods and considerations related to numerical precision in programming.

Discussion Character

  • Technical explanation, Conceptual clarification, Debate/contested

Main Points Raised

  • One participant inquires about defining pi with high precision, specifically using the value 3.1415926535897932384626433832795.
  • Another participant suggests that the default precision for output functions like printf and cout may limit the displayed digits, recommending adjustments to the precision settings.
  • A different viewpoint indicates that to maintain such precision, a custom function may be necessary, as long double types typically hold about 18 digits with a specific error margin.
  • A participant expresses gratitude for the information and mentions that they have decided to use 15 digits of precision instead of the originally sought higher accuracy.
  • One participant provides a mathematical expression, 4.0*atan(1.0), as an alternative way to compute pi.

Areas of Agreement / Disagreement

Participants present multiple viewpoints regarding the representation of pi and its precision, indicating that there is no consensus on the best approach to achieve the desired accuracy.

Contextual Notes

Limitations include the dependence on the system's handling of floating-point precision and the potential for varying results based on the chosen data type.

#define
Messages
3
Reaction score
0
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?
 
Technology news on Phys.org
I believe the default precision for printf and count 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)
 
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. count will be less precise with a define macro.
 
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:
4.0*atan(1.0)
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
4
Views
14K
  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 15 ·
Replies
15
Views
8K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 15 ·
Replies
15
Views
4K