Radians to degrees, minutes and seconds

Hi there,

I'd like to know if I convert radians to degrees, minutes and seconds in the right way. Those 3 values have to be in integral type (int).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    double radians;

    cin >> radians;

    cout << "Degrees - " << static_cast<int>(180 * radians / 3.14)
         << "Minutes - " << static_cast<int>(180 * radians / 3.14 * 60)
         << "Seconds - " << static_cast<int>(180 * radians / 3.14 * 3600);

    return EXIT_SUCCESS;
}


Is it correct code ?
Looks to me like you're calculating the total number of minutes in the angle, and then you're starting again and calculating the total number of minutes, and then you're starting again and calculating the total number of seconds. In effect, you are converting the angle three times, into three different units.

It's like saying a tonnes is a thousand kilograms, and then saying it's also a million grammes. These are both true, but it would be wrong to say one tonne is a thousand kilogrammes plus a million grammes.

Did you meant to convert an angle in radians into a single value; that is, the same angle in degrees-minutes-seconds? If you meant that, then your code above is wrong and you need to think again.

You mean that this is a better solution ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    double radians, temp;

    cin >> radians;

    temp = 180 * radians / 3.14;

    cout << "Degrees - " << static_cast<int>(temp)
         << "Minutes - " << static_cast<int>(temp * 60)
         << "Seconds - " << static_cast<int>(temp * 3600);

    return EXIT_SUCCESS;
}


Did you meant to convert an angle in radians into a single value; that is, the same angle in degrees-minutes-seconds?


Yes, I did. If the code above is still wrong, then I don't know what I should do.
You should first turn the input radians into a number of degrees.

That will give you some value in degrees, which will almost certainly not be a whole number. That is, it will be a decimal.

For example, say it comes out to 89.35 degrees.

So, you know the degrees section will be 89. What will the minutes section be? If you simply do as you are at the moment, which is taking the number of degrees and multiplying it by 60, you will get

89.35 * 60 = 5361 minutes.

Hang on, there's only sixty minutes in a degree! What's gone wrong? What's gone wrong is that you've calculated the number of minutes in 89.35 degrees, where you meant to take the number of minutes in 0.35 degrees.

The same for the seconds. You're calculating the number of seconds in 89.35 degrees, which will be lots and lots.

The algorithm you need is:

Turn radians into degrees. Will be a decimal.
The bit before the decimal is the number of degrees.

Now, take the bit after the decimal. There are sixty minutes in a degree, so if you multiply it by 60, you'll get the number of minutes in that amount of degrees.

For example, 0.5 degrees is half a degree, which is 30 minutes. 0.5 * 60=30.
For further example, 0.25 degrees is a quarter of a degree; 15 minutes. 0.25 * 60 =15

So now you've got the number of minutes. It will probably be a decimal. Output the whole number bit as the number of minutes.

Now, take the bit after the decimal. There are sixty seconds in a minutes, so if you multiply it by 60, you'll get the number of seconds in that amount of minutes.

For example, 0.5 minutes is half a minutes, which is 30 seconds. 0.5 * 60=30.
For further example, 0.25 minutes is a quarter of a minutes; 15 seconds. 0.25 * 60 =15

So now you've got the number of seconds.

Last edited on
You need to use double or float constructors in order to attain a degree of accuracy, if you use type int, it will essentially round the value off to the nearest integer. That way you won't have to static_cast your data to type int
Last edited on
Topic archived. No new replies allowed.