This page is a placeholder. All examples on this page are currently AI-generated and are not correct. This documentation will be completed in the future with accurate, tested examples.
Overview
Opcode: 0x04
Introduced: Frontier (EVM genesis)
DIV performs unsigned integer division on two 256-bit values. Unlike most programming languages, division by zero returns 0 instead of throwing an exception, preventing denial-of-service attacks.
This operation is essential for ratio calculations, scaling, and implementing fractional arithmetic in smart contracts.
Specification
Stack Input:
Stack Output:
Gas Cost: 5 (GasFastStep)
Operation:
Behavior
DIV pops two values from the stack, performs integer division (truncating toward zero), and pushes the quotient:
- If
b ≠ 0: Result is floor(a / b) (truncated)
- If
b = 0: Result is 0 (no exception)
The result is always the integer quotient with remainder discarded. Use MOD to get the remainder.
Examples
Basic Division
Division with Remainder
Division by Zero
Division by One
Large Division
Gas Cost
Cost: 5 gas (GasFastStep)
DIV costs the same as MUL and MOD, more than ADD/SUB due to increased complexity:
Comparison:
- ADD/SUB: 3 gas
- MUL/DIV/MOD/SDIV/SMOD/SIGNEXTEND: 5 gas
- ADDMOD/MULMOD: 8 gas
- EXP: 10 + 50 per byte
Division is ~67% more expensive than addition but significantly cheaper than repeated subtraction.
Edge Cases
Zero Division
Self-Division
Division Truncation
Maximum Value Division
Common Usage
Ratio Calculations
Fixed-Point Division
Average Calculation
Scaling and Conversion
Implementation
Testing
Test Coverage
Security
Division by Zero
Why DIV returns 0 instead of reverting:
The EVM solution:
Precision Loss
Problem: Integer division loses precision
Solution: Multiply first
Rounding Direction
Safe Fixed-Point Math
Overflow in Multi-Step Calculations
Benchmarks
DIV performance characteristics:
Relative execution time:
- ADD: 1.0x
- MUL: 1.2x
- DIV: 2.5x
- MOD: 2.5x
Gas efficiency:
- 5 gas per 256-bit division
- ~200,000 divisions per million gas
- Much faster than repeated subtraction (which would be ~3n gas for n subtractions)
Optimization tip:
References