1. ## Circle Calculator Job

What do you guys think of this? Its my first attempt at a useful C++ program. I would prefer to use if statements but I'm following a book and will learn it eventually. I might have used some time consuming code but I wanted to test my knowledge of functions.

Code:
#include <iostream>

float CircleArea(long int Radius, const float Pi);
float CircleCircumference(long int Radius, const float Pi);

/*Program created by ComDriver
13th Febuary 2005
This program can find a circle's area and circumference*/

int main()
{
const float Pi = 3.141592653589793238462633832795;

std::cout << "Menu- Find a cicle's-\n\n";
std::cout << "1. Area\n";
std::cout << "2. Circumference\n";

std::cout << "What would you like to do? ";
std::cin.ignore(80,'\n');

//The users Menu choice on what he wants to find is evaluated here
{

std::cout << "\nWhat is the radius of the circle (in centimetres): ";
std::cin.ignore(80,'\n');

std::cout << "\nThe circle's area is: " << CircleArea(Radius, Pi) << " square cm.";

}

{

std::cout << "\nWhat is he radius of the circle: ";
std::cin.ignore(80,'\n');

std::cout << "\nThe circumference of the circle is: " << CircleCircumference(Radius, Pi) << "cm";

}

std::cin.get();
return 0;

}

//This function finds a circles area
float CircleArea(long int Radius, const float Pi)
{

}

//This function finds a circles circumference
float CircleCircumference(long int Radius, const float Pi)
{

}

2. Good job, you should add different units instead of just centimeters and maybe different shapes.

3. ## Once I learn

Once I learn the switch statement and loops I want to make a decent calaulator program witch can do all the basic functions, cos, tan and sin, all that circle stuff and more.

4. Most of the precision of your Pi value is thrown away because a float simply cannot be that precise. Run this code and see why:

Code:
#include <iostream>
#include <limits>

using namespace std;

int main()
{
const float Pif = 3.141592653589793238462633832795f;
const double Pid = 3.141592653589793238462633832795;

cout.precision(32);
cout.flags(ios::fixed);
cout << "Pi as string:     3.141592653589793238462633832795" << endl;
cout << "float precision:  " << numeric_limits<float>::epsilon() << endl;
cout << "Pi as float:      " << Pif << endl;
cout << "double precision: " << numeric_limits<double>::epsilon() << endl;
cout << "Pi as double:     " << Pid << endl;
}
Output:
Code:
Pi as string:     3.141592653589793238462633832795
float precision:  0.00000011920928955078125000000000
Pi as float:      3.14159274101257320000000000000000
double precision: 0.00000000000000022204460492503131
Pi as double:     3.14159265358979310000000000000000
The point is, if you're going to use a float, then you might as well only use:
Code:
const float Pi = 3.141592;
However with a double you can go up to:
Code:
const double Pi = 3.141592653589793;

Umm isn't it pi*r*r?

6. I know it's PieRaidusSquared but didn't know how to represent the Squared.

7. Originally Posted by hk_mp5kpdw
Most of the precision of your Pi value is thrown away because a float simply cannot be that precise. Run this code and see why:

The point is, if you're going to use a float, then you might as well only use:
Code:
const float Pi = 3.141592;
However with a double you can go up to:
Code:
const double Pi = 3.141592653589793;
In fact, I don't see any reason to use float at all (unless you have enormous arrays of them and storage is an issue). Why not just use double for everything?

Also, instead of having to find the expression with the 16 or 17 decimal digits for pi to paste into your program every time, you can use the library function atan() to initialize it:

Code:
#include <iostream>
#include <cmath>

int main()
{
const float fPi  = 3.141592653589793238462633832795;
const double dPi = 3.141592653589793238462633832795;
const double xPi = 4.0 * atan(1.0);

std::cout.precision(27);
std::cout << "fPi = " << fPi << std::endl;
std::cout << "dPi = " << dPi << std::endl;
std::cout << "xPi = " << xPi << std::endl << std::endl;

return 0;
}
Someone has already pointed out that the formula for area in the original post has an "oops". A simple way to get the correct answer is something like:
Code:
Regards,

Dave

"pi-r-square: That can't be right. Pie are round; cornbread are square!"

