[Math] What mathematical function would do this: if $x = 0$ then $y = 0$ but if $x > 0$ then $y = 1$


$x = 0$, $f(x) = 0$

$x = 1$, $f(x) = 1$

$x = 2$, $f(x) = 1$

$x = 3$, $f(x) = 1$

There have been so many times I could have used this at different programming problems but I always resorted to logical expressions. I feel that there should be a more elegant, purely mathematical, solution.

Best Answer

Unfortunately, checking my typical python and java packages (the only languages I work in now), I exception handle NaN cases. So my original post wouldn't work unless you also handle them.

If you're looking for a good way to code this, then you might try either

if x > 0: return 1
return 0

or, if you're in a language which evaluates True to 1, False to 0, something like

return int(x>0)

But in terms of a mathematical function, your description alone is a mathematical function. There is no ambiguity about $f:[0,\infty] \to \mathbb{R}$, $f(x) = 0$ if $x = 0$, and $f(x) = 1$ else.

Related Question