Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:


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)

share|cite|improve this question

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.

share|cite|improve this answer
+1. One notation I like is Iverson bracket. – Did Oct 18 '11 at 19:19
But isn't a square bracket also used for rounding? – Jonathan. Oct 18 '11 at 19:23
@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. – Henning Makholm Oct 18 '11 at 19:38
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

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$

share|cite|improve this answer
Ummm, that returns $x$ when $x>0$. You need to divide by $x$ to get 1, but then you are undefined if $x=0.$ – Thomas Andrews Oct 18 '11 at 19:06
Am I being retarded or am I misinterpreting the standard absolute value? $\frac{3+|3|}{2}$ = 3? – user12205 Oct 18 '11 at 19:06
Yes, I did something dumb here; let me check. – gary Oct 18 '11 at 19:10
@Gary, if $x > 0$ I get the answer as x, (e.g. $\frac{5+|5|}{2}=5$) – Jonathan. Oct 18 '11 at 19:10
Jonathan: I edited , and now you get x back if x>0 and 0 otherwise. Does that work? – gary Oct 18 '11 at 19:12

If you absolutely need an elementary formula, you can consider


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

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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