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

  • Thread starter Thread starter #define
  • Start date Start date
  • Tags Tags
    Pi
AI Thread Summary
To define and use pi with high precision, it's important to note that standard data types like double and long double have limitations on the number of digits they can accurately represent. The default precision for output functions like printf and cout is typically set to six digits, which can be adjusted to display more digits. For maximum precision, users may need to implement their own function to handle pi or utilize constants like M_PI from the math.h library. A long double can represent about 18 digits with a small error margin, but for many applications, a precision of around 15 digits is often sufficient. The discussion highlights the importance of understanding data type limitations and output formatting when working with precise numerical values.
#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 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)
 
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.
 
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)
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top