1

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)

Jonathan.
  • 1,495

3 Answers3

8

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. – Did Oct 18 '11 at 19:19
  • 1
    But isn't a square bracket also used for rounding? – Jonathan. Oct 18 '11 at 19:23
  • 1
    @Jonathan, do you mean this? First, these are not the same symbol, second the arguments are different (real numbers vs properties). – Did Oct 18 '11 at 19:37
  • @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. – hmakholm left over Monica Oct 18 '11 at 19:38
  • 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. – Brian M. Scott Oct 18 '11 at 19:43
  • ...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." – J. M. ain't a mathematician Oct 19 '11 at 00:20
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$

gary
  • 4,027
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.