Solved

PI in c

Posted on 2007-03-26
12
806 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
[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
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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 …
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

688 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