Skip to content
SpecializedSource checked

Range Coding

Compresses symbols by narrowing a numeric range according to their probabilities.

Quick Facts

Compact metadata for implementation and discovery.

Category
Compression
Difficulty
Advanced
Family
Entropy Coding
Tier
Specialized
Status
Verified
Review
Source Checked
Sources
1
Formulas
1
Relationships
4
Code example
Ready
Visualization
Ready

Overview

Range Coding is an arithmetic-coding variant that maintains a low value and range width while emitting bytes as the interval stabilizes.

Core Concepts

probability intervalcumulative modelrenormalizationentropy coding

Quick facts

Complexity

Runtime and memory behavior at a glance.

Best

O(n)

Average

O(n)

Worst

O(n)

Space

O(k)

When to Use

  • Use when probability estimates are available and fractional-bit coding matters.
  • Use when entropy coding in compressors is a central requirement.

When Not to Use

  • Avoid when fixed prefix codes are good enough and simpler.
  • Avoid when the added bit-level complexity is not worth the expected size reduction.

What It Does

  • Maintains a coding interval
  • Narrows the interval per symbol
  • Emits stable leading bits or bytes

Use Cases

  • Entropy coding in compressors
  • Adaptive statistical coding
  • Compact binary formats

Inputs

  • Symbol stream
  • Cumulative probability model

Outputs

  • Compressed bitstream

Key Formulas (1)

Range update

rangenext = range · probability(symbol)
range_next = range * probability(symbol)

Shrinks the current interval in proportion to the symbol probability.

Pseudocode

  1. 1.Start with a full numeric range
  2. 2.Look up the symbol's cumulative interval
  3. 3.Narrow low and range to that interval
  4. 4.Emit stable high-order bytes
  5. 5.Repeat until all symbols are encoded

Canonical Example

Input
model = A:0.75, B:0.25, input = A B
Output
range = narrowed first to A range then B subrange
Explanation
The final interval identifies the whole message.

Implementation Notes

  • Use integer arithmetic and tests that compare encoder and decoder after every symbol.
  • Write round-trip tests that encode and decode small hand-checked inputs.
  • Keep a tiny example with expected output beside the implementation.

Common Pitfalls

  • Off-by-one interval boundaries can make the decoder diverge after many symbols.
  • Encoder and decoder must make exactly the same modeling and bit-order choices.
  • Not defining boundary, empty, or degenerate inputs before implementation.

Interactive Mechanism Walkthrough

Sequence Window & Automaton Alignment

Show the active interval shrinking inside a probability range for each symbol.

Input:ABABABCABAB
Window:ABAB
Step 1 of 5: Start with a full numeric range
encoding-stream

Notes

Finite-precision renormalization is the main implementation detail.

Sources (1)

What each reference supports for this entry.

Data Compression: The Complete Reference

Book

Compression reference for range coding as an arithmetic-coding variant with integer interval updates.

Explore Next

Good next jumps from the relationship graph, ranked for useful discovery.

Alternative toANS Coding

Both are entropy coding approaches used in modern compressors.

Range coding is a practical coding method closely related to arithmetic coding.

Related Algorithms

Tags

CompressionEntropy CodingEncoding