Common Bignum Code

Bill Seymour
2026-10-29


Introduction

This paper documents the open-source code declared in bignum_common.hpp that’s used in the integer, decimal and rational classes in the author’s big number library.

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 permissive open-source license.)

All symbols are declared in the bignum namespace.  The throw_overflow function is defined in bignum_common.cpp.

The code can be compiled with any C++ implementation that conforms to C++11 or later.


Rounding modes

enum rounding
{
    all_to_neg_inf, all_to_pos_inf,
    all_to_zero, all_away_zero,
    all_to_even, all_to_odd,
    tie_to_neg_inf, tie_to_pos_inf,
    tie_to_zero, tie_away_zero,
    tie_to_even, tie_to_odd
};
This enumeration is a subset of one proposed in WG21’s P1889R1 §3.3.  Note that it’s unscoped, so the enumerators are in the bignum namespace.

This is used in the decimal and rational classes.


Additional type traits

The following additional type traits are defined in the traits namespace (which in turn is inside the bignum namespace).  They’re used to support SFINAE in templates that are used for converting to and from fundamental types.
template<typename T>
using is_bool = std::is_same<typename std::remove_cv<T>::type, bool>;
Whether T is a (possibly const and/or volatile) bool.
template<typename T> using is_flt = std::is_floating_point<T>;
Whether T is a fundamental floating point type.
template<typename T>
struct is_sgn : std::integral_constant<bool,
  std::is_signed<T>::value && !is_flt<T>::value> { };
Whether T is a fundamental signed integral type.
template<typename T>
struct is_uns : std::integral_constant<bool,
  std::is_unsigned<T>::value && !is_bool<T>::value> { };
Whether T is a fundamental unsigned type other than bool.
template<typename T>
struct is_int : std::integral_constant<bool,
  is_sgn<T>::value || is_uns<T>::value> { };
Whether T is a fundamental integral type other than bool.


Human-readable type names

Unfortunately, std::type_info::name() isn’t always human-readable, so we provide the following in the types namespace (which in turn is inside the bignum namespace).
template<typename T> struct name
{
    static constexpr char value[] = "unknown type";
};
template<> struct name<bool>
{
    static constexpr char value[] = "bool";
};
And similar specializations for all of the usual fundamental arithmetic and character types (including char8_t if you happen to be compiling with a C++ implementation that conforms to C++20 or later).  Listing them all would just be tedious and wouldn’t tell you anything that you don’t already know. 😊


A function that throws a std::overflow_error

[[noreturn]] void throw_overflow(const char*, const char*);
This function is used internally by the integer, decimal and rational classes to report overflow when converting to fundamental types.

The first argument is expected to be the type that we’re converting from; the second argument is expected to be the type that we’re converting to (probably one of the human-readable type names above).  This is used to create an informative overflow_error::what() message.


All suggestions and corrections will be welcome; all flames will be amusing.
Mail to was@pobox.com.