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';// 1606938044258990275541962092341162602522202993782792835301376template<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.
Parsing
Section titled “Parsing”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
0xor0bprefix with no digit after it, whether or not separators follow (0x,0x',-0b''; #2010).
- a run of signs (
- Separators:
'may stand anywhere among the hex and binary digits, so0x'0and0b1''0'are accepted. - Floating-point operands: a NaN or infinite operand, in
a + xas ina += x, converts to 0 aseinteger(x)does (#2010). - Failure:
parse()returns false and leavesvalueunchanged, asinteger’s parse does (#1988). - Zero: a negative zero in any radix (
-0,-0x0) is+0.
Division by zero
Section titled “Division by zero”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 >>= -2isa <<= 2. Since the value is sign-magnitude,>>=truncates toward zero:-5 >> 1is-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 wayeinteger(double)does, truncating toward zero, soa / 2.5isa / 2. - Conversion to
float,doubleandlong 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.
Where it is used
Section titled “Where it is used”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.