Thread: Circle Calculator Job

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    21

    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()
    {
        long int MenuChoice, Radius, Diameter, Circumference;
        const float Pi = 3.141592653589793238462633832795;
        
        //Menu starts here
        std::cout << "Menu- Find a cicle's-\n\n";
        std::cout << "1. Area\n";
        std::cout << "2. Circumference\n";
        //Menu ends here
        
        std::cout << "What would you like to do? ";
        std::cin >> MenuChoice;
        std::cin.ignore(80,'\n');
        
        //The users Menu choice on what he wants to find is evaluated here  
        if (MenuChoice == 1)
        {
                       
           std::cout << "\nWhat is the radius of the circle (in centimetres): ";
           std::cin >> Radius;
           std::cin.ignore(80,'\n');
           
           std::cout << "\nThe circle's area is: " << CircleArea(Radius, Pi) << " square cm.";
           
        }   
           
        if (MenuChoice == 2)
        {
        
           std::cout << "\nWhat is he radius of the circle: ";
           std::cin >> Radius;
           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)
    {
         
         return (Pi*Radius*Pi*Radius);
         
    }
    
    //This function finds a circles circumference
    float CircleCircumference(long int Radius, const float Pi)
    {
          
          return (2*Pi*Radius);
          
    }
    Last edited by ComDriver; 02-13-2005 at 12:11 PM.

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Good job, you should add different units instead of just centimeters and maybe different shapes.
    My computer is awesome.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    21

    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. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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;
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    >>return (Pi*Radius*Pi*Radius);

    Umm isn't it pi*r*r?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    21
    I know it's PieRaidusSquared but didn't know how to represent the Squared.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote 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:
        return (Pi*Radius*Radius);
    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.
    Last edited by Dave Evans; 02-13-2005 at 01:52 PM.

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    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:
    return (pi*(radius*radius))
    My computer is awesome.

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote 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:
    return (pi*(radius*radius))
    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:
    return Pi * Radius * Radius;
    The following code performs two multiplications

    Code:
    return (Pi * (Radius * Radius));
    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
    Last edited by Dave Evans; 02-13-2005 at 08:29 PM.

  10. #10
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by ComDriver
    const float Pi = 3.141592653589793238462633832795;
    Use the M_PI constant in <cmath>

    Quote 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.

    Quote 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:
    return (pi*(radius*radius))
    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.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  11. #11
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote 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. #12
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    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.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  13. #13
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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.
    Last edited by VirtualAce; 02-18-2005 at 04:13 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. circle problem
    By gunghomiller in forum C++ Programming
    Replies: 10
    Last Post: 07-14-2007, 06:40 PM
  2. Drawing circle into a bitmap file
    By brokensail in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2006, 01:26 AM
  3. Half of the circle
    By hdragon in forum Game Programming
    Replies: 14
    Last Post: 03-10-2006, 10:15 PM
  4. circle filling
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-08-2002, 05:37 AM
  5. point lies in circle?
    By cozman in forum Game Programming
    Replies: 3
    Last Post: 12-20-2001, 04:39 PM