$f(x) = 0$ when $x$ is $0$, and $1$ otherwise

21.7k Views Asked by At

I've been trying to create a function that will return $0$ when $x$ is $0$, and for any other $x$ value it should return $1$. I've searched for a pre-existing function online too and wasn't able to find one.

Do you know of any function that can do this?

5

There are 5 best solutions below

8
On BEST ANSWER

How about $f(x)=\left\lceil\frac{x^2}{x^2+1}\right\rceil$

*Works for real numbers, with imaginary numbers you may divide by 0.

0
On

How about $f(x)= 1-\delta_{x,0}$ (using the Kronecker Delta function, in Mathematica/WolframAlpha can write the $\delta_{x,0}$ as

kroneckerdelta(x,0)

)

7
On

You've already defined your function (assuming you've also chosen its domain).

One of the main ways to "create" a function is simply by specifying its values at all points, and your description has done so.

Typical notation for a function created by the sort of description you give is a definition by cases:

$$ f(x) := \begin{cases} 0 & x = 0 \\ 1 & x \neq 0 \end{cases} $$

For many applications — most applications, I expect — this is one of the best descriptions of said function. If need be, name it with a letter, and continue on with whatever you're doing.


The complementary function

$$ g(x) := \begin{cases} 1 & x = 0 \\ 0 & x \neq 0 \end{cases} $$

which is related to your function by $f(x) = 1 - g(x)$ comes up often enough in some contexts to have been given a name and notation: e.g.

  • The Kronecker delta. A few different notations exist depending on the setting; e.g. $\delta_x$, $\delta[x]$, or $\delta_{x,0}$.
  • The Iverson bracket. This would be notated $[x = 0]$. This notation is, IMO, indispensable for doing complicated calculations with summations.
  • x == 0 computes this function in C and C++, and many other programming languages allow similar.

Some applications might want to represent such a function in particular ways. For example, if one only cares about the value of $g(x)$ when $x$ is an integer, but strongly prefers to work with analytic functions (e.g. because you're studying a sequence using complex analysis), one has the fact that

$$ g(x) = \mathop{\mathrm{sinc}}(\pi x) $$

holds whenever $x$ is an integer.

(if you're unfamiliar with it, $\mathop{\mathrm{sinc}}(z)$ is the continuous extension of $\sin(z) / z$)

0
On

Paw88789's answer worked great for what I'm trying to do; the only issue was that it didn't work for all "numbers"; some imaginary numbers would cause division by 0. Luckily tonight I was able to create a function that produced the desired result extended to the range of numbers I need to work with

$f(x) = \left \lceil \frac{1}{2\Gamma \left ( \left | x \right | \right )} \right \rceil$

*Note the absolute value inside of Gamma, as it's easy to go unnoticed

0
On

An approximation of your desired function be of this form: $$f(x) = \sqrt[10^{100}]{\lvert x \rvert}$$ Where $f(1\cdot10^{-50}) = 1$ while $f(0) = 0$.