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?
Best Answer
In C Pi is defined in math.h: #define M_PI 3.14159265358979323846
In C programming language, the value of pi can be defined using the constant π. The constant pi represents the ratio of the circumference of a circle to its diameter, which is approximately 3.14159. In C, you can define pi as a float or double variable:
float pi = 3.14159;
double pi = 3.14159;
The choice between float and double depends on the precision required for your calculations. Float is a single-precision floating-point type, while double is a double-precision floating-point type.
Once pi is defined, you can use it in various mathematical calculations involving circles, such as calculating the area or circumference of a circle. For example, to calculate the area of a circle with a radius of r, you can use the formula:
float area = pi * r * r;
double area = pi * r * r;
Similarly, to calculate the circumference of a circle with a radius of r, you can use the formula:
float circumference = 2 * pi * r;
double circumference = 2 * pi * r;
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.)
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 ;)
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
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;}
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!
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()
.