Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does C define M_2_SQRTPI constant in <math.h>?

Tags:

c

math

In <math.h> the constant M_2_SQRTPI is defined with the value 2/sqrt(pi).

I have never seen this expression used in a formula, so I searched for uses of M_2_SQRTPI but I could only found documentation about the constant and no actual uses of it.

Where is the constant used and what is the reason it is defined in <math.h> as a mathematical constant?

like image 687
Mustafa Serdar Şanlı Avatar asked Oct 20 '25 15:10

Mustafa Serdar Şanlı


2 Answers

The constant is related to the error function.

I don't know why it's provided as a constant in the standard library POSIX, though. With GCC on a x86-64 system, and by extension on every system conforming to IEEE 754, computing 2.0 / sqrt(M_PI) yields exactly the same double-precision value as M_2_SQRTPI.

Try it online!

Even if there was a slight error, the very few people who need the constant could simply define it themselves.

like image 147
nwellnhof Avatar answered Oct 23 '25 05:10

nwellnhof


It is used in different scientific computations (I've seen it in finance, for instance).

Why do you want it as a constant? Because sqrt(M_PI) will not have the same precision as a precomputed value (using more precision for PI to start with). And it makes quite a difference, I've seen the damages of not using it.

like image 37
Matthieu Brucher Avatar answered Oct 23 '25 05:10

Matthieu Brucher