All Classes Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
gears::math::uintx< Bits, Digit, Digits > Struct Template Reference

Multi-precision unsigned integer. More...

Public Member Functions

 uintx ()
 Default constructor. More...
 
template<typename Integer , gears::meta::EnableIf< std::is_integral< Integer >> = gears::meta::_>
 uintx (Integer value)
 Constructs from an integer. More...
 
 uintx (const std::string &s)
 Constructs from a string. More...
 
 operator bool () const noexcept
 Checks if uintx is greater than 0. More...
 
uintxoperator+= (const uintx &other)
 Adds the contents of another uintx. More...
 
uintx operator+ (const uintx &other) const
 
uintxoperator-= (const uintx &other)
 Subtracts the contents of another uintx. More...
 
uintx operator- (const uintx &other) const
 
uintxoperator*= (const uintx &other)
 Multiplies the contents of another uintx. More...
 
uintx operator* (const uintx &other) const
 
uintxoperator/= (const uintx &other)
 Divides the contents of another uintx. More...
 
uintx operator/ (const uintx &other) const
 
uintxoperator%= (const uintx &other)
 Implements modulo arithmetic. More...
 
uintx operator% (const uintx &other) const
 
bool operator== (const uintx &other) const
 Logically compares two uintx objects.
 
bool operator!= (const uintx &other) const
 
bool operator< (const uintx &other) const
 
bool operator> (const uintx &other) const
 
bool operator<= (const uintx &other) const
 
bool operator>= (const uintx &other) const
 
uintx operator++ (int)
 Increments a uintx object by one.
 
const uintxoperator++ ()
 
uintx operator-- (int)
 Decrements a uintx by one. More...
 
const uintxoperator-- ()
 

Static Public Attributes

static constexpr size_t digits10 = std::numeric_limits<Digit>::digits10
 
static constexpr size_t base = detail::pow(10, digits10)
 

Friends

template<typename Elem , typename Traits >
std::basic_ostream< Elem,
Traits > & 
operator<< (std::basic_ostream< Elem, Traits > &out, const uintx &n)
 
template<typename Elem , typename Traits >
std::basic_istream< Elem,
Traits > & 
operator>> (std::basic_istream< Elem, Traits > &in, uintx &n)
 
template<typename T , size_t N, typename U , typename V >
uintx_cast (const uintx< N, U, V > &obj)
 Casts a uintx to another type. More...
 

Detailed Description

template<size_t Bits = static_cast<size_t>(-1), typename Digit = unsigned int, typename Digits = unsigned long long>
struct gears::math::uintx< Bits, Digit, Digits >

Multi-precision unsigned integer. Basic usage does not need to modify the Digit or Digits template parameters, those are there for memory optimisations on rare cases when necessary. Both types should be unsigned if provided.

This multi-precision integer overloads all mathematical operators except the bitwise operators. The streaming operators operator<< and operator>> are provided as well. In order to disable the streaming operators, define GEARS_NO_IOSTREAM before including the file.

All operations do a "bit-check" after their operations to see if the result fits in the bits provided. The "bit-check" in reality just truncates the result to however many digits can fit under Bits / (sizeof(Digit) * 8). It is unclear if this behaviour should change at the moment although it might in the future. A Bits parameter of -1 will avoid the "bit-check".

There are two user-defined literals provided under gears::math::literals help with the construction of a uintx<>. An example is provided below.

using namespace gears::math::literals; // required
auto one = 1902748734897198347123_x; // recommended way
auto two = "89374128732819928123"_x; // also provided
Template Parameters
BitsBits of precision needed. Defaults to -1 for "infinite" precision.
DigitUnderlying type to store its bit properties.
DigitsUnderlying type to store its numeric properties.

Definition at line 136 of file uintx.hpp.

Constructor & Destructor Documentation

template<size_t Bits = static_cast<size_t>(-1), typename Digit = unsigned int, typename Digits = unsigned long long>
gears::math::uintx< Bits, Digit, Digits >::uintx ( )
inline

Sets uintx to 0.

Definition at line 293 of file uintx.hpp.

template<size_t Bits = static_cast<size_t>(-1), typename Digit = unsigned int, typename Digits = unsigned long long>
template<typename Integer , gears::meta::EnableIf< std::is_integral< Integer >> = gears::meta::_>
gears::math::uintx< Bits, Digit, Digits >::uintx ( Integer  value)
inline

