Working Out A Cirles Circumferance In C++

DY357LXDY357LX Playing since day 1. Still can't Comm.England Join Date: 2002-10-27 Member: 1651Members, Constellation
<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-->

Comments

  • OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
    edited August 2004
    move using namespace std; to the global.

    *edit*

    And you could make the two cout << endl; to cout << endl << endl;
  • DY357LXDY357LX Playing since day 1. Still can&#39;t Comm. England Join Date: 2002-10-27 Member: 1651Members, Constellation
    Well I was only using one function so
    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-->
  • brute_forcebrute_force Join Date: 2003-10-04 Member: 21433Members, Constellation
    Personally, I'd have written it like this:

    <!--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-->
  • [WHO]Them[WHO]Them You can call me Dave Join Date: 2002-12-11 Member: 10593Members, Constellation
    circumference is 2*pi*r
  • Dr_ShaggyDr_Shaggy Join Date: 2002-09-26 Member: 1340Members, Constellation
    I believe if you include math.h then you can use M_PI as the pi value, its already in math.h although i dont know how accurate that is.


    Or cmath, whichever is better, i can never remember.
  • Crono5Crono5 Join Date: 2003-07-22 Member: 18357Members
    edited August 2004
    <!--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-->#include <iostream>
    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.
  • taboofirestaboofires Join Date: 2002-11-24 Member: 9853Members
    Pffft, console apps are the way to go when writing an app for yourself. Heck, I write a console version of any input/output just so I can more easily script testing.
  • [WHO]Them[WHO]Them You can call me Dave Join Date: 2002-12-11 Member: 10593Members, Constellation
    that, and console apps can be written entirely from memorizable commands.

    A windows app always takes some online research, cutting/pasting, and bug testing.
  • BaconTheoryBaconTheory Join Date: 2003-09-06 Member: 20615Members
    I could be wrong, but don't the numbers of Pi go on forever?
  • [WHO]Them[WHO]Them You can call me Dave Join Date: 2002-12-11 Member: 10593Members, Constellation
    <!--QuoteBegin-Skidzor+Aug 15 2004, 06:46 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (Skidzor @ Aug 15 2004, 06:46 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> I could be wrong, but don't the numbers of Pi go on forever? <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
    Yes, a lot of numbers go on forever. The world has accepted reasonable approximations
  • NumbersNotFoundNumbersNotFound Join Date: 2002-11-07 Member: 7556Members
    <!--QuoteBegin-[WHO]Them+Aug 15 2004, 06:35 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> ([WHO]Them @ Aug 15 2004, 06:35 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> circumference is 2*pi*r <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
    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.
  • [WHO]Them[WHO]Them You can call me Dave Join Date: 2002-12-11 Member: 10593Members, Constellation
    edited August 2004
    The compiler will figure it out, but yes, it probably will, the smallest increment of a float when it starts with 3.XXX is 0.00000011920928955078125 , so the chances of it getting the given pi exactly right are nill, but providing more precision gives the compiler a better idea of what to try to match.
  • NumbersNotFoundNumbersNotFound Join Date: 2002-11-07 Member: 7556Members
    <!--QuoteBegin-[WHO]Them+Aug 15 2004, 08:55 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> ([WHO]Them @ Aug 15 2004, 08:55 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> The compiler will figure it out, but yes, it probably will, the smallest increment of a float when it starts with 3.XXX is 0.00000011920928955078125 , so the chances of it getting the given pi exactly right are nill, but providing more precision gives the compiler a better idea of what to try to match. <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
    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.
  • [WHO]Them[WHO]Them You can call me Dave Join Date: 2002-12-11 Member: 10593Members, Constellation
    <!--QuoteBegin-404NotFound+Aug 15 2004, 07:01 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (404NotFound @ Aug 15 2004, 07:01 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> <!--QuoteBegin-[WHO+--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> ([WHO)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->Them,Aug 15 2004, 08:55 PM] The compiler will figure it out, but yes, it probably will, the smallest increment of a float when it starts with 3.XXX is 0.00000011920928955078125 , so the chances of it getting the given pi exactly right are nill, but providing more precision gives the compiler a better idea of what to try to match. <!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->
    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.
  • SwiftspearSwiftspear Custim tital Join Date: 2003-10-29 Member: 22097Members
    You can use a variable that has twice the data storage of a float if you want, for the life of me I can't remember what it is called though... A 'double' maby?
  • BlackMageBlackMage [citation needed] Join Date: 2003-06-18 Member: 17474Members, Constellation
    <!--QuoteBegin-OttoDestruct+Aug 15 2004, 06:21 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (OttoDestruct @ Aug 15 2004, 06:21 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> move using namespace std; to the global.

    *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";
  • OttoDestructOttoDestruct Join Date: 2002-11-08 Member: 7790Members
    <!--QuoteBegin-Black Mage+Aug 15 2004, 09:23 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (Black Mage @ Aug 15 2004, 09:23 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> <!--QuoteBegin-OttoDestruct+Aug 15 2004, 06:21 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (OttoDestruct @ Aug 15 2004, 06:21 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> move using namespace std; to the global.

    *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.
  • Crono5Crono5 Join Date: 2003-07-22 Member: 18357Members
    <!--QuoteBegin-Swiftspear+Aug 15 2004, 09:13 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (Swiftspear @ Aug 15 2004, 09:13 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> You can use a variable that has twice the data storage of a float if you want, for the life of me I can't remember what it is called though... A 'double' maby? <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
    Yeah, it's in my version.

    const double pi = whatever;
  • TheWizardTheWizard Join Date: 2002-12-11 Member: 10553Members, Constellation
    22/7 is a generally accepted approximation of PI
  • taboofirestaboofires Join Date: 2002-11-24 Member: 9853Members
    It's still a good habit to use built-in constants. Particularly for things like "TRUE" = 1 (simplest example) or window handler constants, which your compiler will fill in for you from the headers.

    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.
  • SoulSkorpionSoulSkorpion Join Date: 2002-04-12 Member: 423Members
    edited August 2004
    It's considered good practise to make constants constant, and to write the identifiers in all caps <!--emo&;)--><img src='http://www.unknownworlds.com/forums/html//emoticons/wink-fix.gif' border='0' style='vertical-align:middle' alt='wink-fix.gif' /><!--endemo-->.

    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>.
Sign In or Register to comment.