Path: utzoo!attcan!uunet!ns-mx!iowasp.physics.uiowa.edu!maverick.ksu.ksu.edu!zaphod.mps.ohio-state.edu!wuarchive!mit-eddie!bloom-beacon!athena.mit.edu!phils From: phils@athena.mit.edu (Philip R. Thompson) Newsgroups: comp.lang.c Subject: Message-ID: <1990Oct20.215718.15519@athena.mit.edu> Date: 20 Oct 90 21:57:18 GMT Sender: daemon@athena.mit.edu (Mr Background) Reply-To: phils@athena.mit.edu (Philip R. Thompson) Organization: Massachusetts Institute of Technology Lines: 57 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 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@athena.mit.edu) < > Computer Resource Laboratory < > Department of Architecture and Urban Planning < > M.I.T., Rm 9-526 < > Cambridge, MA 02139 (617) 253-0782 < ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^