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

  • C/C++
  • Thread starter #define
  • Start date
  • Tags
    Pi
In summary, the conversation discusses the difficulty in accurately defining and using the mathematical constant pi, with one participant suggesting using a long double or #define to increase precision. Another participant mentions changing the precision in display routines and the existence of M_PI in the math.h include file. Ultimately, it is suggested to write a custom function for even greater precision. The conversation ends with the acknowledgement that it may not be necessary to have such a high level of accuracy.
  • #1
#define
3
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
  • #2
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
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
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
4.0*atan(1.0)
 

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

1. What is pi and why is it important in mathematics?

Pi is a mathematical constant that represents the ratio of a circle's circumference to its diameter. It is approximately equal to 3.14159 and is an irrational and transcendental number. Pi is important in mathematics because it appears in many formulas and equations, and is used in various calculations in geometry, trigonometry, and other branches of mathematics.

2. How is pi typically defined in C++?

In C++, pi is typically defined using the built-in constant "M_PI" from the "cmath" library. This constant has a value of approximately 3.14159 and can be used in mathematical calculations involving pi.

3. Can pi be approximated using a C++ program?

Yes, pi can be approximated using a C++ program. There are many different methods for approximating pi, such as the Monte Carlo method or the Leibniz formula. These methods involve using a series of calculations to get closer and closer to the actual value of pi.

4. How precise is the value of pi in C++?

The value of pi in C++ is precise up to the number of digits specified by the data type used. For example, if a "double" data type is used, the value of pi will be precise up to 15-16 digits. However, since pi is an irrational number, it cannot be represented exactly in a finite number of digits.

5. Is there a limit to how many digits of pi can be calculated in C++?

Yes, there is a limit to how many digits of pi can be calculated in C++. This limit is determined by the data type used and the computational power of the computer. However, even with the most advanced computers, it is not possible to calculate an infinite number of digits of pi.

Similar threads

  • Programming and Computer Science
Replies
4
Views
132
  • Programming and Computer Science
Replies
1
Views
661
  • Calculus and Beyond Homework Help
Replies
3
Views
339
  • Programming and Computer Science
Replies
6
Views
8K
  • General Math
Replies
24
Views
1K
  • Calculus and Beyond Homework Help
Replies
8
Views
362
  • Programming and Computer Science
2
Replies
35
Views
2K
  • Programming and Computer Science
Replies
4
Views
13K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
2
Views
4K
Back
Top