Find the formula to get $0$ or $1$ when input is greater than a threshold

394 Views Asked by At
  • $x \geq m, f(x) = 1$

  • $x <m, f(x) = 0$

My best result so far is :

$$f(x) = \frac{ x-m + \left| x-m \right|}{2\left| x-m \right|} $$

But when $x = m$, it is invalid...

2

There are 2 best solutions below

3
On

Let sgn be the sign function (present in most programming languages). Then this function should do it for a threshold $m$: $$\frac{\mathrm{sgn}(x-m)+1}{2}$$

Depending on the implementation of sgn, this gives $1/2$ for $x=m$. If you want it to output $1$ instead, then use $$\mathrm{ceil}((\mathrm{sgn}(x-m)+1)/2)$$

0
On

I ended up with :

$$f(x) = \operatorname{ceil}\left( \frac{ x-m + \left| x-m \right|}{2\left| x-m \right| + 1}\right)$$

Not very mathematic but usable for programming.

@Chrystomath's solution is somewhat better/shorter in term of programming.