<None>

Philip R. Thompson phils at athena.mit.edu
Sun Oct 21 07:57:18 AEST 1990


Here's function that has annoying round-off errors.
Does anyone know of a better way to do this decimal
degree conversion?
I've tried lots of work arounds without much success,
so I'm now posting it here.  Any solutions are welcome.
This has been tried on a vax, dec and ibm/rt.

Thanks,
Philip
--------------

/* Convert Degrees, Minutes, Seconds number to decimal degrees */
/* Note: 38.30 degrees doesn't work. */

#include <stdio.h>

main()
{
    double value, DMSToDecimal();

    printf("Enter value (+-dd.mmss): ");
    while (scanf("%lf", &value)) {
        printf("dec: %lf\n", DMSToDecimal(value));
    	printf("Another value (+-dd.mmss): ");
    }
    exit(0);
}

double DMSToDecimal(deg)
double deg;
{
    double ideg, frac, minutes, seconds;
    double modf();

    frac = modf(deg, &ideg) * 100.0;
    seconds = modf(frac, &minutes) * 100.0;
    if ((minutes > 60.0) || (seconds > 60.0) || (minutes < 0.0) ||
            (seconds < 0.0)) {
        fprintf(stderr,
                "Bad input: %f degs %f minutes %f secs\n", ideg,
                minutes, seconds);
        return(0.0);
    }
    fprintf(stderr, "%G =  %G degs %G minutes %G secs\n", deg,
		 ideg, minutes, seconds);
    return (ideg + minutes / 60.0 + seconds / 3600.0);
}


-- 
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
> Philip R. Thompson   (phils at athena.mit.edu)                       <
> Computer Resource Laboratory                                      <
> Department of Architecture and Urban Planning                     <
> M.I.T., Rm 9-526                                                  <
> Cambridge, MA  02139       (617) 253-0782                         <
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^



More information about the Comp.lang.c mailing list