Skip to content

Fix acos() NaN in TNTGeoSearch #357

@YannLe

Description

@YannLe

Hi,

I think I’ve found a small issue in TNTGeoSearch::buildQuery().

When using the findNearest function, the distance value calculated in SQL can sometimes be slightly greater than 1.0 if the two coordinates are very close (maybe due to floating-point precision errors?).

Since acos() is only defined for values in the range [-1, 1], this causes it to return NaN, and the nearest location is excluded from the final results.

Suggested fix: Clamp $location['distance'] between -1.0 and 1.0 before calling acos() here:
https://github.com/teamtnt/tntsearch/blob/master/src/TNTGeoSearch.php#L71

$val = max(-1.0, min(1.0, (float)$location['distance']));
$location['distance'] = acos($val) * $this->earthRadius;

Happy to open a PR with the fix if it’s helpful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions