Decimal, Binary, Hexadecimal, Octal . . . Roman

As a programmer, you encounter new counting bases: binary, hexadecimal, and perhaps octal. As a human, you use decimal, but decimal is only a recent invention. Before 1600 or so, if you worked with numbers you probably used Roman numerals.

The Roman counting system is base 10, like most human counting systems. Values are represented by characters, which was common during ancient times:

Letter Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000

The Romans lacked a symbol for zero, which didn’t seem to slow them down. Values were expressed left-to-right, with largest values first: MMXVII is 2017.

The Romans would tally values and then process a long number to reduce it. So if you’re counting the number of sesterces you collected in taxes, you might end up with a tally like:

XXXXXXXXXXXIIIIIII

A Roman tax collector would then squeeze down that value to:

LLXVII

And further to:

CXVII

Which translates to 117.

This system might seem crazy to us because decimal makes values so much easier to manipulate, but for the Romans it was practical. In fact, you can perform basic math by using Roman numerals — addition, subtraction, and multiplication — with relative ease. Also, remember that a lot of number manipulation was done on an abacus; paper was rare and expensive 2,000 years ago, which is another reason why a Roman would squeeze down a written value.

Further squeezing was also possible by reducing the values 900, 400, 90, 40, 9, and 4. These numbers can be expressed by using shortcuts CM, CD, XD, XL, IX, and IV, respectively. The reason was to save even more space. So Instead of writing:

CCCCLXXXXVIIII (499)

A Roman could write:

CDXDIX

Romans used a reduced expression only to save space, such as when chiseling a date into stone. Yet it somehow became common today to express all Roman numeral values with a preference toward this reduction. While it does save space, it makes manipulating Roman numerals more difficult. In fact, an ancient Roman would expand the number CDXDIX to CCCCLXXXXVIIII if he planned on doing any written math on the value.

All this Roman numeral stuff has nothing to do with C. Well, unless you remember that C is 100, but even that’s irrelevant: Like other programming languages, C has the capability to deal with decimal (plain and scientific notation), hexadecimal, octal, and that’s about it. For other counting bases, such as binary, you write functions to manipulate the values, which are represented as strings.

Can you write functions that read and write Roman numerals? Sure. The functions would have a degree of complexity to them, due to the flexible nature of Roman numerals, such as both strings “IIII” and “IV” representing the value 4. But like any programming task, it’s possible.

In next week’s Lesson, I present a function that translates Roman numeral input into decimal. Future Lessons explore translation the other way, including how to reduce Roman numeral values.

Leave a Reply