Thread: floating point values

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    12

    floating point values

    i am trying to do the following:

    program that reads two floating-point values representing angles and displays a message stating which angle has the greater tangent value. Note that the trigonometric functions in the math library expect angles in radians, not degrees. 360 degrees = 2 x pi radians, so 45 degrees would be about 0.785 radians.

    I have come up with code that takes in 2 numbers and displays the highest values. What I am stuck on is the calculations part. I am searching online and reading the books. Any help in this area will be great. Here is the code I have so far:

    Code:
    #include <iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    
    
    int main()
    
    {
    
    float x,y;
    
    cout<< "Enter the first angle: ";
    cin>> x;
    
    cout<< "Enter the second angle: ";
    cin>> y;
    
    
    if ( x > y )
    cout << " The highest angle is "<< x <<endl;
    
    else
    cout<< "The highest angle is "<< y <<endl;
    
    
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    All you have to do is compare tan(angle1) with tan(angle2). If tan(angle1) > tan(angle2) then output angle1 else output angle2.

    Note: you should also be #include'ing <cmath> if you are going to be calling the tan function in your code. Also, your angles would be better as doubles and not floats.
    "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

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    12
    i am not sure what you mean. dont i have to put in somewhere the formula to figure out which angle is larger? also i am not sure what you mean by tan(angle)

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by jmarsh56
    dont i have to put in somewhere the formula to figure out which angle is larger?
    Quote Originally Posted by jmarsh56
    i am trying to do the following:

    program that reads two floating-point values representing angles and displays a message stating which angle has the greater tangent value.
    I'm working off of what you said in the original post... which is finding the angle has the greater tangent value, not finding the greater angle. To do that you must call the tangent function which is called tan and pass into it the angle (in radians). So... as an example to display the tangent of an angle input by the user:

    Code:
    double angle;
    cout << "Enter an angle (in radians): ";
    cin >> angle;
    cout << "The tangent of " << angle << " radians is " << tan(angle) << '.' << endl;
    Output:
    Code:
    Enter an angle (in radians): .785398
    The tangent of .785398 radians is 1.
    "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
    Registered User
    Join Date
    Dec 2005
    Posts
    12
    thanks, i added that in with a few more lines i came up with and received the answer i was looking for. thanks again

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The remark about a double being better than a float was because doubles are much more accurate.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  2. checking for floating point number
    By ssharish2005 in forum C Programming
    Replies: 6
    Last Post: 10-18-2005, 08:14 PM
  3. floating point question
    By Eric Cheong in forum C Programming
    Replies: 8
    Last Post: 09-10-2004, 10:48 PM
  4. Floating point numbers in a binary file
    By frenchfry164 in forum C++ Programming
    Replies: 6
    Last Post: 07-31-2003, 10:04 AM
  5. Replies: 2
    Last Post: 09-10-2001, 12:00 PM