c - Trouble converting latitude/longtiude to DMS and back -
i've written 2 functions convert latitude , longitude , forth components degrees, minutes, , seconds. far method dectodms() goes, math seems okay positive/negative signs off on minutes , seconds.
for dmstodec, i'm barely off in calculations , i'm not quite sure error lies. here both code samples:
int dmstodec(double *deg, int degrees, int minutes, double seconds) { //test invalid inputs if (deg == null) { return 1; } if (*deg < -180 || *deg > 180) { return 1; } // formula degrees = degrees + minutes/60 + seconds/3600; double m; double s; double d; d = (double) degrees; m = (double) minutes / 60; s = (double) seconds / 3600; *deg = d + m + s; printf("dms dec %lf\n", *deg); return 0; } int dectodmsint *degrees, int *minutes, double *seconds, double deg) { if (degrees == null || minutes == null || seconds == null) { return 1; } if (deg < -180 || deg > 180) { return 1; } *degrees = deg; deg = deg - trunc(deg); // multiply decimal 60; deg *= 60; *minutes = deg; // drop integer deg = deg - trunc(deg); deg *= 60; *seconds = deg; return 0; }
how about:
*degrees = deg; deg = deg - trunc(deg); // minutes , seconds >= 0 if (deg < 0) deg *= -1; // multiply decimal 60 deg *= 60; *minutes = deg; // drop integer deg = deg - trunc(deg); deg *= 60; *seconds = deg;