#define _USE_MATH_DEFINES???

Im trying to show pi. I know how to set precision and all that, but I cant seem to get it! I did this:

1
2
#define _USE_MATH_DEFINES
#include <cmath> 


Isnt this what I supposed to do? I think there's something to do with M_PI, but I type that in and its still underlined in red in my compiler. What mistake am I making?

EDIT: DO I have to type in all the digits of pi and then define it? just wondering
Last edited on
I think M_PI may be non-standard, depending on which compiler is used.

You could do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <iomanip>
#include <cmath>

    using namespace std;

int main()
{
    const double PI = 4.0 * atan(1.0);

    cout << "PI = " << setprecision(16) << PI << endl;

    return 0;
}


Output:
PI = 3.141592653589793


Line 9 uses the fact that the angle whose tangent equals 1, is PI/4 radians or 45 degrees. So calculate it once at the start of the program and from then on use the stored value.
Ok thank you.
Topic archived. No new replies allowed.