Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# PI in c

Posted on 2007-03-26
Medium Priority
812 Views
#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.

PS: i use visual c++ 6.0
0
Question by:valleytech
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 3
• 2
• +4

LVL 4

Expert Comment

ID: 18797975
what is M_P?

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

Regards,
0

LVL 3

Accepted Solution

srinims earned 150 total points
ID: 18798000
hi it is M_PI not M_P

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
0

LVL 39

Assisted Solution

itsmeandnobodyelse earned 300 total points
ID: 18798021
>>>> it is a good practice to use float PI=static_cast<float>(M_P)
You 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

0

LVL 4

Expert Comment

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

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

Regards,
0

LVL 53

Assisted Solution

Infinity08 earned 150 total points
ID: 18798231
>> It is one of the many definitions of PI in mathematics where PI is derived from.

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.
0

LVL 39

Expert Comment

ID: 18798413
>>>> It is one of the many definitions of PI in mathematics
>>>> 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.
0

LVL 4

Expert Comment

ID: 18798435
>>No, PI is not derived it is a constant.

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.
0

LVL 24

Expert Comment

ID: 18798631
very funny C and static_cast<float>(

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

Regards
Friedrich
0

LVL 6

Expert Comment

ID: 18798797
sometimes when you need more precision you can calculate the method of PI by algorithms, say like you want a precision even greater than double, or double in x64 architectures, and pi isnt well enogh define (PI has infinite number not equal 0 after the . ) for your purpose then you should calculate it. but normally the trigonometric functions already use a constant  (math.h is your friend), calculating something which is already calculated may cause loss of precision and time. You can calculate PI by:
4*(1-(1/3)+(1/5)-(1/7)+(1/9)- ...)= M_PI
0

LVL 11

Assisted Solution

cup earned 150 total points
ID: 18810946
When I used to work on 16 bit machines, 355.0 / 113.0 was a pretty good approximation - better than 22/7 which was only accurate to 2 DP.

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.14159265358979323846264338327950288419716939937510;

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.
0

LVL 39

Assisted Solution

itsmeandnobodyelse earned 300 total points
ID: 18813973
>>>>> const double PI = 3.14159265358979323846264338327950288419716939937510;
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 ;-)
0

LVL 11

Expert Comment

ID: 18820111
Cut and paste is your friend :)
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticallâ€¦
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data definâ€¦
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relatâ€¦
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
###### Suggested Courses
Course of the Month9 days, 12 hours left to enroll