#### Archived

This topic is now archived and is closed to further replies.

# a number for pi

This topic is 5468 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
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.

##### Share on other sites
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]

##### Share on other sites
I tend to grab it & #define it from windows calculator...or any calculator I can get my hands on!

##### Share on other sites
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]

##### Share on other sites
Check math.h, you have to #define some macro in MSVC to make M_PI and various other constants available.

##### Share on other sites
#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.

##### Share on other sites
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;

##### Share on other sites
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!

##### Share on other sites
Assuming your using a 387 chip or above...

There is actually an intruction to push an 80-bit value of pi onto the FPU register stack. It saves you the expense of a memory call and you get the extended-precision (80-bit) value. Only use it if you are looking to eek out an extra fraction of a microsecond of CPU time.

##### Share on other sites
3.1415926 is tedious
3.1415926 is a literal
3.1415926 is an f9
3.1415926 is good enough
3.1415926 is good enough for many applications
3.1415926 is the latest
3.1415926 is a constant or numeric literal
3.1415926 is a real number
3.1415926 is a magic number ? pi is a good identifier
3.1415926 is closer to
3.1415926 is less than pi
3.1415926 is the value i used for pie
3.1415926 is not an int

##### Share on other sites
3.1415926 is tedious
3.1415926 is a literal
3.1415926 is an f9
3.1415926 is good enough
3.1415926 is good enough for many applications
3.1415926 is the latest
3.1415926 is a constant or numeric literal
3.1415926 is a real number
3.1415926 is a magic number ? pi is a good identifier
3.1415926 is closer to
3.1415926 is less than pi
3.1415926 is the value i used for pie
3.1415926 is not an int

##### Share on other sites
Can''t you just take the atn(4) ? actually it might be atn(8)...

##### Share on other sites
It''s 4*atan(1). I''ve used it once, but it made my code longer and harder to read Not everyone knows at first glance that the previous statement is equivalent to PI...

##### Share on other sites
But if you store the value in a variable named pi, then people will know, nor will it make the code harder to read than a hardcoded variable.

##### Share on other sites
quote:
Original post by MatKing
Can''t you just take the atn(4) ? actually it might be atn(8)...

nop it''s 2*atan(infinite)

##### Share on other sites
quote:
Original post by pawn69
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647

Hey... good approximation!

##### Share on other sites
This could be interesting:

www.hut.fi/~mnippula/votepi.html

As could this:

lcf.www9.50megs.com/pi.html

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

##### Share on other sites
so is there an actual equation to pi (as (1+sqrt(5))/2 is to phi) ? Sounds dumb but deriving the value from really precise and accurate measurements of circumfrence and radius seems awkward. Thanks ahead for curing me of another bit of ignorance!

##### Share on other sites
Unfortunately (or perhaps fortunately for the universe) I believe Pi is known to be non-algebraic - meaning there''s no equation for it. There are several sequences that provide progressively better approximations, though most converge pretty slowly (I tried one with a programmable calculator and got bored of hitting the execute button before it had stabilised to the limits of the display (10 digits)). It should be pretty easy to find them with a little research.

##### Share on other sites
You are right, pi is not algebraic, which means it is not the solution to a polynomial function with integer coefficients. There are formulas that exist- the best place to look for them is http://mathworld.wolfram.com. However, as one of GRhodes'' links tell you, it is not really needed for all practical purposes. I forget what the exact value is, but if you wanted to calculate the circumfrance of the universe to the diameter of a hydrogen atom, you need less than 100 digits of pi.

Brendan

##### Share on other sites
Haha, thanks for the input, and that last example gave a bit of perspective to it all. Thanks again.

##### Share on other sites
another expression for PI:

4*integral(sqrt(1-x^2) dx, [0,1])

##### Share on other sites
Pi = sqrt(6/P)

where P is the probability of two random numbers being relatively prime. So you can compute an approximation to Pi by computing a large number of pairs of random integers and approximating P as the fraction of those pair that actually are relatively prime.

Not very accurate, but I find it funny...