# Function that converts values to 0 or 1

I'm looking for a function that takes a number between 0 and 1 and converts it into 0 if number is between 0 and 0.5, and into 1 if number is between 0.5 and 1.

So far I've got the first part, f(x) = max(x - 0.5, 0.0), but can't figure out how to continue the formula for the second part.

EDIT: The idea is to write it as one expression to avoid if branching.

• Can you tell me why $f(x) = 0$ if $0 \le x < .5$ $f(x) = 1$ if $.5 \le x \le 1$ is not acceptable? That is a function and it's very well defined and calculable. – fleablood May 26 '17 at 23:44
• If the language you are using has a round function, then round(x) will do the trick. BTW, while it may make your code look cleaner (which may be the more important consideration), it'll almost surely be less CPU efficient to invoke a function rather than perform a branch. The only exception I can think of is if round(x) is an inline function. – Χpẘ May 27 '17 at 0:49
• Or probably (int) (x >= 0.5). – johnchen902 May 27 '17 at 2:19
• @Χpẘ You're right regarding CPU, thing is in my case it's GPU and I use GLSL :) – user1617735 May 27 '17 at 5:48

You could just use a piecewise function, but if you're looking for a different option, this should work: $$f(x) = \lfloor2x\rfloor$$ If $0<x<0.5$, then $0<2x<1$, so $\lfloor2x\rfloor=0$ and if $0.5<x<1$, then $1<2x<2$, so $\lfloor2x\rfloor=1$.

• Damn I can't upvote now – Oussama Boussif May 26 '17 at 23:35
• Don't want to be a spoilsport, but how is the floor function not a piecewise function, and why is the floor function any more or less acceptable then simply the function $f(x) = \begin{cases} 0 & \text{if } 0 \le x < 0.5\\1 & \text{if } .5 \le x \le 1\end{cases}$ – fleablood May 26 '17 at 23:50
• I know. But it is a big bugaboo pet-peeve of mine that beginning students often assume incorrectly to be a valid function it has to have some expressible formula and if the cant find it they can't express it as a function. I REALLY want to abuse students of the notion. If you can say "I want a function that returns 0 if x is less than .5 and returns 1 otherwise" then that IS the function. The student HAS found a function and described it perfectly adequately. – fleablood May 26 '17 at 23:57
• @fleablood +1 for the comment. Note: you want "disabuse", not "abuse". – Ethan Bolker May 27 '17 at 0:12
• The usual programmer's trick for this kind of rounding is $f(x) = \left\lfloor x+\frac12\right\rfloor.$ This doesn't require any additional mechanism for the case $x=1.$ It also performs rounding within the intervals between other integers than just $0$ and $1,$ although for this application that's apparently not necessary. – David K May 27 '17 at 9:35

"I'm looking for a function that takes a number between 0 and 1 and converts it into 0 if number is between 0 and 0.5, and into 1 if number is between 0.5 and 1."

A function is any relation (set of ordered pairs) where the first term is mapped to a unique second term. That is all. A function doesn't have to have a rule or formula to make it calculable. ANything that can be unambiguously described is an acceptable function.

So the function you want is:

$f(x) =\begin{cases} 0 & \text {if } 0 \le x < .5 \\ 1 & \text {if } .5 \le x \le 1\end{cases}$.

That's it! That is all you need to say.

Unless you are asking for a mathematical formula.

But in that case you should ask for a formula, not a function. You already have the function.

It is "a function that takes a number between 0 and 1 and converts it into 0 if number is between 0 and 0.5, and into 1 if number is between 0.5 and 1".

• Either that's a partial function, or the domain is not the reals... – Kevin May 27 '17 at 3:53
• The domain is [0,1]. The function isn't defined anywhere else. The function ' s domain is implicit in its definition/description. – fleablood May 27 '17 at 4:22

I don't know if this is answer your question, but could it be the Heaviside theta function $\theta(x-0.5)$? You can write it as the derivative of the maximum function: $$\theta(x-0.5) = \frac{d}{dx}\max\{x-0.5,0\}= \begin{cases} 0 & \text{ if } x < 0.5\\ 1 & \text{ if } x > 0.5 \end{cases}$$

$\def\sign{\operatorname{sign}}$

Using another piecewise function, which is often included in a standard set of functions:

\begin{align} \sign x &= \begin{cases} -1 & \text{if } x < 0 \\ \phantom{-}0 & \text{if } x = 0 \\ \phantom{-}1 & \text{if } x > 0 \end{cases} \quad, \\ f(x)&=\tfrac12(\sign(2x-1)+1)\cdot \sign(2x-1) \end{align}

Using the Iverson bracket, one could describe the function simply as $[x>0.5],\; x\in [0,1]$.