Should I use cmath to use PI and trig functions? I dont think math.h has it - and cmath and math seem like redefine alot of the same things.

What is standard practice?

Thanks,

Dave

daviddoria 334 Posting Virtuoso Featured Poster

Should I use cmath to use PI and trig functions? I dont think math.h has it - and cmath and math seem like redefine alot of the same things.

What is standard practice?

Thanks,

Dave

Narue 5,707 Bad Cop Team Colleague

>What is standard practice?

Most people just define their own as needed:

`const double PI = 3.141592;`

White-Gandalf 0 Newbie Poster

M_PI used to be in "math.h". And it's still there.

Narue 5,707 Bad Cop Team Colleague

Only on your compiler. M_PI isn't a standard name.

White-Gandalf 0 Newbie Poster

daviddoria 334 Posting Virtuoso Featured Poster

Narue 5,707 Bad Cop Team Colleague

>so how do i make PI a standard name on my compiler?

Join the standardization committee and convince them to add it.

>It doesn't seem to work with PI or M_PI if i include math.h and/or cmath.h

Right, so look at my first reply and define it yourself.

bbman 12 Junior Poster

Just to clarify if you need M_PI you usually need to do the following:

```
#define _USE_MATH_DEFINES
#include "math.h"
```

in that sequence!

then you get M_PI defined!

An alternative would be to use the c++ version

```
#define _USE_MATH_DEFINES
#include <cmath>
```

cheerz

Tiqiwac 0 Newbie Poster

Simple :

double const Pi=4*atan(1); // Pi=3.14

rgds

Dave

Tiqiwac 0 Newbie Poster

double const Pi=4*atan(1);

Theme_1 0 Newbie Poster

const double pi = boost::math::constants::pi<double>();

duskoKoscica 0 Newbie Poster

Try to see it in limits.h

COULD BE RIGHT COULD BE WRONG!

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Try to see it in limits.h

It might be there, but if it is, it's a library extension unique to your compiler.

duskoKoscica 0 Newbie Poster

Well if you like to have fun, try to construct a array that will converge to it. Then use that number in that file if you really like to have a fun with programing, but most do have limits.h.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

And if not, it would be good idea to have it, there are some constants that people use very often, so you cold try to writte them and then use them if you in need of it.

That's the approach I'd recommend. Create your own header with these things and reference it where needed. That way you don't depend on implementation-defined extensions.

but most do have limits.h

Everyone has that header, what I mean is that some variation of PI isn't a standard macro.

duskoKoscica 0 Newbie Poster

I don't know what you have ment, but this is it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.