How To Use Pi In Dev C++

  1. How To Use Pi In Dev C 4
  2. How To Use Pi In Dev C Youtube
How To Use Pi In Dev C++Use
P: n/a
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.1415926535897932384626433832795
#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.1415926535897932384626433832795f
#define PI_D 3.1415926535897932384626433832795
#define PI_L 3.1415926535897932384626433832795l
#define PI_C (4*atan(1))
int main(void)
{
printf('printf PI_x as double:n');
printf('PI_F %.*gn', DBL_DIG, PI_F);
printf('PI_D %.*gn', DBL_DIG, PI_D);
printf('PI_L %.*gn', DBL_DIG, (double) PI_L);
printf('PI_C %.*gnn', DBL_DIG, PI_C);
printf('printf PI_x as long double:n');
printf('PI_F %.*Lgn', LDBL_DIG, (long double) PI_F);
printf('PI_D %.*Lgn', LDBL_DIG, (long double) PI_D);
printf('PI_L %.*Lgn', LDBL_DIG, PI_L);
printf('PI_C %.*Lgnn', LDBL_DIG, (long double) PI_C);
return 0;
}
The output for one implementation
printf PI_x as double:
PI_F 3.14159274101257
PI_D 3.14159265358979
PI_L 3.14159265358979
PI_C 3.14159265358979
printf PI_x as long double:
PI_F 3.14159274101257324
PI_D 3.14159265358979312
PI_L 3.14159265358979324
PI_C 3.14159265358979312
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.141592653589793
is preferred
c) If the maximum possibel precision is required with a standard
type, use
#define PI 3.1415926535897932384626433832795l
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.1415926535897932384626433832795 /* 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.

How To Use Pi In Dev C 4

How to use pi in dev c 4

How To Use Pi In Dev C Youtube

Note that it is not best practice within C to use #defines for mathematical constants! Instead, as an example, you should use const double pi = 3.The #defines are a legacy feature of.