Szabolcs Nagy wrote:

i've just found out (searching through n1124.pdf) that in c99 math.h

does not contain M_PI

what is the desired way to use the PI constant in a c code then?

#define PI 3.1415926535897 932384626433832 795

#define PI (4*atan(1))

....

?

There are two obvious disadvantages to the (4*atan(1)) form

a) atan(1) is not computed at compile time, so the computation of

atan(1) occurs each time PI is used

b) atan() returns a double. Your first form, which has more digits than

most implementations have for a long double, suggests an desire for

a degree of accuracy beyond the requirements even of astrophysics.

If LDBL_DIG DBL_DIG you may well find (4*atan(1)) the less

satisfactory in terms of precision.

Suppose you ran a program like this:

#include <stdio.h>

#include <float.h>

#include <math.h>

#define PI_F 3.1415926535897 932384626433832 795f

#define PI_D 3.1415926535897 932384626433832 795

#define PI_L 3.1415926535897 932384626433832 795l

#define PI_C (4*atan(1))

int main(void)

{

printf("printf PI_x as double:\n");

printf("PI_F %.*g\n", DBL_DIG, PI_F);

printf("PI_D %.*g\n", DBL_DIG, PI_D);

printf("PI_L %.*g\n", DBL_DIG, (double) PI_L);

printf("PI_C %.*g\n\n", DBL_DIG, PI_C);

printf("printf PI_x as long double:\n");

printf("PI_F %.*Lg\n", LDBL_DIG, (long double) PI_F);

printf("PI_D %.*Lg\n", LDBL_DIG, (long double) PI_D);

printf("PI_L %.*Lg\n", LDBL_DIG, PI_L);

printf("PI_C %.*Lg\n\n", LDBL_DIG, (long double) PI_C);

return 0;

}

The output for one implementation

printf PI_x as double:

PI_F 3.1415927410125 7

PI_D 3.1415926535897 9

PI_L 3.1415926535897 9

PI_C 3.1415926535897 9

printf PI_x as long double:

PI_F 3.1415927410125 7324

PI_D 3.1415926535897 9312

PI_L 3.1415926535897 9324

PI_C 3.1415926535897 9312

suggests for this implementation:

a) if float operations are faster than doubles, and no more 8

significant digits are needed

#define PI 3.14159265f

suffices

b) if double operations are at least as fast as floats, and no

more than 16 significant digits, or, whatever the relative

speed of floats and doubles, if at 9 but not more than 16

significant digits are needed

#define PI 3.1415926535897 93

is preferred

c) If the maximum possibel precision is required with a standard

type, use

#define PI 3.1415926535897 932384626433832 795l

is best.

In fact each of those defines should probably carry all the digits up to

some unneeded level of precision, since a different implementation might

well make use of more than does this implementation.

The only reason I can see for

#define PI (4*atan(1))

is that the implementation may well ensure that this has the maximum

precision and accuracy that a double can attain (but no more).

But that can probably be done with

#define PI 3.1415926535897 932384626433832 795 /* double */

better.

If you insist on using the (4*atan(1)) form, use, rather than a define,

in each function that now uses the symbol PI

const double PI = 4 * atan(1);

Straining the usual conventions on identifiers that are all caps.