Constructing Number Systems from First Principles
In the beginning, there was zero.
The beginning of developing an axiomatic number system starts with the natural numbers { 0, 1, 2, 3, ... }, which are constructed using the Peano Axioms. In this approach, we define everything in terms of a "starting element" 0 and a "successor function" S(a). This set, combined with the addition operation, forms a commutative monoid, but fails to form an addative group, lacking inverses.
To extend the natural numbers into a full ring, we include additive inverses (a.k.a. "negatives") for all the natural numbers. This yields the ring of integers.
Similarly to how the natural numbers lacked additive inverses to make it a group, the integers lack multiplicative inverses to make it a field. As the integers form an integral domain, we can extend them to a field of fractions, yielding the rational numbers.
The limitations of the algebraic representation are obvious if you try to create a natural number for 10,000: Stack Overflow! It turns out this is a very inefficient way to represent numbers.
The next question I had is how can we define an infinite precision number system. Fixed-size approximations such as Float
or Double
are just that; addition is technically not even associative.
To solve this problem, we first create an object Truncation that represents any finite length decimal. This is modeled after IEEE floating points, with a sign bit, exponent, and significant. However, for infinite precision, the length of the significant is unbounded. However, as it is a vector, it must be finite. Along with these truncations, we define a Zero object. These are captured in Scala's BigDecimal
type.