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.

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

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