This course teaches how to install and configure Windows Server 2012 R2. It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

#include <stdio.h>

#include <math.h>

void main()

{

float PI= 4.0*atan(1.0);

float x = PI;

printf("%f",x);

}

----> it works. However, i tried

float PI=(float) M_P instead of float PI= 4.0*atan(1.0);

---> doen'st work.

Please help me . Thanks

PS: i use visual c++ 6.0

#include <math.h>

void main()

{

float PI= 4.0*atan(1.0);

float x = PI;

printf("%f",x);

}

----> it works. However, i tried

float PI=(float) M_P instead of float PI= 4.0*atan(1.0);

---> doen'st work.

Please help me . Thanks

PS: i use visual c++ 6.0

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

u have to define _USE_MATH_DEFINES using

#define _USE_MATH_DEFINES

to get use of math definitions for common math constants

or you have to include _USE_MATH_DEFINES in the compiler settings, where we set external macros

if not working try below

acctually atan will uses double as input and double as output, better use atanf for floating point values

#ifndef M_PI

#define M_PI 3.14159265358979323846 /* pi */

#endif

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trialYou don't need a cast at all. If you need a better precisiuon use a double instead of a float.

>>>> float PI= 4.0*atan(1.0);

As PI is a constant you should avoid to compute it by using trigonometric functions.

Regards, Alex

It is one of the many definitions of PI in mathematics where PI is derived from.

Regards,

The problem is that the atan() function probably makes use of PI to calculate the result (directly or indirectly). So, you've got a recursive definition, which is not good :)

>> float PI=(float) M_P instead of float PI= 4.0*atan(1.0);

>> ---> doen'st work.

How does it not work ? What result do you get ? If M_PI is defined like this :

#define M_PI 3.14159265358979323846 /* pi */

then there shouldn't be a problem.

>>>> where PI is derived from.

No, PI is not derived it is a constant. You can get PI as a result of many mathematical algorithms or by calculating infinite rows, however that is like you would calulate 2.0 by sum(1 + 1/2 + 1/4 + 1/8 + ...) what gives the correct result but nevertheless is nonsense.

It is indeed a constant where the value is derived by many mathematical methods.

I agree that in programming instead of derivation using the constant value will definitely make more sense.

using the proper constans is the correct answer IMHO or if you need more precision checking the diverse large rational packages.

Regards

Friedrich

4*(1-(1/3)+(1/5)-(1/7)+(1/

This should be a compile time thing so I'd declare it as

const double PI = 355.0 / 113.0; /* the pattern is 11 33 55 written from bottom to top */

or if you want something more accurate (from http://en.wikipedia.org/wiki/Pi)

const double PI = 3.141592653589793238462643

const basically means "I promise not to change it". It is better than using #defines - if you get it wrong, you can change it in the debugger (OK - I broke my promise but I was debugging) and keep on testing instead of having to stop and rebuild.

As a double on a 32bit platform has a maximum precision of about 15 significant decimal digits you may not worry if you have a typo when copying the above number ;-)

C++

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

and also it is a good practice to use float PI=static_cast<float>(M_P)

Regards,