Subtract smaller or larger number from x?



I have a number x. I would now like to find the difference between this
number x and another number y.

But y can be either larger or smaller than x. Currently I check if the
number y is greater or smaller than x:

int diff; // the difference between x and y

if (y > x){
diff = y-x;
}else{
diff = x-y;
}

is there not a more elegant way to do this when I would like the
difference to be a positive value??

.



Relevant Pages

  • Re: Subtract smaller or larger number from x?
    ... int diff; // the difference between x and y ... That rather depends on what you mean by elegant. ... the/a mathematical definition of 'difference' as possible. ... You all think I'm paranoid, ...
    (comp.programming)
  • Re: Subtract smaller or larger number from x?
    ... int diff; // the difference between x and y ... is there not a more elegant way to do this when I would like the ... Since this looks like C, abs(), labs() or labsmay be useful ...
    (comp.programming)