Pangolin
Header-only C++20 plane computational geometry library
Loading...
Searching...
No Matches
pgl::BigInt Class Reference

Arbitrary precision signed integer. More...

#include <bigint.hpp>

Public Member Functions

 BigInt ()=default
 Default constructor (zero).
 BigInt (pgl::detail::extended_integral auto value)
 Construct from any integer (including pgl::int128).
template<std::floating_point Float>
 BigInt (Float value)
 Construct from floating point, truncating toward zero.
bool isZero () const
 Whether the value is zero.
bool isOne () const
 Whether the value is one.
bool isNegative () const
 Whether the value is strictly negative.
bool fitsInt128 () const
 Whether the magnitude fits in a single pgl::int128.
bool fitsInt64 () const
 Whether the magnitude fits in 63 bits (i.e. in an int64_t).
bool fitsLimbs (std::size_t limbs) const
 Whether the magnitude occupies at most limbs heap limbs.
int sign () const
 Sign of the value: -1, 0, or 1.
BigInt abs () const
 Absolute value.
 operator pgl::int128 () const
 Convert to pgl::int128 (low bits, with sign, when it overflows).
template<std::signed_integral T>
 operator T () const
 Convert to any narrower signed integer (low bits, with sign).
 operator bool () const
 operator long double () const
 Convert to floating point.
 operator double () const
 operator float () const
BigInt operator- () const
BigInt operator+ () const
BigIntoperator+= (const BigInt &o)
BigIntoperator-= (const BigInt &o)
BigIntoperator*= (const BigInt &o)
BigIntoperator/= (const BigInt &o)
BigIntoperator%= (const BigInt &o)
BigIntoperator++ ()
BigInt operator++ (int)
BigIntoperator-- ()
BigInt operator-- (int)
std::strong_ordering operator<=> (const BigInt &o) const
bool operator== (const BigInt &o) const
template<std::floating_point Float>
bool operator== (Float f) const
 Equality with a floating-point value (true only when f is a whole number equal to this integer).
template<std::floating_point Float>
std::partial_ordering operator<=> (Float f) const
 Ordering against a floating-point value (partial: NaN is unordered).

Static Public Member Functions

static BigInt subGeneral (const BigInt &a, const BigInt &b)
 Cold out-of-line subtraction fallback (a magnitude spilled to limbs).
static BigInt mulGeneral (const BigInt &a, const BigInt &b, bool neg)
 Cold out-of-line schoolbook multiplication (a magnitude exceeds int128).

Friends

BigInt operator+ (const BigInt &a, const BigInt &b)
BigInt operator- (const BigInt &a, const BigInt &b)
BigInt operator* (const BigInt &a, const BigInt &b)
BigInt operator/ (const BigInt &a, const BigInt &b)
BigInt operator% (const BigInt &a, const BigInt &b)
std::ostream & operator<< (std::ostream &os, const BigInt &b)
std::istream & operator>> (std::istream &is, BigInt &b)

Detailed Description

Arbitrary precision signed integer.

The value is stored in sign-magnitude form:

  • negative_ holds the sign (always false when the value is zero);
  • when limbs_ is empty the magnitude is small_, a non-negative pgl::int128 (fast path, no allocation);
  • otherwise the magnitude is the little-endian base-2^62 number spelled by limbs_, whose most significant limb is non-zero and which represents a value too large to fit in a pgl::int128.

Constructor & Destructor Documentation

◆ BigInt() [1/3]

pgl::BigInt::BigInt ( )
default

Default constructor (zero).

◆ BigInt() [2/3]

pgl::BigInt::BigInt ( pgl::detail::extended_integral auto value)
inline

Construct from any integer (including pgl::int128).

◆ BigInt() [3/3]

template<std::floating_point Float>
pgl::BigInt::BigInt ( Float value)
inlineexplicit

Construct from floating point, truncating toward zero.

The fractional part is discarded (round toward zero); the integer part is kept exactly no matter how large the exponent is, so e.g. 2.0**200 is represented bit-for-bit rather than rounded to a double. The value must be finite. Marked explicit so a stray double never slips into BigInt arithmetic or comparisons implicitly.

Member Function Documentation

◆ abs()

BigInt pgl::BigInt::abs ( ) const
inline

Absolute value.

◆ fitsInt128()

bool pgl::BigInt::fitsInt128 ( ) const
inline

Whether the magnitude fits in a single pgl::int128.

◆ fitsInt64()

bool pgl::BigInt::fitsInt64 ( ) const
inline

Whether the magnitude fits in 63 bits (i.e. in an int64_t).

Used by Rational to decide when deferred reduction has become urgent: reducing operands while they still fit here keeps the impending multiply or cross-addition within the inline int128 store, off the heap limb path.

◆ fitsLimbs()

