Fast comparison with a tolerance
If for all $|r - s| &lt; 1$ it is the case that $K(r) = K(s)$ then $K$ is constant (exercise).

What you are asking is impossible.

One thing which is possible is to compare keys with three rather than two possible outcomes:


If $K(r)$ is close to $K(s)$ then $|r - s| &lt; 1$.
If $K(r)$ is far from $K(s)$ then $|r - s| \geq 1$.
Otherwise, we don't know.


Of course, the aim is that the third case happens as rarely as possible. One simple way to achieve this is to choose a random $x$ and some small (non-random) $\epsilon &gt; 0$, and to use
$$
K(r) = \lfloor r/\epsilon + x \rfloor.
$$
Under this definition, the only uncertainty is for $r,s$ at distance $(1-\epsilon,1+\epsilon)$. The random component mitigates even this uncertainty, albeit only by a little.