Thread: DD to DMS

  1. #1
    Registered User
    Join Date
    May 2021
    Posts
    3

    Question DD to DMS

    Can i know how to write C code for converting longitude and latitude from DD to DMS and also the cardinal direction? I have no idea on it.

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    Do you have some test data and results you can share?

  3. #3
    Registered User
    Join Date
    May 2021
    Posts
    3
    no...my lecturer just asks me to do this...no more info

  4. #4
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    I've finally got to the point where I no longer trust floating point - because some things are a little unpredictable, and "float" is only accurate to 25 bits, and just degrees, minutes and seconds can use up about 20 of those.

    So I would suggest you:

    - first ensure that the input is in the range expected +/-90.0 for latitude, +/-180.0 for longitude. fmod() might come in handy here.

    - convert your number to an 32-bit integer, with the unit of 1000 arc-seconds - multiply your floating point number by (60*60*1000) and add 0.5, and assign it to an 32-bit integer. 180.0 degrees will become 648,000,000 - and that fits fine in a 32-bit integer.

    - The you can use the '%' and '/' operators to extract:

    -- thousandths of arc seconds (use % and / by 1000)
    -- arc seconds (use % and / by 60)
    -- arc minutes (use % and / by 60)
    -- degrees (the remainder left over)

    The sign of the degrees will tell you if it is North, South (for latitude) or East or West (for longitude).
    Last edited by hamster_nz; 05-09-2021 at 02:48 AM.

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by hamster_nz View Post
    I've finally got to the point where I no longer trust floating point - because some things are a little unpredictable, and "float" is only accurate to 25 bits, and just degrees, minutes and seconds can use up about 20 of those.
    float has 24 bits of precision, not 25... But you can use double (53 bits). In an Intel or ARM processor there's no difference in performance (only double has "double" the size, in comparison with float, and depending on the structures you can get a more severe cache miss effects).

    Anyway... I don't trust floating point as well and prefer using integers. Not because of precision, but because of accuracy. Some simple values cannot be exactly represented in floating point, such as 0.1.

  6. #6
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338

    Thumbs up

    from DD to DMS
    Code:
    #include <stdio.h>
    
    
    double DD = 0.0, seconds, remain ;
    int degrees = 0, minutes = 0;
    
    
    int main() {        
            
        DD = 13.5423; 
        
        degrees = (int)DD; 
         remain = ((DD-degrees)*60);
        minutes = (int)(remain);
        seconds = ((remain-minutes)*60);
    
    
        printf( "DD:\t %f \n", DD );
        printf( "DMS:\t %i° %i' %.2f\" \n", degrees, minutes, seconds);
    
    
        return 0;
    }
    Last edited by Structure; 05-10-2021 at 10:08 AM.
    "without goto we would be wtf'd"

Popular pages Recent additions subscribe to a feed

Tags for this Thread