Skip to content

einteger: Adaptive-Precision Integer

integer<N> fixes its width at compile time, which is what you want when the number has to fit a register or an interchange format. When it does not – factorials, combinatorics, exact rational arithmetic, cryptographic sizes, or the exact oracles this library scores its own floating-point types against – you want an integer that simply grows.

einteger allocates limbs as the value needs them. There is no overflow, only memory.

#include <universal/number/einteger/einteger.hpp>
using namespace sw::universal;
einteger<std::uint32_t> two(2), p(1);
for (int i = 0; i < 200; ++i) p = p * two;
std::cout << p << '\n';
// 1606938044258990275541962092341162602522202993782792835301376
template<typename BlockType = std::uint32_t>
class einteger;

BlockType is the limb type the magnitude is stored in. The value is sign-magnitude, so the limb array holds the magnitude and the sign rides alongside – there is no two’s complement wrap, and no most-negative-value asymmetry.

Property Value
Width grows with the value
Overflow none
Representation sign + magnitude limb array
Default limb std::uint32_t

Full arithmetic, comparison, and decimal/hex parsing and printing are provided. ipow(a, b) is the exact integer power, with an einteger or long long exponent and the same edge cases as integer’s; it never overflows. showLimbs() prints the limb encoding when you need to see the representation.

parse(str, value) reads the whole string, with at most one leading sign:

Form Example Value
decimal -1234 -1234
octal: a leading 0 selects it, C-style 0777, 00777 511
hex, with ' digit separators 0xFF'FF 65535
binary, with ' digit separators -0b1010'0101 -165
  • Rejected input:
    • a run of signs (--5, -+0x5);
    • a leading-zero string with a digit outside [0-7] (08, 0749);
    • a 0x or 0b prefix with no digit after it, whether or not separators follow (0x, 0x', -0b''; #2010).
  • Separators: ' may stand anywhere among the hex and binary digits, so 0x'0 and 0b1''0' are accepted.
  • Floating-point operands: a NaN or infinite operand, in a + x as in a += x, converts to 0 as einteger(x) does (#2010).
  • Failure: parse() returns false and leaves value unchanged, as integer’s parse does (#1988).
  • Zero: a negative zero in any radix (-0, -0x0) is +0.

einteger has no NaN or infinity to carry a failed division in the value, so by default a division by zero throws einteger_divide_by_zero. A caller that would rather get a value opts out before the include, with EINTEGER_THROW_DIVIDE_BY_ZERO 0, or with EINTEGER_THROW_ARITHMETIC_EXCEPTION 0 when the first is not set:

#define EINTEGER_THROW_DIVIDE_BY_ZERO 0
#include <universal/number/einteger/einteger.hpp>

A build with exceptions disabled (-fno-exceptions) gets the value without asking: EINTEGER_THROW_DIVIDE_BY_ZERO defaults to UNIVERSAL_HAS_EXCEPTIONS, which <universal/utility/exceptions.hpp> takes from the compiler. Setting it to 1 in such a build stops with an #error (#1925).

With the exception off, x / 0 and x % 0 are 0, as are both outputs of reduce(). Nothing is printed (#1894).

Shifts, native operands and conversion to float

Section titled “Shifts, native operands and conversion to float”
  • Shifts. <<= and >>= scale the magnitude by 2^k and keep the sign. A negative count shifts the other way: a >>= -2 is a <<= 2. Since the value is sign-magnitude, >>= truncates toward zero: -5 >> 1 is -2, where a native arithmetic shift rounds toward -inf and gives -3. A negative value shifted to zero is +0 (#1987).
  • Native operands. + - * / % take any native arithmetic type on either side (a / 2, 7 - a, a * 2u). An integer operand converts exactly. A floating-point operand converts the way einteger(double) does, truncating toward zero, so a / 2.5 is a / 2.
  • Conversion to float, double and long double. The result is rounded once, to nearest with ties to even, as a native 64-bit integer converts. A value beyond the target’s range becomes infinity.

einteger backs the exact-arithmetic oracles used to validate the floating-point types. When a test needs to decide whether a qd or ereal result is correct, it computes the same quantity as an exact dyadic rational over einteger and compares – so no floating-point sits between an implementation and its own score.

  • integer – fixed-width, for hardware-shaped values
  • edecimal – adaptive integer in base 10
  • erational – exact ratios of adaptive integers