Constructs uintx from an integer type. uintx is then set to the value provided. No bit-checking is done in this constructor. If the value is negative, then it is made positive before hand.

Parameters
valueValue to set uintx to.

Definition at line 304 of file uintx.hpp.

template<size_t Bits = static_cast<size_t>(-1), typename Digit = unsigned int, typename Digits = unsigned long long>
gears::math::uintx< Bits, Digit, Digits >::uintx ( const std::string &  s)
inline

Constructs uintx from a string type. uintx is then set to the value provided from the string. If the string contains letters or special characters, the behaviour is undefined. The string must not represent a negative integer. Bit-checking is done in this constructor.

Parameters
sString to set the uintx to.

Definition at line 323 of file uintx.hpp.

Member Function Documentation

template<size_t Bits = static_cast<size_t>(-1), typename Digit = unsigned int, typename Digits = unsigned long long>
gears::math::uintx< Bits, Digit, Digits >::operator bool ( ) const
inlineexplicitnoexcept

Checks if uintx is greater than 0.

Returns
true if the internal value is greater than 0, false otherwise.

Definition at line 596 of file uintx.hpp.

template<size_t Bits = static_cast<size_t>(-1), typename Digit = unsigned int, typename Digits = unsigned long long>
uintx& gears::math::uintx< Bits, Digit, Digits >::operator%= ( const uintx< Bits, Digit, Digits > &  other)
inline

Implements modulo arithmetic.

Exceptions
std::logic_errorThrown when division by zero occurs.
Parameters
otherThe left hand side to use modulus on.

Definition at line 487 of file uintx.hpp.

template<size_t Bits = static_cast<size_t>(-1), typename Digit = unsigned int, typename Digits = unsigned long long>
uintx& gears::math::uintx< Bits, Digit, Digits >::operator*= ( const uintx< Bits, Digit, Digits > &  other)
inline

Multiplies the contents of another uintx.

Parameters
otherThe left hand side to multiply with.

Definition at line 403 of file uintx.hpp.

template<size_t Bits = static_cast<size_t>(-1), typename Digit = unsigned int, typename Digits = unsigned long long>
uintx& gears::math::uintx< Bits, Digit, Digits >::operator+= ( const uintx< Bits, Digit, Digits > &  other)
inline

Adds the contents of another uintx.

Parameters
otherThe left hand side to add with.

Definition at line 347 of file uintx.hpp.

template<size_t Bits = static_cast<size_t>(-1), typename Digit = unsigned int, typename Digits = unsigned long long>
uintx gears::math::uintx< Bits, Digit, Digits >::operator-- ( int  )
inline

Decrements a uintx by one. If the value ends up being negative, the behaviour is undefined.

Definition at line 579 of file uintx.hpp.

template<size_t Bits = static_cast<size_t>(-1), typename Digit = unsigned int, typename Digits = unsigned long long>
uintx& gears::math::uintx< Bits, Digit, Digits >::operator-= ( const uintx< Bits, Digit, Digits > &  other)
inline

Subtracts the contents of another uintx. If the result would end up being a negative value, the behaviour is undefined.

Parameters
otherThe left hand side to subtract with.

Definition at line 381 of file uintx.hpp.

template<size_t Bits = static_cast<size_t>(-1), typename Digit = unsigned int, typename Digits = unsigned long long>
uintx& gears::math::uintx< Bits, Digit, Digits >::operator/= ( const uintx< Bits, Digit, Digits > &  other)
inline

Divides the contents of another uintx.

Exceptions
std::logic_errorThrown when division by zero occurs.
Parameters
otherThe left hand side to divide with.

Definition at line 450 of file uintx.hpp.

Friends And Related Function Documentation

template<size_t Bits = static_cast<size_t>(-1), typename Digit = unsigned int, typename Digits = unsigned long long>
template<typename T , size_t N, typename U , typename V >
T uintx_cast ( const uintx< N, U, V > &  obj)
friend

Casts a uintx to another type. This type can be an integer type or a string type. To cast to a string, uintx_cast<std::string> must be used. If the underlying value of uintx does not fit in the type casted, then the behaviour is undefined.

Parameters
objObject to cast.
Template Parameters
TType to cast to.
Returns
Casted value.

Definition at line 641 of file uintx.hpp.