bool pgl::BigInt::fitsLimbs ( std::size_t limbs) const
inline

Whether the magnitude occupies at most limbs heap limbs.

A value in the inline int128 store occupies none, so fitsLimbs(0) is fitsInt128. Used by Rational to bound how large a deferred fraction may grow before reduction is worth its gcd.

◆ isNegative()

bool pgl::BigInt::isNegative ( ) const
inline

Whether the value is strictly negative.

◆ isOne()

bool pgl::BigInt::isOne ( ) const
inline

Whether the value is one.

Rational asks this of a denominator on every comparison and every arithmetic step, to take the integer shortcut, so it reads the inline store directly rather than building a BigInt to compare against.

◆ isZero()

bool pgl::BigInt::isZero ( ) const
inline

Whether the value is zero.

◆ mulGeneral()

BigInt pgl::BigInt::mulGeneral ( const BigInt & a,
const BigInt & b,
bool neg )
inlinestatic

Cold out-of-line schoolbook multiplication (a magnitude exceeds int128).

◆ operator bool()

pgl::BigInt::operator bool ( ) const
inlineexplicit

◆ operator double()

pgl::BigInt::operator double ( ) const
inlineexplicit

◆ operator float()

pgl::BigInt::operator float ( ) const
inlineexplicit

◆ operator long double()

pgl::BigInt::operator long double ( ) const
inlineexplicit

Convert to floating point.

◆ operator pgl::int128()

pgl::BigInt::operator pgl::int128 ( ) const
inlineexplicit

Convert to pgl::int128 (low bits, with sign, when it overflows).

◆ operator T()

template<std::signed_integral T>
pgl::BigInt::operator T ( ) const
inlineexplicit

Convert to any narrower signed integer (low bits, with sign).

Written over the concept rather than as one overload per fixed-width alias for the reason pgl::detail::_promote is: int, long and long long are three distinct types, and which two of them the aliases name changes with the data model, so naming aliases leaves the third without a conversion on every platform. That gap is only reachable from a coordinate type that promotes this far, which is how it stayed hidden.

◆ operator%=()

BigInt & pgl::BigInt::operator%= ( const BigInt & o)
inline

◆ operator*=()

BigInt & pgl::BigInt::operator*= ( const BigInt & o)
inline

◆ operator+()

BigInt pgl::BigInt::operator+ ( ) const
inline

◆ operator++() [1/2]

BigInt & pgl::BigInt::operator++ ( )
inline

◆ operator++() [2/2]

BigInt pgl::BigInt::operator++ ( int )
inline

◆ operator+=()

BigInt & pgl::BigInt::operator+= ( const BigInt & o)
inline

◆ operator-()

BigInt pgl::BigInt::operator- ( ) const
inline

◆ operator--() [1/2]

BigInt & pgl::BigInt::operator-- ( )
inline

◆ operator--() [2/2]

BigInt pgl::BigInt::operator-- ( int )
inline

◆ operator-=()

BigInt & pgl::BigInt::operator-= ( const BigInt & o)
inline

◆ operator/=()

BigInt & pgl::BigInt::operator/= ( const BigInt & o)
inline

◆ operator<=>() [1/2]

std::strong_ordering pgl::BigInt::operator<=> ( const BigInt & o) const
inline

◆ operator<=>() [2/2]

template<std::floating_point Float>
std::partial_ordering pgl::BigInt::operator<=> ( Float f) const
inline

Ordering against a floating-point value (partial: NaN is unordered).

◆ operator==() [1/2]

bool pgl::BigInt::operator== ( const BigInt & o) const
inline

◆ operator==() [2/2]

template<std::floating_point Float>
bool pgl::BigInt::operator== ( Float f) const
inline

Equality with a floating-point value (true only when f is a whole number equal to this integer).

◆ sign()

int pgl::BigInt::sign ( ) const
inline

Sign of the value: -1, 0, or 1.

◆ subGeneral()

BigInt pgl::BigInt::subGeneral ( const BigInt & a,
const BigInt & b )
inlinestatic

Cold out-of-line subtraction fallback (a magnitude spilled to limbs).

◆ operator%

BigInt operator% ( const BigInt & a,
const BigInt & b )
friend

◆ operator*

BigInt operator* ( const BigInt & a,
const BigInt & b )
friend

◆ operator+

BigInt operator+ ( const BigInt & a,
const BigInt & b )
friend

◆ operator-

BigInt operator- ( const BigInt & a,
const BigInt & b )
friend

◆ operator/

BigInt operator/ ( const BigInt & a,
const BigInt & b )
friend

◆ operator<<

std::ostream & operator<< ( std::ostream & os,
const BigInt & b )
friend

◆ operator>>

std::istream & operator>> ( std::istream & is,
BigInt & b )
friend