Working Out A Cirles Circumferance In C++
DY357LX
Playing since day 1. Still can't Comm.England Join Date: 2002-10-27 Member: 1651Members, Constellation
![DY357LX](https://forumstest.unknownworlds.com/uploads/userpics/359/nVBAQG6NUC5J1.png)
in Off-Topic
<div class="IPBDescription">Pi = 3.1415926535897932. Is This Right?</div> Since I can't play Doom 3 or PoP:SoT i've been messing
about in C++ and trying to think of some useful programs
to code.
I settled on working out the circumference of a circle but i'm
not sure if my code is right because i've never really understood
the mathematical way of working out the problem.... oh look,
i've confused myself. Haha, here's my code:
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->// float pi ?
#include <iostream>
int main()
{
using namespace std;
float pi;
pi = 3.1415926535897932;
float size;
cout << "This program requires that you know the radius of the circle." << endl;
cout << "The radius is the distance from one edge of the circle into the exact middle." << endl;
cout << "The radius is then multiplied by pi." << endl;
cout << endl;
cout << endl;
cout << "Enter the radius of the circle: " << endl;
cin >> size;
size = size * pi;
cout << "The size is: " << endl;
cout << size << endl;
return 0;
}
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
I'm not sure if a "float" was right to use for storing pi
but the program compiles and runs fine.
So.... does to look right to the programmers out there?
Any other ways of coding this program?
Share. <!--emo&:)--><img src='http://www.unknownworlds.com/forums/html//emoticons/smile.gif' border='0' style='vertical-align:middle' alt='smile.gif' /><!--endemo-->
about in C++ and trying to think of some useful programs
to code.
I settled on working out the circumference of a circle but i'm
not sure if my code is right because i've never really understood
the mathematical way of working out the problem.... oh look,
i've confused myself. Haha, here's my code:
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->// float pi ?
#include <iostream>
int main()
{
using namespace std;
float pi;
pi = 3.1415926535897932;
float size;
cout << "This program requires that you know the radius of the circle." << endl;
cout << "The radius is the distance from one edge of the circle into the exact middle." << endl;
cout << "The radius is then multiplied by pi." << endl;
cout << endl;
cout << endl;
cout << "Enter the radius of the circle: " << endl;
cin >> size;
size = size * pi;
cout << "The size is: " << endl;
cout << size << endl;
return 0;
}
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
I'm not sure if a "float" was right to use for storing pi
but the program compiles and runs fine.
So.... does to look right to the programmers out there?
Any other ways of coding this program?
Share. <!--emo&:)--><img src='http://www.unknownworlds.com/forums/html//emoticons/smile.gif' border='0' style='vertical-align:middle' alt='smile.gif' /><!--endemo-->
Comments
*edit*
And you could make the two cout << endl; to cout << endl << endl;
I didn't think it was important where it went Otto.
But thanks for reading through <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html//emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif' /><!--endemo-->
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->// float pi ?
#include <iostream>
using namespace std;
int main(void)
{
float pi;
pi = 3.1415926535897932;
float size;
cout << "This program requires that you know the radius of the circle.\nThe radius is the distance from one edge of the circle into the exact middle.\nThe radius is then multiplied by pi.\n\n\nEnter the radius of the circle: \n";
cin >> size;
size *= pi;
cout << "The size is: \n" << size << "\n";
return 0;
}<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
It doesn't really matter, I just prefer to keep my code compact. But yes, it looks correct <!--emo&:)--><img src='http://www.unknownworlds.com/forums/html//emoticons/smile.gif' border='0' style='vertical-align:middle' alt='smile.gif' /><!--endemo-->
Or cmath, whichever is better, i can never remember.
using namespace std;
int main(void)
{
const double pi = 3.1415926535897932;
float radius;
cout << "SUPER CIRCLE HAPPY FUN TEIM, INPUT RADIUS TO FIND CIRCUMFERENCE!\n";
cin >> radius;
cout << "\n" << 2*pi*radius << " is your circumference\n";
return 0;
}<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
Hidey Pidey.
Also, why not try learning the Windows GUI or something so you don't have to program console apps.
A windows app always takes some online research, cutting/pasting, and bug testing.
Yes, a lot of numbers go on forever. The world has accepted reasonable approximations
That is correct.
Also, would making Pi such a looong number (3.XXXXXXXXXXXXXXXXX) not overflow the Float? I'd really shorten it down to 3.141 or so.
Real number precision... how I hate thee.
I ran into it a little bit while I was making this one Pascal program years ago. Never really understood the explanation of why it happened.
Real number precision... how I hate thee.
I ran into it a little bit while I was making this one Pascal program years ago. Never really understood the explanation of why it happened. <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
It's quite simple, you run out of bits to store the information.
I won't get into the nitty gritty, but in this particular case, the computer only has 22 bits to use to store the "smaller than 1" part of the number.
Which forces me to make a slight retraction, the smallest increment is actuall 0.0000002384185791015625 , I forgot about the bit needed to store the whole 3.
*edit*
And you could make the two cout << endl; to cout << endl << endl; <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
or cout << "\n\n";
*edit*
And you could make the two cout << endl; to cout << endl << endl; <!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->
or cout << "\n\n"; <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
Eh. I prefer endl since it flushes youre buffer.
Yeah, it's in my version.
const double pi = whatever;
It makes your code more readable if somebody else ever has to look at it, yet it shouldn't have any affect on your run-time performance (it will just take a tiny fraction of an extra second to compile).
If something happens down the line, say using a new compiler or something changes in the language, your code will still work, rather than breaking into an undecipherable mass of numbers.
As for the precision of pi... well in practical terms, you don't need a greater degree of precision than you have the radius in. Nevertheless, if you want a really, really high degree of precision you could be naughty and use the preprocessor. I don't think there's a limit on the precision of <span style='font-family:Courier'>#define</span>'d constants... actually, come to think of it, the compiler [edit]<s>probably uses the biggest floating point variable it can, when it goes to work with it</s> uses <span style='font-family:Courier'>double</span>. Just remembered [/edit]. So you might as well use a <span style='font-family:Courier'>const long double</span>.