4
  • $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...

cqfd
  • 12,219
  • 6
  • 21
  • 51

2 Answers2

4

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)$$

Chrystomath
  • 10,798
  • 1
    Tongue in cheek: $(\frac{d}{dx}|x-m|+1)/2$ – Chrystomath Jul 03 '20 at 06:44
  • @Chrystomath, what would be the correct way to say what the OP meant? I mean the proper wording for "pure math way" – UmbQbify Jul 03 '20 at 07:10
  • 1
    @user675453 I don't see a difference between pure math and programming. They're just different ways of expressing functions. Or do you mean notation? The step function comes with a name: the Heaviside function, so perhaps $H(x-m)$. – Chrystomath Jul 03 '20 at 13:26
2

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.

cqfd
  • 12,219
  • 6
  • 21
  • 51