From 89dc41dfa35e1c2144c89212e20cdd043eeaa0a3 Mon Sep 17 00:00:00 2001 From: Christoph Berg Date: Mon, 16 Oct 2023 13:02:46 +0200 Subject: [PATCH] spoint_dwithin: Use FPle, add tests --- expected/points.out | 16 ++++++++++++++++ sql/points.sql | 9 +++++++++ src/point.c | 2 +- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/expected/points.out b/expected/points.out index 6fb3c17b..f3a0713a 100644 --- a/expected/points.out +++ b/expected/points.out @@ -666,3 +666,19 @@ SELECT '( 0h 2m 30s , -90d 0m 0s)'::spoint<->'( 12h 2m 30s , -90d 0m 0s)'::spoin 0 (1 row) +-- spoint_dwithin function ---------- +SELECT a, b, radius, a <-> b AS "<->", spoint_dwithin(a, b, radius) +FROM (VALUES + ('(0, 0)'::spoint, '(0, 0)'::spoint, 0), + ('(0, 0)', '(0, 1)', 1), + ('(0, 0)', '(0.1, 0.1)', 0.14), + ('(0, 0)', '(0.1, 0.1)', 0.15) + ) sub (a, b, radius); + a | b | radius | <-> | spoint_dwithin +---------+-------------+--------+-----------------+---------------- + (0 , 0) | (0 , 0) | 0 | 0 | t + (0 , 0) | (0 , 1) | 1 | 1 | t + (0 , 0) | (0.1 , 0.1) | 0.14 | 0.1413032986961 | f + (0 , 0) | (0.1 , 0.1) | 0.15 | 0.1413032986961 | t +(4 rows) + diff --git a/sql/points.sql b/sql/points.sql index 5e63260a..b34ae6ec 100644 --- a/sql/points.sql +++ b/sql/points.sql @@ -240,3 +240,12 @@ SELECT '( 0h 2m 30s , 90d 0m 0s)'::spoint<->'( 12h 2m 30s , 90d 0m 0s)'::spoint; SELECT '( 0h 2m 30s , -90d 0m 0s)'::spoint<->'( 12h 2m 30s , -90d 0m 0s)'::spoint; +-- spoint_dwithin function ---------- + +SELECT a, b, radius, a <-> b AS "<->", spoint_dwithin(a, b, radius) +FROM (VALUES + ('(0, 0)'::spoint, '(0, 0)'::spoint, 0), + ('(0, 0)', '(0, 1)', 1), + ('(0, 0)', '(0.1, 0.1)', 0.14), + ('(0, 0)', '(0.1, 0.1)', 0.15) + ) sub (a, b, radius); diff --git a/src/point.c b/src/point.c index c3c7aaaa..d6e42371 100644 --- a/src/point.c +++ b/src/point.c @@ -232,7 +232,7 @@ spherepoint_dwithin(PG_FUNCTION_ARGS) float8 within = PG_GETARG_FLOAT8(2); float8 dist = spoint_dist(p1, p2); - PG_RETURN_BOOL(dist <= within); + PG_RETURN_BOOL(FPle(dist, within)); }