N
Nate Bargmann
I am working on a function that takes degrees, minutes, seconds
coordinates and converts them to decimal representation. Traditionally,
in DMS notation the '-' sign, to indicate west longitude or north
latitude, precedes the degree value, e.i. -96° 59' 59". The float type
is capable of carrying a signed ZERO for the purpose of representing
between 0 and 1 degree west or south.
My problem comes from trying to select the correct algorithm for computing
a positive or negative value. To illustrate, here is the function:
double dms2dec(float degrees, int minutes, double seconds) {
if (degrees < 0 || degrees == -0.0) {
return (double)degrees - (double)minutes/60. - seconds/3600.;
} else {
return (double)degrees + (double)minutes/60. + seconds/3600.;
}
}
This function has an error in that it treats all instances of degrees,
whether -0.0 or 0.0, as negative and executes the first statement. If
degrees is less than or equal to -1 or greater than or equal to 1, the
function returns the expected result. If degrees is greater than -1 but
less than 0.0 the calculation is correct. When degrees is greater than
0.0 but less than 1, the error occurs.
My question, how can I properly test the degrees argument and make a
correct decision based on whether degrees is -0.0 or 0.0? Perhaps the
best solution involves the use of copysign() to store the sign, performing
the calculation as a positive value then signing the result, if necessary.
What do the gurus think?
- Nate >>
coordinates and converts them to decimal representation. Traditionally,
in DMS notation the '-' sign, to indicate west longitude or north
latitude, precedes the degree value, e.i. -96° 59' 59". The float type
is capable of carrying a signed ZERO for the purpose of representing
between 0 and 1 degree west or south.
My problem comes from trying to select the correct algorithm for computing
a positive or negative value. To illustrate, here is the function:
double dms2dec(float degrees, int minutes, double seconds) {
if (degrees < 0 || degrees == -0.0) {
return (double)degrees - (double)minutes/60. - seconds/3600.;
} else {
return (double)degrees + (double)minutes/60. + seconds/3600.;
}
}
This function has an error in that it treats all instances of degrees,
whether -0.0 or 0.0, as negative and executes the first statement. If
degrees is less than or equal to -1 or greater than or equal to 1, the
function returns the expected result. If degrees is greater than -1 but
less than 0.0 the calculation is correct. When degrees is greater than
0.0 but less than 1, the error occurs.
My question, how can I properly test the degrees argument and make a
correct decision based on whether degrees is -0.0 or 0.0? Perhaps the
best solution involves the use of copysign() to store the sign, performing
the calculation as a positive value then signing the result, if necessary.
What do the gurus think?
- Nate >>