I need to lighten the code, and then eliminate the underlying if
that check if the subtraction are <0
and if the condition is true I multiply * -1 in order to have subtraction >0 (absolute value).
I wonder if there is a function or some operator able to perform the calculation directly in absolute value?
int i, j, N, max, s, s1;
s = v[i] - v[j];
s1 = i - j;
if (s1 < 0){
s1 *= -1;
}
if (s < 0){
s *= -1;
}
Absolute value
To avoid
int
overflow ofabs(v[i] - v[j])
and its undefined behavior (UB) , take advantage that theunsigned
range is very commonly twice theint
positive range and perform unsigned math.