Solved

PI in c

Posted on 2007-03-26
12
804 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 50 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 100 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
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 50 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 50 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 100 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

829 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