Calculating PI value is one of the complex problem and wikipedia talks about the approximations done for it and says it\'s difficult to calculate PI accurately.
How
In C Pi is defined in math.h: #define M_PI 3.14159265358979323846
I don't know exactly how C
calculates PI
directly as I'm more familiar with C++
than C
; however, you could either have a predefined C
macro
or const
such as:
#define PI 3.14159265359.....
const float PI = 3.14159265359.....
const double PI = 3.14159265359.....
/* If your machine,os & compiler supports the long double */
const long double PI = 3.14159265359.....
or you could calculate it with either of these two formulas:
#define M_PI acos(-1.0);
#define M_PI (4.0 * atan(1.0)); // tan(pi/4) = 1 or acos(-1)
IMHO I'm not 100% certain but I think atan()
is cheaper than acos()
.
Just define:
#define M_PI acos(-1.0)
It should give you exact PI number that math functions are working with. So if they change PI value they are working with in tangent or cosine or sine, then your program should be always up-to-dated ;)
Depending on the library you are using the standard GNU C Predefined Mathematical Constants are here... https://www.gnu.org/software/libc/manual/html_node/Mathematical-Constants.html
You already have them so why redefine them? Your system desktop calculators probably have them and are even more accurate so you could but just be sure you're not conflicting with existing defined ones to save on compile warnings as they tend to get defaults for things like that. Enjoy!
The closest thing C does to "computing π" in a way that's directly visible to applications is acos(-1)
or similar. This is almost always done with polynomial/rational approximations for the function being computed (either in C, or by the FPU microcode).
However, an interesting issue is that computing the trigonometric functions (sin
, cos
, and tan
) requires reduction of their argument modulo 2π. Since 2π is not a diadic rational (and not even rational), it cannot be represented in any floating point type, and thus using any approximation of the value will result in catastrophic error accumulation for large arguments (e.g. if x
is 1e12
, and 2*M_PI
differs from 2π by ε, then fmod(x,2*M_PI)
differs from the correct value of 2π by up to 1e12*ε/π times the correct value of x
mod 2π. That is to say, it's completely meaningless.
A correct implementation of C's standard math library simply has a gigantic very-high-precision representation of π hard coded in its source to deal with the issue of correct argument reduction (and uses some fancy tricks to make it not-quite-so-gigantic). This is how most/all C versions of the sin
/cos
/tan
functions work. However, certain implementations (like glibc) are known to use assembly implementations on some cpus (like x86) and don't perform correct argument reduction, leading to completely nonsensical outputs. (Incidentally, the incorrect asm usually runs about the same speed as the correct C code for small arguments.)
anyway you have not a unlimited accuracy so C define a constant in this way:
#define PI 3.14159265358979323846
import math.h to use this