# A function whose value is either one or zero

First I apologize in advance for I don't know math's English at all and I haven't done math in almost a decade.

I'm looking for a function whose "domain/ensemble of definition" would be ℝ (or maybe ℤ) and whose "ensemble/domain of variation" would be ℕ{0, 1} that would look like something this awesome ascii graph...

          ^f(x)
|
|
1________ | __________
0________\./__________>x
0|
|
|
|


f(x) is always 1, but 0 when x = 0

Actually I need this for programming, I could always find other ways to do what I wanted with booleans so far but it would be much better in most cases to have a simple algebra formula to represent my needs instead.

I just hope it's not too complicated a function to write/understand and that it exists of course. Thanks in advance guys.

Define $f: \mathbb{R} \rightarrow \{0,1\}$ via $$f(x) = \begin{cases} 0 &\text{ if } x = 0\\ 1 &\text{ if } x \neq 0. \end{cases}$$

If you are programming, this can be accomplished with a single if-then-else statement. This will surely be more efficient to compute than any "algebra formula" (by which I take you to mean "non-piecewise formula"), since if clauses are built into the language at a very low level.

• Ok thanks for the programming answer but, mathematically speaking, is this really how you'd write the formula? cause "otherwise" doesn't seem to be a mathematical term at all. wouldn't is at least be something like if x= n for n in R0? Dec 2 '12 at 14:59
• It still feels like there should be a formula whose function has the same output without booleans, this question was out of curiosity more than practical use, so I don't restrict yourselves to usefulness, please. Dec 2 '12 at 15:01
• @sinekonata I've updated the formula with a little extra mathematical boilerplate. The term "otherwise" is quite common to define piecewise functions (i.e. functions that have a different definition on different parts of the domain), but it can always be avoided if you don't like that word. Dec 2 '12 at 18:24
• Thanks, it's much better and kind of embarrassing that I couldn't do it myself n_n;; Dec 3 '12 at 0:52

The function $f: \mathbb{R} \rightarrow \{0,1\}$ defined via $$f(x) = \lim_{n \rightarrow \infty} \sqrt[n]{x^2}$$ satisfies $f(0) = 0$ and $f(x) = 1$ for all $x \neq 0$. This is entirely useless for programming, but it is the simplest function I can think of that avoids a piecewise definition.

• I accepted this one even though the others are cleaner because I wanted to avoid conditions. Thanks for everything guys. Dec 3 '12 at 0:54
• Amazing, as I said in one of the comments at this level of complexity it's more about curiosity... So what does the formula mean? Why would x=0 not be 1 like the rest? since 0^0 = 5^0 = 1 what happens at n→∞ that makes the equivalence different? Dec 3 '12 at 1:01
• @sinekonata For $x = 0$, you have $$\lim_{n \rightarrow \infty} \sqrt[n]{0}.$$ The sequence generated by the $n$'s is constantly $0$ ($0 = \sqrt{0} = \sqrt{0} = \cdots$), so the limit is $0$. Dec 3 '12 at 1:11

I think that the most compact way to write this is using the Iverson Bracket:

$f: \mathbb{R} \to \{{0,1}\}$

$$f(x) = [x \neq 0]$$

• With having to clarify that you use this notation, by a definition at the beginning. I like this notation at places where you really need it.
– yo'
Dec 2 '12 at 19:25
• @tohecz Are you saying that since one must say, $[A]$ is the Iverson Bracket, it diminishes its value as a compact notation? I suppose that is a valid point. Its value then increases with the number of subsequent calculations using the notation AFTER it has been defined. Dec 2 '12 at 19:36
• I'm sure Iverson Bracket is as so commonly used name, as you claim it to be, maybe it is in some branches, but not in general. If you need it once or twice, don't use it and write it out. After all, the other answer is much more clear and better readable. If you do many calculations wihth it, it's completely ok, but for one definition?
– yo'
Dec 2 '12 at 19:45
• Sorry, I wanted to say "no as commonly used name as you claim it to be".
– yo'
Dec 2 '12 at 20:29
• This one is pretty straightforward since it's only a redefinition. Dec 3 '12 at 0:52

Well, $$f(x)=1-0^x$$ will work provided we agree in $$0^0=1$$.

• Hmmm. My calculator says 0^0 is undefined but python agrees with you. Python has a problem with raising 0 to negative powers though. Calls it a ZeroDivisionError. Mar 30 at 20:33

I found this question relevant in social science when you are testing for the effect of a variable but are uncertain whether it is its mere presence which is significant, or whether the degree of the presence should be used. I have used the following approach in Excel to convert degree of presence ($$A_2 \ge 1$$) to just presence ($$A_2=1$$).

The following simple nested 'IF' statement will return 1 if $$A_2$$ is a positive number, $$0$$ for $$0$$, and $$-1$$ for a negative number.

=IF(A2=0,0,IF(A2>0,1,-1))