Arithmetic Coding
In arithmetic coding, a message is encoded as a real number in an interval from one to zero. Arithmetic coding typically has a better compression ratio than Huffman coding, as it produces a single symbol rather than several seperate codewords. Arithmetic coding is a lossless coding technique. There are a few disadvantages of arithmetic coding. One is that the whole codeword must be received to start decoding the symbols, and if there is a corrupt bit in the codeword, the entire message could become corrupt. Another is that there is a limit to the precision of the number which can be encoded, thus limiting the number of symbols to encode within a codeword. There also exists many patents upon arithmetic coding, so the use of some of the algorithms also call upon royalty fees.
Here is the arithmetic coding algorithm, with an example to aid understanding.
| Symbol | Probability | Interval |
| a | 0.2 | [0.0, 0.2) |
| b | 0.3 | [0.2, 0.5) |
| c | 0.1 | [0.5, 0.6) |
| d | 0.4 | [0.6, 1.0) |
| Symbol | New "a" Interval |
| a | [0.0, 0.04) |
| b | [0.04, 0.1) |
| c | [0.1, 0.102) |
| d | [0.102, 0.2) |
| Symbol | New "b" Interval |
| a | [0.102, 0.1216) |
| b | [0.1216, 0.151) |
| c | [0.151, 0.1608) |
| d | [0.1608, 0.2) |
And lastly, the final result is:
| Symbol | New "d" Interval |
| a | [0.1608, 0.16864) |
| b | [0.16864, 0.1804) |
| c | [0.1804, 0.18432) |
| d | [0.18432, 0.2) |
To decode the message, a similar algorithm is followed, except that the final number is given, and the symbols are decoded sequentially from that.