You can try the following:
if (((a < 0) ^ (b < 0)) && (a % b != 0)) { return (a/b - 1); } else { return (a/b); }
Edit (after some discussion in the comments below):
Without using if-else, I would do the following:
return (a/b - Convert.ToInt32(((a < 0) ^ (b < 0)) && (a % b != 0)));
Note: Convert.ToIn32(bool value) also requires a jump, see the implementation of the method:
return value? Boolean.True: Boolean.False;
It is theoretically impossible to calculate the division for a = long.MinValue and b = -1L , since the expected result is a/b = abs(long.MinValue) = long.MaxValue + 1 > long.MaxValue . (The long range is from β9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 .)
source share