- P2P Network Overview/
- BCH Specification/
- Protocol Upgrades/
- Bitcoin & Blockchain
- Addresses
- Base58
- Bitcoin Keys
- Bitcoin Script
- Block
- Block Header
- Block Validation Rules
- Cash-Address
- Chainwork
- DAA
- Hash
- Locking Script
- Mempool
- Merkle Tree
- Mining
- Multisig
- Network Validation Rules
- Proof of Work
- Script: division
- Script: Op_Data
- Script: OP_X
- Signatures
- Transaction
- Transaction Ordering
- Transaction Signing
- Transaction Validation
- Unlocking Script
Bitcoin Cash Specification
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 will be true: numerator = quotient * denominator + remainder
.
This allows for both negative quotients and remainders.
The following examples may provide clarity:
OP_DIV 5 2 -> 2
OP_MOD 5 2 -> 1
OP_DIV -5 2 -> -2
OP_MOD -5 2 -> -1
OP_DIV 5 -2 -> -2
OP_MOD 5 -2 -> 1
OP DIV -5 -2 -> 2
OP_MOD -5 -2 -> -1