Way to compare three numbers with an algorithm.

7.7k Views Asked by At

Say I have values

10, 3, 6

What would be a way of finding a subjective difference between them. Or in other words, how close they are to each-other.

So if i had

3, 2.5, 2.8; they would be close together in relation to one another.

3, 10, 10.5; would not be close together, despite two values being similar.

2

There are 2 best solutions below

7
On BEST ANSWER

Compute the standard deviation of the set.

In other words, let $m = \frac{1}{n} \sum_{i=1}^n x_i$ be the average of the set, then compute $$ \sigma = \sqrt{\frac{1}{n} \sum_{i=1}^n \left(x_i-m\right)^2} = \sqrt{\frac{1}{n} \sum_{i=1}^n x_i^2 - m^2} $$

It's nice that this will work for $n=1,2$ or even very large $n$.

UPDATE (thanks to BlueRaja - Danny Pflughoeft)

This is will handle a absolute difference reasonably. However, if you want a relative difference (e.g. $90,100,110$ should behave similar to $9,10,11$) - compute the relative standard deviation, dividing by the mean or the median of the set, i.e. $\sigma/m$.

1
On

I'm not completly sure if that is what you want, but, how about this?

Three positive numbers $x<y<z$ are "near" if $$\frac{z-x}{y}$$ is "small".