Script Integer Division
The Bitcoin Script OP_DIV and OP_MOD operations are performed on script numbers, which are signed integers. However, there are multiple valid approaches to signed integer division, particularly with respect to how negative values are handled, so this must be disambiguated.
The above operations follow C-style integer division semantics.
For a given numerator and denominator, if OP_DIV returns quotient and OP_MOD returns remainder, the following relation must be true: numerator = quotient * denominator + remainder.
This allows for both negative quotients and remainders: the quotient is truncated toward zero, and the remainder has the sign of the numerator.
If the divisor is zero, OP_DIV and OP_MOD fail evaluation.
The following examples may provide clarity:
<5> <2> OP_DIV -> 2
<5> <2> OP_MOD -> 1
<-5> <2> OP_DIV -> -2
<-5> <2> OP_MOD -> -1
<5> <-2> OP_DIV -> -2
<5> <-2> OP_MOD -> 1
<-5> <-2> OP_DIV -> 2
<-5> <-2> OP_MOD -> -1