Solved

PI in c

Posted on 2007-03-26
12
800 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
Comment Utility
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
Comment Utility
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
Comment Utility
>>>> 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

by:MacroLand
Comment Utility
>>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
Comment Utility
>> 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
Comment Utility
>>>> 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 4

Expert Comment

by:MacroLand
Comment Utility
>>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
Comment Utility
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_
Comment Utility
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
Comment Utility
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
Comment Utility
>>>>> 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
Comment Utility
Cut and paste is your friend :)
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
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 this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now