## A Big Number Library in C++

### Bill Seymour

2024-05-05

This document introduces three open-source numeric types for C++,
an unbounded integer,
a big decimal type
that can be used as either a fixed point or a floating point type,
and a rational number.
The code is distributed under the
Boost Software License
which isn’t viral as some others are said to be. (This is not
part of Boost. The author
just likes their open-source license.)

The `integer` and `decimal` types began their lives as C++ equivalents
of SQL’s `NUMERIC` and `DECIMAL` types for use in a database access library
that the author is working on, and are deliberately written to require no language or
standard library features beyond C++11;
the rational number began its life as part of a Technical Specification (“TS”,
a kind of warning about possible future standardization) that
WG21’s
numerics study group was once (but is no longer) working on
(P1889R1 §7).

(At present, the `rational` class doesn’t require anything past C++11 either,
but a possible future version that has NaNs and infinities would use C++20’s
spaceship operator
for comparisons.)

The `decimal` class uses `integer` as a type in which it can return very large
integers, and `rational` uses `integer` internally for its numerator and denominator
to help guard against overflow, so the author decided to put all three types into a single library.

The code has been tested with GCC (`__GNUC__` `==` `10`,
`__GNUC_MINOR__` `==` `2`) on Linux, and with a Microsoft compiler
(`_MSC_VER` `==` `1928`) on Windows. Both default to
C++14
but claim to support C++17 with appropriate command-line arguments.

The source code is provided in A ZIP archive
which contains:

- Documentation
- bignum.html – this paper
- integer.html
- decimal.html
- rational.html

- The
`integer` class
- integer.hpp
- integer_io.hpp
- integer.cpp

- The
`decimal` class
- decimal.hpp
- decimal_io.hpp
- decimal_parse.hpp
- decimal.cpp
- decimal_parse.cpp

- The
`rational` class
- rational.hpp
- rational_io.hpp
- rational.cpp
- rational_parse.cpp

- Common to more than one class
- bignum_common.hpp
- bignum_common.cpp
- bignum_type_traits.inc – not a proper header file, included inside the
`integer` and `decimal` classes

- LICENSE_1_0.txt – the Boost license

All suggestions and corrections will be welcome; all flames will be amusing.

Mail to was@pobox.com