?
Solved

PI in c

Posted on 2007-03-26
12
Medium Priority
?
814 Views
Last Modified: 2008-02-01
#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
0
Comment
Question by:valleytech
  • 3
  • 3
  • 2
  • +4
12 Comments
 
LVL 4

Expert Comment

by:MacroLand
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

by:
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

by:itsmeandnobodyelse
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 4

Expert Comment

by:MacroLand
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

by:Infinity08
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

by:itsmeandnobodyelse
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

by:MacroLand
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

by:fridom
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

by:_iskywalker_
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

by:cup
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

by:itsmeandnobodyelse
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

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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
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…
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question