Skip to content
Dave Hudson edited this page Apr 1, 2017 · 2 revisions

Documentation

c8 is an arbitrary precision arithmetic library written in C++. It provides 3 public classes, c8::natural, c8::integer, and c8::rational. These types represent natural numbers (0 and positive integers), integers, and rational numbers respectively.

Aside from constructors and destructors, the general approach within the library is to support standard C++ operators. Each class has additional methods where they make sense, however.

Implementation

The code is implemented in 4 layers. These are digit arrays, and the 3 public classes. They form a hierarchy, with c8::natural being composed from digit arrays, c8::integer being composed from c8::natural, and c8::rational being composed from c8::integer.

Digit Arrays

Much like the sort of maths that we might learn in school, all of the operations implemented here are done in terms of digits. Digits are very much larger in this case, but the principles involved are identical. The larger the digits, however, the faster it is to compute results.

While individual digits are useful for small numbers, we need to use more than one digit to represent larger numbers. Within this library this larger representation is a digit array.

Within modern C++ software we strive to avoid exposing low-level implementation details and the digit array layer is a good example of why. In order to be fast, it makes extensive use of pointer operations that would often be deemed unsafe were they not very carefully controlled. In this instance they are entirely controlled by the functions in the c8::natural class, and that class is designed to use digit arrays in a safe way. Digit arrays are also intended to be able to support fast inline-assembler implementations so it would be inappropriate to try to force them to use more complex interfaces.

Clone this wiki locally