I am not sure if I understand this final top-level else clause. `diff.is_negative` is surely equivalent to p < q. So the `if (p < q) is True` should be redundant, shouldn't it?

In any case, are you using here that '0 <= p < q' implies 'p % q == p', and '-q <= p < 0' implies 'p % q == p + q'? If so would it make sense to check these inequalities in a less convoluted way (note in particular that some of them are strict and some are not)?