Math constant PI value in C
CC Problem Overview
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 does C calculate PI? Does it calculate it every time or is it using a less accurate fixed value?
C Solutions
Solution 1 - C
In C Pi is defined in math.h: #define M_PI 3.14159265358979323846
Solution 2 - C
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.)
Solution 3 - C
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 ;)
Solution 4 - C
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
Solution 5 - C
you could make a function that calculates PI, by doing one of multiple possible infinite sum calculations(I wouldn't recommend something like atan() as those functions probably use pi in one form or another themselves). That said I would not recommend manually calculating pi. Noone needs pi to be any more accurate than math.h provides with M_PI, in fact, it would probably be best for you to use just the first few digits of it instead as that would allow you to have slightly faster code. Unless you want to calculate the size of the universe down to the centimetre why bother?
If you still want to calculate it here's how
(method from 3blue 1 brown The Wallis product for pi, proved geometrically
double caculate_pi(int accuracy){
double result = 1;
int a = 2;
int b = 1;
for(i = 0;i < accuracy; i ++){
result = a/b * result;
if(a < b){
a = a + 2;
}
else if(b < a){
b = b + 2;
}
}
return result * 2;
}
Solution 6 - C
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!
Solution 7 - C
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()
.