Calculate Factor thats 1 if value is not 0, and 0 if it is

38 Views Asked by At

I don't know if this is the right stackexchange for this question, please tell me/move it if it isn't.

I calculate a value $x$ that can be any integer number. I now want to use this variable to get a factor thats $0$ if $x$ is zero, and 1 if it isn't.

I can easily program this with an if-statement, however due to optimization reasons, I want to get around the if. So I was wondering if there is a mathematical expression that would give me $fac = 1$ if $x\ne0$ and $fac=0$ if $x = 0$.

1

There are 1 best solutions below

6
On BEST ANSWER

You can set a new variable equal to the floor of $x/(x-1)$. ( as long as $x$ can't be equal to $1$). Alternatively the ceiling of $x/(x+1)$ (as long as $x$ isn't negative).