Skip to content

Math.acos may return NaN #19

@daisukes

Description

@daisukes

I found a bug that sometimes RouteSearchBean returns a null route. (0.0000001 difference can change the result)
The root problem is that Math.acos returns NaN when dist is a bit greater than 1.0
So the dist should be truncated within -1.0 to 1.0.

--- a/MapService/src/hulop/hokoukukan/bean/RouteSearchBean.java
+++ b/MapService/src/hulop/hokoukukan/bean/RouteSearchBean.java
@@ -593,7 +593,6 @@ public class RouteSearchBean {
                double theta = deg2rad(point1[0] - point2[0]);
                double lat1 = deg2rad(point1[1]), lat2 = deg2rad(point2[1]);
                double dist = Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(theta);
+               dist = Math.min(1.0, Math.max(-1.0, dist));
                return METERS_PER_DEGREE * Math.acos(dist) * 180.0 / Math.PI;
        }

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions