Cardinal, Ordinal, and Nominal numbers in software engineering

In Uncategorized on February 1, 2010 at 9:15 pm

I have two hands. I was the second person to arrive in the morning. My employee ID is 2.

Cardinal numbers are used for counting.

Ordinal numbers are used for ordering items.

Nominal numbers name things.

Even in regular prose we distinguish between cardinal and ordinal numbers.

Number Operations Supported
Cardinal Addition, Subtraction1, Comparison
Ordinal Comparison

The only language that I know of where I could easily restrict operations on numbers is Ada. I don’t like Ada. I think that’s because we misused Ada at work. All cardinal numbers were new types, and explicit coercions were strewn throughout the code base.

For instance, the work philosophy would have me create:

  1. Type Fan_Count to count the number of fans in a system (range 1..10).
  2. Type Fan_Index to index into an array of fans (range 1..10).
  3. Type Fan_Controller_Count to count the number of fan controllers (range 1..10).
  4. Type Fan_Controller_Index to index into an array of fan controllers (range 1..10).

We never created a Fan_Identifier type.

Now, the problems with the above were:

  • What if I can’t communicate with any of the fans? I can’t represent 0 fans.
  • I want to count the fans in two chassis. All of a sudden I overflowed the bounds of Fan_Count. Likewise with Fan_Controller_Count.
  • I want to count the total number of fans and fan controllers in a chassis. I need to cast both to an integer.
  • I want to look up some fault code depending on the number of fans, or fan controllers that have failed on a system. I needed two arrays containing exactly the same information, or I needed a cast.

Over time I came to believe that we should use natural numbers (Natural) for counting.

These days I program in the C programming language and I have the opposite problem. I wish for some type checking. Most C code conflates the index of something, with the count of something, or the index of something, with the identity of something.

1. Other operations such as multiplication and division may be supported.

%d bloggers like this: