Subject: | Bug in (at least) Vincenty calculation |
Date: | Fri, 29 May 2009 23:01:18 -0400 |
To: | bug-GIS-Distance-Fast [...] rt.cpan.org |
From: | John Engelhart <john.engelhart [...] gmail.com> |
Hello,
There is a bug in
"GIS-Distance-Fast-0.02/lib/GIS/Distance/Formula/Vincenty/Fast.pm", line 88:
while( abs(lambda-lambda_pi) > 1e-12 && --iter_limit>0 ){
The problem is with the use of 'abs()'. abs() takes an int for an argument
and returns an int value (ie, works with int types, not double types). To
get the desired behavior, you need to use 'fabs()', which accepts and
returns double values. Because of the use of abs(), which converts the
difference to an int, the while() body effectively never iterates because
the int type is unable to represent the required precision. A few,
completely random tests would seem to indicate that after the first
iteration, the difference is ~ > 0.001, so the abs((int)(lambda-lambda_pi))
Show quoted text
> 1e-12 check is always true. :(
Also, I'd recommend using the math.h / C[89]9 standard definition for pi,
M_PI. For my platform, M_PI is 3.14159265358979323846264338327950288, which
is of greater precision than the supplied PI value
of 3.14159265358979323846.
A third recommendation is to turn all the integer constants in the source to
double constants (ie, 256 becomes 256.0). Using integer constant values
technically means something very slightly different according to the C
standard, so it's just good form to be explicit about it.