clamped arccosine to [-1,1] to prevent rounding errors in geoDistance

This commit is contained in:
Gani Georgiev committed 2026-09-06 00:45:12 +03:00
1 parent dbea21f1b2
commit df4e6eeb35
4 files changed
+63 -28

No files matched your search

+5 -2
View File
@@ -49,11 +49,14 @@ var TokenFunctions = map[string]func(
return &ResolverResult{
NullFallback: NullFallbackDisabled,
Identifier: `(6371 * acos(` +
// the clamping is to prevent floating point rounding errors for values like
// "1.0002" that could occur for example when comparing identical points
// (see the NULL note for arccosine in https://sqlite.org/lang_mathfunc.html#overview)
Identifier: `(6371 * acos(min(1, max(-1, ` +
`cos(radians(` + latA + `)) * cos(radians(` + latB + `)) * ` +
`cos(radians(` + lonB + `) - radians(` + lonA + `)) + ` +
`sin(radians(` + latA + `)) * sin(radians(` + latB + `))` +
`))`,
`))))`,
Params: mergeParams(resolvedArgs[0].Params, resolvedArgs[1].Params, resolvedArgs[2].Params, resolvedArgs[3].Params),
}, nil
},