[Math] Calculate percentage of how far a number is away from a given point in a range

percentages

so I have some range, let's say $ [a,b]$ for $a,b > 0 $. Now I'm given some number $c \in [a,b]$, to act as some 'target' number. My question is, if I have another number, $x \in [a,b]$, how can I find a percentage of how close $x$ is to $c$. Where if $x=c$, closeness = 100%, and the further $x$ is from $c$, the lower the percentage, regardless if $x$ is greater or less than $c$.

Eg:
For $a=0, b=10, c=5$

If $x=4$, closeness = 80%

If $x = 6$, closeness = 80% (not 120%)

I know I could do it with two functions, depending if x is < or > c, simple fractions, but we don't always know this.

Best Answer

Let $\phi$ denote the closeness function. If $\gamma$ is the point furthest away from $c$ in the interval (it could be $a$ or $b$), then we need $\phi(\gamma) = 0$ and $\phi(c) = 1$.

Let $d$ denote the farthest distance possible from $c$, $|\gamma - c|$. A simple function that does the job is $$\phi(x) = \frac{|d - |c - x||}{d}.$$

Related Question