Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Given two values, I'm trying to come up with a formula that will return 50% if both values are equal, 25% if the first value is half the second, 75% if the second is half the first. In other words:

given (a=3,b=12) returns .125

given (a=3,b=6) returns .25

given (a=3,b=3) returns .5

given (a=6,b=3) returns .75

given (a=12,b=3) returns .875

(a and b will always be positive)

... the idea being that if a is half of b, it's 25%, if it's half of half, it's 12.5% or half of 25%, if it's half of half of half, etc - the numbers go down if a is greater, but up in the same way if b is greater.

I'd love to know what that equation would look like - I don't really know enough about how this stuff fits together to do much more than fiddle with it on my own. (not even sure what tags to apply to this)

share|cite|improve this question
Are you sure the second example is right? I think you meant "given (a=3,b=3) returns .5" What do you want it to return beyond these cases, e.g., if one is a quarter or some other fraction of the other? There is clearly not enough information for a unique solution, but with some assumptions we can create an interpolation. – Emre Oct 19 '11 at 22:47
What (if anything) do you want the value of the function to be if $a\not=b$ and $\frac{a}{2}\not=b$? – user5137 Oct 19 '11 at 22:48
you're right, @emre, misplaced a 6 there. added a couple other cases. – matt lohkamp Oct 19 '11 at 22:56
@jack - I don't know how to answer that - if a isn't equal to b, and half a isn't equal to b... it'd depend on what a is, but that doesn't answer your question. – matt lohkamp Oct 19 '11 at 23:00
@mattlohkamp - Well, you're the one trying to construct the function; you tell me. We want (using the new construction from the edit): $f(x,y)=0.5$ if $x=y$, $f(x,y)=0.25$ if $x=\frac{y}{2}$ and $f(x,y)=0.75$ if $2x=y$. Do you want $f$ to be defined for any other values of $x$ and $y$? If not, we're done. If so, what do you want to do? Incidentally, where are the values of 0.125 and 0.875 coming from? – user5137 Oct 19 '11 at 23:07
up vote 1 down vote accepted

This function will reproduce your numbers (at least if $a$ and $b$ are positive):

$$f(a,b)= \frac{a}{2b} \text{ when } a \le b$$ $$f(a,b)= 1- f(b,a) \text{ when } a \gt b$$

You can write the latter case as $f(a,b)= \dfrac{2a-b}{2a}$ when $a \ge b$.

You can write both as $\dfrac{1}{2}+\dfrac{(a-b)\times \min(a,b)}{2ab}$.

share|cite|improve this answer
Ah, I was curious about this - whether I needed two different things, one for when a > b, and one for the alternative. Also, good point about a and b being positive - they are, for my purposes. – matt lohkamp Oct 19 '11 at 23:07
...and you're right, this does exactly what I want. – matt lohkamp Oct 19 '11 at 23:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.