a number for pi

Started by
27 comments, last by JYoung 21 years, 1 month ago
hey, i have a program where i have to convert radians to degrees. In order to do that i have to divide by pi. Instead of using the inaccurate 3.14 is there a known function or variable for pi? thanks, JYoung
Advertisement
There are a number of ways of calculating PI at runtime, such as 4*atan(1). The thing is floating point numbers don''t have a huge accuracy (around 7 digits for single precision and 15 digits for double precision), so 3.14159 or something similar is usually accurate enough for typical use.
Assuming you're using C/C++, it is pretty standard for all C/C++ compilers to have a define for pi, called M_PI, defined in math.h, so:

#include <math.h>

And then just use M_PI

eg:

double pi = M_PI;


Unless you need pi to a ridiculous number of decimal points (more than a double can hold), this works fine and is easily understood.

[edited by - gmcbay on February 7, 2003 1:32:51 PM]
I tend to grab it & #define it from windows calculator...or any calculator I can get my hands on!
double Pi = M_PI;

C:\Program Files\Microsoft Visual Studio\VC98\OpenGL\TheGrapher\main.cpp(81) : error C2065: 'M_PI' : undeclared identifier


[edited by - JYoung on February 7, 2003 3:47:49 PM]
Check math.h, you have to #define some macro in MSVC to make M_PI and various other constants available.
#define my_PI 3.1415926535897932384626433832795

and you are good to go.

____________________________________________________________
Try RealityRift at www.planetrift.com
Feel free to comment, object, laugh at or agree to this. I won''t engage in flaming because of what I have said.
I could be wrong or right but the ideas are mine.

No no no no! :)
Here is some more digits if you care. Of course, it all depends on the resolution on your platform.

const long double gPI = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647094;
Sooooooo.....


if you are not satisfied with one million digits...
And you laugh at 2 million digits..
and 3 million make you yawn..
and you think 4 million digits are for girls...
..and so on...

http://www.hepl.phys.nagoya-u.ac.jp/%7Emitsuru/pi-e.html


that should satisfy even the toughest...
Wow!

This topic is closed to new replies.

Advertisement