-- A little jingle I learned in my youth in the flatlands of Southeast Arkansas.

8. I'll probably be wrong and you guys will be like duh, but I think its still wrong. Remember order of operations??? You need to do exponents first so it would be:
Code:

9. Originally Posted by cerin
I'll probably be wrong and you guys will be like duh, but I think its still wrong. Remember order of operations??? You need to do exponents first so it would be:
Code:
Thie following code performs two multiplications (no exponents in this expression --- as a matter of fact there are no exponent operators in C or C++)
Code:
The following code performs two multiplications

Code:
Results are the same with or without parentheses, since there is no precedence to contend with.

In cases like this, you can sometimes learn by making a program that uses both expressions and see if you can come up with an example that shows a difference. (Just remember: you can't prove that they are the same just by testing; you have to know how things work. On the other hand, if you can come up with an example that shows they are different, then you have proved your point.)

Regards,

Dave

10. Originally Posted by ComDriver
const float Pi = 3.141592653589793238462633832795;
Use the M_PI constant in <cmath>

Originally Posted by Dave Evans
Also, instead of having to find the expression with the 16 or 17 decimal digits for pi to paste into your program every time, you can use the library function atan() to initialize it:
atan uses numerical methods and might not be accurate.
M_PI is shorter, more or equally exact and easier to understand.

Originally Posted by cerin
I'll probably be wrong and you guys will be like duh, but I think its still wrong. Remember order of operations??? You need to do exponents first so it would be:
Code:
No.
Basic elementary school algebra: The order of multiplication doesn't matter.

More precision of PI than a few digits is quite pointless actually. Even if you could measure the radius exactly, the formula A=pi*r^2 itself becomes inaccurate.

11. Originally Posted by Sang-drax
Use the M_PI constant in <cmath>

atan uses numerical methods and might not be accurate.
M_PI is shorter, more or equally exact and easier to understand.

No.
Basic elementary school algebra: The order of multiplication doesn't matter.

More precision of PI than a few digits is quite pointless actually. Even if you could measure the radius exactly, the formula A=pi*r^2 itself becomes inaccurate.
Not all systems have M_PI defined. Since the initialization of pi by using atan() is done once in the program (and, in fact, it can be done at compile time), performance is not an issue.

In the systems that I have tested, atan(1.0) is correct to the number of bits of a double. I certainly can't claim that that is always so, but if your library function can't calculate atan(1.0) correctly then you might not have as much success with mathematical operations as you would wish.

I would be interested in seeing printouts from anyone who is interested that show the accuracy of M_PI compared with the accuracy of 4.0 * atan(1.0). Tell me which compiler you used, and print out 18 (or more) decimal digits.

I have had many numerical analysis applications where a value of pi to 6 decimal digit precision was not adequate, so the statement that, "more precision than a few digits is pointless," doesn't cut any ice with me (but that's just me). Sometimes we calculate based on something other than physical measurements. (And sometimes physical measurements can be breathtakingly precise, these days.) The principles learned in such an elementary problem as the area of a circle may carry over into more demanding and more rewarding endeavors.

Just my opinion, of course, and worth exactly what you paid for it (actually, a bargain at twice the price).

Regards,

Dave

12. My statment about the precision of Pi was in response to
const float Pi = 3.141592653589793238462633832795;
To calulate the area of a circle with this precision, you cannot use the formula
A = pi * r * r
even if you knew the exact radius. In the real world, that formula is only an approximation, because the space-time is curved etc. etc.
More precision is never wrong, but quickly becomes quite pointless.

13. You will be fine using floats. Since you are not trying to launch a rocket from here to Jupiter the imprecision in the float data type is not going to significantly affect the outcome given your situation.

If you are trying to launch a rocket to Jupiter then neither float or double will get it there.

BTW expanding PI that far out really does nothing in relation to the task at hand - the differences are so infinitely small that the resultant change is close to infinite - or undefined. No expansion of PI will do anything to help the imprecision found in float and/or double - there are certain ranges of values that simply cannot be expressed by the floating point format and thus some rounding will always occur.

You will have to use a library that supports larger floating point data types and/or you can use the FPU's native data type in pure asm. But I see no point in this.