Solved

PI

Posted on 1998-12-16
9
338 Views
Last Modified: 2010-04-16
VC5, WinNT4.0

I know that #define PI      3.14159 would be good enough in my programme.

Does it make any sense if i do something like this?
What is the effect on the compiler/linker ?

#define PI      3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679



Leow  
0
Comment
Question by:engllf
[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
  • 5
  • 3
9 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 1180261
first of all, don't use #define to define constatnts in C++, that has been replaced with a safer mechansim

const double PI = 3.14.....

To answer your question, if you make it longer, you will have automatically more accuracy if you port the program to a system that has floating point numbers with additional accuracy.  
0
 
LVL 1

Expert Comment

by:The_Brain
ID: 1180262
It would depending on.  
The calculation which you are doing.

for instance

void main()
{
  float answer;
  cout<<answer*PI;
}

it would be 12 dec places.  I not sure about that.  you can set it cout.precision(x);
don't use define though.  use const double Pi=3.14159 etc

but if not float, if double then 18 digits precision by default.

So if you use your define, then float would be appropiate.
else define it more accurately, (because)
cout<<PI+(double)
3.14159000000000
etc.

so be more accurate for double 18 dec places is enough. float 11-12 is enough. etc.  it depend on what you want to do.  for the sake of accuracy more dec places is better.  not more than 18, that would be useless.

0
 
LVL 22

Expert Comment

by:nietod
ID: 1180263
you can also get the desited accuracy using an expression, like

const double PI = acos(-1);


0
Technology Partners: 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 22

Expert Comment

by:nietod
ID: 1180264
>> for the sake of accuracy more dec places is better.  not more than 18, that would be useless
The C++ standard only defines minimum requirements on the accuracy of floating point values.  Some implimentations may define more accurate floating point values.  Many mainframes have 16 byte floating point numbers that have accuracy greater than 30 decimal digits. (That is what mainframes are still used for.)
0
 
LVL 1

Expert Comment

by:The_Brain
ID: 1180265
Ok fine, pick the easy way out of defining PI :)
acos(-1), fine be like that :):):):)
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1180266
have a look at

http://snippets.org/#section1group11

Various ways of computing PI to ridiculous lengths

acos(-1) cannot be used globally, because acos(-1) is not a constant expression.  Or am I mistaken here?

0
 
LVL 1

Expert Comment

by:The_Brain
ID: 1180267
yup.
acos(-1) is a constant expression. don't be ridiculous, if I am wrong please as a mathemitician and programmer i would LOVE to see why.... I am very interested. :)
0
 
LVL 22

Expert Comment

by:nietod
ID: 1180268
you can use

const int PI = acos(-1);

globally.  There is not guarantee that the acost(-1) will be calculated at compile time and embedded as a constant like it would be with

const int PI = 3.14159;

but if, not it will be calculated one time at program start-up which is not big deal.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1180269
engulf, are you going to grade this question?
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is the real difference between C++ and Visual C++.NET? 7 142
Header of docx file 17 142
learn programming 8 92
keep track of class structure 1 50
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

738 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