What is the name of the operation where you find the "closeness" of 2 values from 0 to 1?

47 Views Asked by At

This type of function comes up every now and then during my projects. Often and in performance critical parts enough that I'd like to learn more about it and see how others have implemented it.

Essentially, the purpose is to find out how linearly close 2 values are on a rotating scale of 0 to 1

For example:

0.5 and 0.5 = 1, maximum close
0.5 and 0.4 = 0.8, pretty close
0.5 and 0 = 0, least close
0.5 and 1 = 0, also least close
0.8 and 0.5 = 0.4
0.8 and 0.3 = 0

etc.

My implementation in js is as follows:

find_closeness(pivot, value) {
    return 1 - Math.min(Math.abs(value - pivot), Math.abs(value - pivot - 1), Math.abs(value - pivot + 1)) * 2;
}

But I'm sure theres a more efficient way to do it, math isn't my strong suit. I call the variables pivot and value because thats how I visualize it.

What is this operation called?

EDIT: previously posted implementation had a bug, this older version still works though

2

There are 2 best solutions below

1
On

This seems to just be equal to $$1-2\times\text{Math.Min}(\text{Math.Abs}(\text{pivot}-\text{value}),0.5)$$ but I'm confused by your example of $0.8,0.5\to0.6\ne0.4$.

0
On

I don't think that there's a name for this in mathematics -- we tend to give names to distance functions rather than closeness functions, for instance.

The underlying distance function ($0$ when two points are the same, $1$ when they're as far apart as possible) is twice the ordinary distance on a circle of circumference $1$ (i.e., you take your interval $[0, 1]$ and roll it up into a circle, and then measure "closest" distance along the circle).

As for efficient computation, what you've got now is just about as efficient as it can possibly be, because there pretty much has to be an "if" in there somewhere to swap between the two possible choices for which route to take around the circle.

I suppose that if you wanted to make up a name for it, you could call it "clock distance" or something like that, but I wouldn't bother doing so.