# Math Genius: Dealing with ABSOLUTE VALUE and IF STATEMENT in linear integer programming

I am trying to write a linear constraint that computes the absolute value of a difference, only if both the variables $$x$$ and $$y$$ are different from zero.

$$x,y$$ are binary variables while $$s$$ is a positive integer variable. $$a$$ and $$b$$ are positive integer coefficients.

$$|xa-yb| leq s$$ only if $$x neq 0$$ and $$y neq 0$$

for the first part I did:

$$(xa-yb) leq s$$ and $$-(xa-yb) leq s$$

but I don’t know how to proceed with the second part, if that is ever possible.

You can change $$dots le s$$ to $$dots le s + M(2 – x – y)$$ where $$M$$ is an a priori upper bound on $$|xa-yb|$$. If either or both of the binary variables is 0, the inequality will not be restrictive.
$$(x = 1 land y = 1) implies |a-b| le s$$
$$|a-b|(x + y – 1) le s.$$
$$|a-b| le s implies (x = 1 land y = 1),$$