1
$\begingroup$

Is there an equation, that will return either 1 if a variable is positive, or 0 if the variable is negative.

For example, to see if an integer is odd or even you can use:

$$r=\frac{(-1)^n+1}{2}$$

where $r$ will be $1$ if $n$ is even, and $r$ will be $0$ if $n$ is odd.

So is there a similar equation, to see if $n$ is positive or negative?

(sorry I have no idea what to tag this)

3 Answers 3

9

What you want is known as the Heaviside step function. There are more or less contrived ways to represent it as a closed formula, but usually no good reason to bother. It is much easier for everybody simply to say "1 if positive, 0 if negative" than to attempt to get that result out of a particular arithmetic expression.

  • 3
    +1. One notation I like is [Iverson bracket](http://en.wikipedia.org/wiki/Iverson_bracket).2011-10-18
  • 1
    But isn't a square bracket also used for rounding?2011-10-18
  • 1
    @Jonathan, do you mean [this](http://en.wikipedia.org/wiki/Floor_and_ceiling_functions)? First, these are not the same symbol, second the arguments are different (real numbers vs properties).2011-10-18
  • 0
    @Jonathan, not universally. In general, you get to define your use of notation freely as long as you make it clear to your reader what you're doing.2011-10-18
  • 3
    I’d not understand $[x]$ as an abbreviation for $\left\lfloor x+\dfrac12\right\rfloor$ unless that meaning was explicitly stated; I *would* understand $[x\ge 0]$ (or square brackets around any other relational expression) as an Iverson bracket, though I’d expect a sensible author to explain the notation.2011-10-18
  • 0
    ...and if you're programming in a language like C/++ or JS, Iverson brackets are already implicitly supported, as they treat `TRUE` as 1 and `FALSE` as 0. But really, @Jonathan, just think of $[x > 0]$ as really compressed shorthand for "1 if $x$ is greater than zero (positive), and 0 otherwise."2011-10-19
1

I don't know if this is what you are looking for, but maybe:

EDIT: $\frac {x+|x|}{2}$ where $|x|$is the standard absolute value, returns a $x$ if $x>0$ and a 0 if $x\leq 0$

  • 1
    Ummm, that returns $x$ when $x>0$. You need to divide by $x$ to get 1, but then you are undefined if $x=0.$2011-10-18
  • 1
    Am I being retarded or am I misinterpreting the standard absolute value? $\frac{3+|3|}{2}$ = 3?2011-10-18
  • 0
    Yes, I did something dumb here; let me check.2011-10-18
  • 0
    @Gary, if $x > 0$ I get the answer as x, (e.g. $\frac{5+|5|}{2}=5$)2011-10-18
  • 0
    Jonathan: I edited , and now you get x back if x>0 and 0 otherwise. Does that work?2011-10-18
  • 0
    Oh I was thinking of something else@!2011-10-18
  • 0
    Doesn't this actually work, as 0 is neither positive or negative, and therefore undefined?2011-10-18
1

If you absolutely need an elementary formula, you can consider

$f(x)=\frac{1}{2}(\frac{\sqrt{x^2}}{x}+1)$.

Do note however that $f(0)$ is undefined.