Convert DegMinSec to Decimal Degrees

This is a discussion on Convert DegMinSec to Decimal Degrees within the C Programming forums, part of the General Programming Boards category; Convert DegMinSec to Decimal Degrees Hello, I once again found a little time to toy with C Programming. I have ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    8

    Convert DegMinSec to Decimal Degrees

    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.0101.
    Please see the comments at the end of my program for details.

    Code:
    /* Name: TODECDEG.C */
    /* Purpose: Convert Degrees Minutes Seconds to Decimal Degrees */
    /* Author: JacquesLeJock */
    /* Date: November 19, 2007 */
    /* Compiler: Pacific C for MS-DOS, v7.51 */
    
    #include <stdio.h>
    
    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\n\n", 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,

    Jacques

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    rounding errors are probably causing sec to be something like 0.99999999999998
    which is assigned to an int as 0 instead of 1.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    Consider using a single multiply by 10000 (not *100 then *100).
    Then you can stick to only using integer arithmetic to extract all the information you need.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    8
    Thanks for the reply NeonBlack and Salem!
    Last edited by JacquesLeJock; 11-22-2007 at 12:04 AM. Reason: Wanted to include names of responders.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. convert from Single to Decimal
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 06-11-2008, 07:13 AM
  2. how to convert decimal to hexa decimal in C/
    By kalamram in forum C Programming
    Replies: 4
    Last Post: 09-03-2007, 08:39 AM
  3. how to convert decimal number to ASCII code?
    By oie in forum C Programming
    Replies: 11
    Last Post: 11-03-2006, 06:19 PM
  4. Temperature conversion...
    By Onslaught in forum C Programming
    Replies: 3
    Last Post: 10-21-2005, 02:15 PM
  5. Replies: 10
    Last Post: 08-18-2005, 12:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21