2
$\begingroup$

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.

$\endgroup$
  • 15
    $\begingroup$ 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. $\endgroup$ – fleablood May 26 '17 at 23:44
  • 2
    $\begingroup$ 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. $\endgroup$ – Χpẘ May 27 '17 at 0:49
  • 3
    $\begingroup$ Or probably (int) (x >= 0.5). $\endgroup$ – johnchen902 May 27 '17 at 2:19
  • $\begingroup$ @Χpẘ You're right regarding CPU, thing is in my case it's GPU and I use GLSL :) $\endgroup$ – user1617735 May 27 '17 at 5:48
14
$\begingroup$

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

$\endgroup$
  • 1
    $\begingroup$ Damn I can't upvote now $\endgroup$ – Oussama Boussif May 26 '17 at 23:35
  • 5
    $\begingroup$ 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}$ $\endgroup$ – fleablood May 26 '17 at 23:50
  • 15
    $\begingroup$ 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. $\endgroup$ – fleablood May 26 '17 at 23:57
  • 4
    $\begingroup$ @fleablood +1 for the comment. Note: you want "disabuse", not "abuse". $\endgroup$ – Ethan Bolker May 27 '17 at 0:12
  • 6
    $\begingroup$ 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. $\endgroup$ – David K May 27 '17 at 9:35
16
$\begingroup$

"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."

And you've already found it.

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".

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

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

$\endgroup$
2
$\begingroup$

$\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}

$\endgroup$
2
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.