# Need a math function [closed]

This function take 2 integer and if both int are positive return 0, if the first is positive and the second is negative return 1, if both are negative return 2 and if the first is negative and the second is positive return 3.

-

## closed as off topic by Asaf Karagila, The Chaz 2.0, Beni Bogosel, Kannappan Sampath, RasmusApr 20 '12 at 21:18

Questions on Mathematics Stack Exchange are expected to relate to math within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

What about this function: $$f(x,y)=\begin{cases} 0& x>0, y>0\\ 1& x>0, y<0\\ 2& x<0,y<0\\ 3& x<0,y>0\\ ??& x=0\text{ or }y=0\end{cases}$$ –  Asaf Karagila Apr 20 '12 at 19:54
@Pioz, What if both are zero? –  Julius Apr 20 '12 at 19:57
Pioz, what you have described is a function from $\mathbb{Z} \times \mathbb{Z} \setminus (0,0) \to \mathbb{Z}$ –  The Chaz 2.0 Apr 20 '12 at 19:57
See what happened here? Two users came to MSE especially for a question and an answer that would have been more than reasonable on SO but verges off-topic on MSE. –  Asaf Karagila Apr 20 '12 at 19:59
This is hardly a math question. –  copper.hat Apr 20 '12 at 20:02
show 2 more comments

In C your may write (with ^ the 'xor') :

(y<0)^(3*(x<0))

-

Consider a one-argument function $g(x)$ that is equal $1$ if $x$ is negative, and $0$ if $x$ is positive.

Then a possible answer is $$f(x,y)=2\cdot g(x)+g(y)$$

Now pick you favorite $g(x)$, for example, $$g(x)=\frac{1-sign(x)}{2}$$

-

Maybe this will help:

#include <math.h>

int func(int a, int b)
{
int sa = (a > 0) - (a < 0);
int sb = (b > 0) - (b < 0);
return (-sa+1) + (-sb+1)/2;
}


It works this way:

+---+---+----+----+------+
| a | b | sa | sb | func |
+---+---+----+----+------+
| + | + | +1 | +1 |    0 |
| + | - | +1 | -1 |    1 |
| - | + | -1 | +1 |    2 |
| - | - | -1 | -1 |    3 |
+---+---+----+----+------+


But it wrong when a or b is equal 0.

Here you can read a lot about bit hack: http://graphics.stanford.edu/~seander/bithacks.html

-