1

I'll be writing a program that compares two numbers and based on how different they are, perform, or not perfom an action. It would make sense to have one formula to get a standardized measure that I can use on all number sets.

I've tried each of these three commonly advised methods below, as well as a few other home-brewed variations.

x = (larger - smaller) / larger

x = (larger - smaller) / ((larger + smaller) / 2)

x = smaller / larger

Each of these methods have something in common which forms a problem for me. Each of these methods comes down to dividing something (left) by a reference (right). When the reference is close to zero, the resulting number x increases exponentially and fails to represent what I need it to. In cases where my program runs this formula, and this happens, my program is going to show an undesired outcome.

Is my approach of the problem wrong? Do I just need a different solution? Could someone tell me what am I looking at here?

  • There is no unique and objective definition of "different". The "methods" you are discussing are in fact alternative definitions of what "different" means. They may all be legitimate depending on circumstances. Which definition makes sense for you will depend on the meaning of the numbers and the implications of the choice, i.e., the action. Having a more precise idea of what kinds of numbers you expect may also help. – Christian Hennig Mar 23 '24 at 11:05
  • The question indeed does not clarify what 'different' is, let alone how 'different' the two numbers would have to be. The definition implied in "x = (larger - smaller) / ((larger + smaller) / 2)" seems the most useful to my challenge which I'd define as a magnitude of the difference between two numbers, relative to not one (such as just MAX) but actually both numbers. I'd like to get something like that, but something that provides a consistent output and doesn't go out of control when the reference approaches zero. – Lucas Johnston Mar 25 '24 at 19:55

1 Answers1

0

One way to circumvent it is to add a different clause for small numbers, e.g. if $\max(x_1,x_2)<threshold$ then return $|x_1-x_2|$ else return one of your formulas.

Another idea I've got is to use softmax function. It's not really supposed to be used for this, but I think it works okay. Basically you use: $\frac{e^{x_1}}{e^{x_1}+e^{x_2}}$: if $x_1$ and $x_2$ are close, the result will be close to $\frac{1}{2}$, otherwise it will go to $0$ or $1$, depending on which one is larger.

In the end "closeness" is a subjective term and thus it's up to you if such methods are suitable for your task. As you didn't disclose what is the purpose of assessing the closeness of numbers, that's the best I can give you.

MartinYakuza
  • 116
  • 2