Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Convert DegMinSec to Decimal Degrees

Hello,

I once again found a little time to toy with C Programming. I have applied the helpful advice from this message board since my last post. I used the utility on http://www.fcc.gov/mb/audio/bickel/DDDMMSS-decimal.html to check my output. I was pleased that my program output matched that of the utility except on my final test input of 1 degree 1 minute 1 second. Please see the comments at the end of my program for details.

/* Name: TODECDEG.C */
/* Purpose: Convert Degrees Minutes Seconds to Decimal Degrees */
/* Author: MarkTheSpark */
/* Date: November 19, 2007 */
/* Compiler: Pacific C for MS-DOS, v7.51 */

#include

main()
{
double deg_min_sec = 0, decimal_deg = 0;
double mind = 0, secd = 0;
int deg = 0, min = 0, sec = 0;

printf("Enter Degrees Minutes Seconds: ");
scanf("%lf", &deg_min_sec);


deg = deg_min_sec; /* conversion during assignment */
min = (deg_min_sec - deg) * 100;
sec = ((deg_min_sec - deg) * 100 - min) * 100;

mind = (double) min / 60; /* http://c-faq.com/expr/truncation1.html */
secd = (double) sec / 3600;
decimal_deg = deg + mind + secd;

printf("Decimal Degrees: %f

", decimal_deg);
return 0;
}

/* Sample output:

Enter Degrees Minutes Seconds: 1.0101
Decimal Degrees: 1.016667

Press any key to continue ...

*/

/* Note:

This conversion does not agree with
http://www.fcc.gov/mb/audio/bickel/DDDMMSS-decimal.html.
Their utility yields 1.016944. Why?

*/

Any help would be greatly appreciated!

Regards,

Mark

Comments

  • zibadianzibadian Posts: 6,349Member
    : Hello,
    :
    : I once again found a little time to toy with C Programming. I have
    : applied the helpful advice from this message board since my last
    : post. I used the utility on
    : http://www.fcc.gov/mb/audio/bickel/DDDMMSS-decimal.html to check my
    : output. I was pleased that my program output matched that of the
    : utility except on my final test input of 1 degree 1 minute 1 second.
    : Please see the comments at the end of my program for details.
    :
    : /* Name: TODECDEG.C */
    : /* Purpose: Convert Degrees Minutes Seconds to Decimal Degrees */
    : /* Author: MarkTheSpark */
    : /* Date: November 19, 2007 */
    : /* Compiler: Pacific C for MS-DOS, v7.51 */
    :
    : #include
    :
    : main()
    : {
    : double deg_min_sec = 0, decimal_deg = 0;
    : double mind = 0, secd = 0;
    : int deg = 0, min = 0, sec = 0;
    :
    : printf("Enter Degrees Minutes Seconds: ");
    : scanf("%lf", &deg_min_sec);
    :
    :
    : deg = deg_min_sec; /* conversion during assignment */
    : min = (deg_min_sec - deg) * 100;
    : sec = ((deg_min_sec - deg) * 100 - min) * 100;
    :
    : mind = (double) min / 60; /*
    : http://c-faq.com/expr/truncation1.html */
    : secd = (double) sec / 3600;
    : decimal_deg = deg + mind + secd;
    :
    : printf("Decimal Degrees: %f

    ", decimal_deg);
    : return 0;
    : }
    :
    : /* Sample output:
    :
    : Enter Degrees Minutes Seconds: 1.0101
    : Decimal Degrees: 1.016667
    :
    : Press any key to continue ...
    :
    : */
    :
    : /* Note:
    :
    : This conversion does not agree with
    : http://www.fcc.gov/mb/audio/bickel/DDDMMSS-decimal.html.
    : Their utility yields 1.016944. Why?
    :
    : */
    :
    : Any help would be greatly appreciated!
    :
    : Regards,
    :
    : Mark
    :
    Your maths look sound. Perhaps there is some rounding error. You should check that by printing out the various intermediate results. That way you can check if the deg, min, sec are indeed 1 and if the mind, secd have the correct values.
    I've checked the calculation using Excel and there is a rounding error in the calculation of the sec variable. This causes it to be less than 1.
  • LundinLundin Posts: 3,711Member
    The problem is floating point inaccuracy. http://c-faq.com/fp/strangefp.html

    Because of that, you get a bug on this line:

    sec = ((deg_min_sec - deg) * 100 - min) * 100;

    First, it does deg_min_sec-deg.

    - The result is not 0.0101 as expected, but rather 0.0100999999 (nine nine... and so on).
    - 0.0100999999 is multiplied by 100, we get 1.00999999.
    - Subtract 1, we get 0.00999999.
    - Multiply 0.009999 with 100 = 0.99999, truncate to integer = 0.

    The real problem in your program is not this but the unecessarily strange input. Is there a reason you aren't doing as they do on that web site?

    This works:

    [code]#include

    int main() /* in ANSI/ISO C, main() must return int */
    {
    double deg;
    double min;
    double sec;
    double decimal_deg;

    printf("Enter degrees: ");
    scanf("%lf", &deg);
    getchar(); /* getchar takes care of the line feed character */

    printf("Enter minutes: ");
    scanf("%lf", &min);
    getchar();

    printf("Enter seconds: ");
    scanf("%lf", &sec);
    getchar();

    min /= (double)60;
    sec /= (double)3600;

    decimal_deg = deg + min + sec;

    printf("
    Decimal Degrees: %lf
    ", decimal_deg);

    return 0;
    }

    [/code]
  • MarkTheSparkMarkTheSpark Posts: 7Member
    : : Hello,
    : :
    : : I once again found a little time to toy with C Programming. I have
    : : applied the helpful advice from this message board since my last
    : : post. I used the utility on
    : : http://www.fcc.gov/mb/audio/bickel/DDDMMSS-decimal.html to check my
    : : output. I was pleased that my program output matched that of the
    : : utility except on my final test input of 1 degree 1 minute 1 second.
    : : Please see the comments at the end of my program for details.
    : :
    : : /* Name: TODECDEG.C */
    : : /* Purpose: Convert Degrees Minutes Seconds to Decimal Degrees */
    : : /* Author: MarkTheSpark */
    : : /* Date: November 19, 2007 */
    : : /* Compiler: Pacific C for MS-DOS, v7.51 */
    : :
    : : #include
    : :
    : : main()
    : : {
    : : double deg_min_sec = 0, decimal_deg = 0;
    : : double mind = 0, secd = 0;
    : : int deg = 0, min = 0, sec = 0;
    : :
    : : printf("Enter Degrees Minutes Seconds: ");
    : : scanf("%lf", &deg_min_sec);
    : :
    : :
    : : deg = deg_min_sec; /* conversion during assignment */
    : : min = (deg_min_sec - deg) * 100;
    : : sec = ((deg_min_sec - deg) * 100 - min) * 100;
    : :
    : : mind = (double) min / 60; /*
    : : http://c-faq.com/expr/truncation1.html */
    : : secd = (double) sec / 3600;
    : : decimal_deg = deg + mind + secd;
    : :
    : : printf("Decimal Degrees: %f

    ", decimal_deg);
    : : return 0;
    : : }
    : :
    : : /* Sample output:
    : :
    : : Enter Degrees Minutes Seconds: 1.0101
    : : Decimal Degrees: 1.016667
    : :
    : : Press any key to continue ...
    : :
    : : */
    : :
    : : /* Note:
    : :
    : : This conversion does not agree with
    : : http://www.fcc.gov/mb/audio/bickel/DDDMMSS-decimal.html.
    : : Their utility yields 1.016944. Why?
    : :
    : : */
    : :
    : : Any help would be greatly appreciated!
    : :
    : : Regards,
    : :
    : : Mark
    : :
    : Your maths look sound. Perhaps there is some rounding error. You
    : should check that by printing out the various intermediate results.
    : That way you can check if the deg, min, sec are indeed 1 and if the
    : mind, secd have the correct values.
    : I've checked the calculation using Excel and there is a rounding
    : error in the calculation of the sec variable. This causes it to be
    : less than 1.

    Thanks for the reply!
  • MarkTheSparkMarkTheSpark Posts: 7Member
    : The problem is floating point inaccuracy.
    : http://c-faq.com/fp/strangefp.html
    :
    : Because of that, you get a bug on this line:
    :
    : sec = ((deg_min_sec - deg) * 100 - min) * 100;
    :
    : First, it does deg_min_sec-deg.
    :
    : - The result is not 0.0101 as expected, but rather 0.0100999999
    : (nine nine... and so on).
    : - 0.0100999999 is multiplied by 100, we get 1.00999999.
    : - Subtract 1, we get 0.00999999.
    : - Multiply 0.009999 with 100 = 0.99999, truncate to integer = 0.
    :
    : The real problem in your program is not this but the unecessarily
    : strange input. Is there a reason you aren't doing as they do on that
    : web site?
    :
    : This works:
    :
    : [code]: #include
    :
    : int main() /* in ANSI/ISO C, main() must return int */
    : {
    : double deg;
    : double min;
    : double sec;
    : double decimal_deg;
    :
    : printf("Enter degrees: ");
    : scanf("%lf", &deg);
    : getchar(); /* getchar takes care of the line feed character */
    :
    : printf("Enter minutes: ");
    : scanf("%lf", &min);
    : getchar();
    :
    : printf("Enter seconds: ");
    : scanf("%lf", &sec);
    : getchar();
    :
    : min /= (double)60;
    : sec /= (double)3600;
    :
    : decimal_deg = deg + min + sec;
    :
    : printf("
    Decimal Degrees: %lf
    ", decimal_deg);
    :
    : return 0;
    : }
    :
    : [/code]:

    Thanks for the reply! I read http://c-faq.com/fp/strangefp.html concerning the floating point inaccuracy. I also found What Every Computer Scientist Should Know About Floating-Point Arithmetic, by David Goldberg, published in the March, 1991 issue of Computing Surveys on the Internet. It is interesting but too advanced for me. The strange input I'm using for my program is meant to save key strokes. I want to write a program that solves triangles (SAS, SSS, ASA) and also a coordinate geometry program (rotation). I couldn't imagine entering 36 Deg - 52 Min - 12 Sec as 36 [Enter], 52 [Enter] and 12 [Enter] instead of just 36.5212 [Enter}.
  • LundinLundin Posts: 3,711Member
    If you want to save keystrokes, take the input from a file instead.

    Or if you [italic]must[/italic] read the whole input at once, user-unfriendly and confusing as it is, read it as a string and then parse through it.
Sign In or Register to comment.