piranha  0.10
Public Member Functions | Static Public Attributes | Friends | List of all members
mppp::mp_integer< SSize > Class Template Reference

Multiprecision integer class. More...

#include <piranha/mppp/mp++.hpp>

Public Member Functions

 mp_integer ()=default
 Default constructor. More...
 
 mp_integer (const mp_integer &other)=default
 Copy constructor. More...
 
 mp_integer (mp_integer &&other)=default
 Move constructor. More...
 
template<typename T , generic_ctor_enabler< T > = 0>
 mp_integer (T x)
 Generic constructor. More...
 
 mp_integer (const char *s, int base=10)
 Constructor from C string. More...
 
 mp_integer (const std::string &s, int base=10)
 Constructor from C++ string (equivalent to the constructor from C string). More...
 
 mp_integer (const ::mpz_t n)
 Constructor from mpz_t. More...
 
mp_integeroperator= (const mp_integer &other)=default
 Copy assignment operator. More...
 
mp_integeroperator= (mp_integer &&other)=default
 Move assignment operator. More...
 
template<typename T , generic_assignment_enabler< T > = 0>
mp_integeroperator= (const T &x)
 Generic assignment operator. More...
 
bool is_static () const
 Test for static storage. More...
 
bool is_dynamic () const
 Check for dynamic storage. More...
 
std::string to_string (int base=10) const
 Conversion to string. More...
 
template<typename T , generic_conversion_enabler< T > = 0>
 operator T () const
 Generic conversion operator. More...
 
bool promote ()
 Promote to dynamic storage. More...
 
bool demote ()
 Demote to static storage. More...
 
std::size_t nbits () const
 Size in bits. More...
 
std::size_t size () const
 Size in limbs. More...
 
int sgn () const
 Sign. More...
 
mpz_view get_mpz_view () const
 Get an mpz_t view. More...
 
mp_integerneg ()
 Negate in-place. More...
 
mp_integer operator+ () const
 Identity operator. More...
 
template<typename T , in_place_enabler< T > = 0>
mp_integeroperator+= (const T &op)
 In-place addition operator. More...
 
mp_integeroperator++ ()
 Prefix increment. More...
 
mp_integer operator++ (int)
 Suffix increment. More...
 
mp_integer operator- () const
 Negated copy. More...
 
template<typename T , in_place_enabler< T > = 0>
mp_integeroperator-= (const T &op)
 In-place subtraction operator. More...
 
mp_integeroperator-- ()
 Prefix decrement. More...
 
mp_integer operator-- (int)
 Suffix decrement. More...
 
template<typename T , in_place_enabler< T > = 0>
mp_integeroperator*= (const T &op)
 In-place multiplication operator. More...
 
template<typename T , in_place_enabler< T > = 0>
mp_integeroperator/= (const T &d)
 In-place division operator. More...
 
template<typename T , in_place_mod_enabler< T > = 0>
mp_integeroperator%= (const T &d)
 In-place modulo operator. More...
 
template<typename T , shift_op_enabler< T > = 0>
mp_integeroperator<<= (T s)
 In-place left shift operator. More...
 
template<typename T , shift_op_enabler< T > = 0>
mp_integeroperator>>= (T s)
 In-place right shift operator. More...
 
mp_integerabs ()
 In-place absolute value. More...
 
mp_integernextprime ()
 Compute next prime number (in-place version). More...
 
int probab_prime_p (int reps=25) const
 Test primality. More...
 
mp_integersqrt ()
 Integer square root (in-place version). More...
 
bool odd_p () const
 Test if value is odd. More...
 
bool even_p () const
 Test if value is even. More...
 
mppp_impl::integer_union< SSize > & _get_union ()
 Return a reference to the internal union. More...
 
const mppp_impl::integer_union< SSize > & _get_union () const
 Return a const reference to the internal union. More...
 
std::remove_extent<::mpz_t >::type * get_mpz_t ()
 Get a pointer to the dynamic storage. More...
 
bool is_zero () const
 Test if the value is zero. More...
 
bool is_one () const
 Test if the value is equal to one. More...
 
bool is_negative_one () const
 Test if the value is equal to minus one. More...
 

Static Public Attributes

static constexpr std::size_t ssize = SSize
 Alias for the template parameter SSize.
 

Friends

std::ostream & operator<< (std::ostream &os, const mp_integer &n)
 Output stream operator for mp_integer. More...
 
std::istream & operator>> (std::istream &is, mp_integer &n)
 Input stream operator for mp_integer. More...
 
int sgn (const mp_integer &n)
 Sign function. More...
 
void neg (mp_integer &rop, const mp_integer &n)
 Binary negation. More...
 
mp_integer neg (const mp_integer &n)
 Unary negation. More...
 
void add (mp_integer &rop, const mp_integer &op1, const mp_integer &op2)
 Ternary add. More...
 
template<typename T , typename U >
common_t< T, U > operator+ (const T &op1, const U &op2)
 Binary addition operator. More...
 
template<typename T , in_place_lenabler< T > = 0>
T & operator+= (T &x, const mp_integer &n)
 In-place addition for interoperable types. More...
 
void sub (mp_integer &rop, const mp_integer &op1, const mp_integer &op2)
 Ternary subtraction. More...
 
template<typename T , typename U >
common_t< T, U > operator- (const T &op1, const U &op2)
 Binary subtraction operator. More...
 
template<typename T , in_place_lenabler< T > = 0>
T & operator-= (T &x, const mp_integer &n)
 In-place subtraction for interoperable types. More...
 
void add_ui (mp_integer &rop, const mp_integer &op1, unsigned long op2)
 Ternary add with unsigned long. More...
 
void mul (mp_integer &rop, const mp_integer &op1, const mp_integer &op2)
 Ternary multiplication. More...
 
template<typename T , typename U >
common_t< T, U > operator* (const T &op1, const U &op2)
 Binary multiplication operator. More...
 
template<typename T , in_place_lenabler< T > = 0>
T & operator*= (T &x, const mp_integer &n)
 In-place multiplication for interoperable types. More...
 
void addmul (mp_integer &rop, const mp_integer &op1, const mp_integer &op2)
 Ternary multiply–accumulate. More...
 
void tdiv_qr (mp_integer &q, mp_integer &r, const mp_integer &n, const mp_integer &d)
 Ternary truncated division. More...
 
template<typename T , typename U >
common_t< T, U > operator/ (const T &n, const U &d)
 Binary division operator. More...
 
template<typename T , in_place_lenabler< T > = 0>
T & operator/= (T &x, const mp_integer &n)
 In-place division for interoperable types. More...
 
template<typename T , typename U >
common_mod_t< T, U > operator% (const T &n, const U &d)
 Binary modulo operator. More...
 
void mul_2exp (mp_integer &rop, const mp_integer &n, ::mp_bitcnt_t s)
 Ternary left shift. More...
 
template<typename T , shift_op_enabler< T > = 0>
mp_integer operator<< (const mp_integer &n, T s)
 Left shift operator. More...
 
void tdiv_q_2exp (mp_integer &rop, const mp_integer &n, ::mp_bitcnt_t s)
 Ternary right shift. More...
 
template<typename T , shift_op_enabler< T > = 0>
mp_integer operator>> (const mp_integer &n, T s)
 Right shift operator. More...
 
int cmp (const mp_integer &op1, const mp_integer &op2)
 Comparison function for mp_integer. More...
 
template<typename T , typename U , rel_enabler< T, U > = 0>
bool operator== (const T &op1, const U &op2)
 Equality operator. More...
 
template<typename T , typename U , rel_enabler< T, U > = 0>
bool operator!= (const T &op1, const U &op2)
 Inequality operator. More...
 
template<typename T , typename U , rel_enabler< T, U > = 0>
bool operator< (const T &op1, const U &op2)
 Less-than operator. More...
 
template<typename T , typename U , rel_enabler< T, U > = 0>
bool operator>= (const T &op1, const U &op2)
 Greater-than or equal operator. More...
 
template<typename T , typename U , rel_enabler< T, U > = 0>
bool operator> (const T &op1, const U &op2)
 Greater-than operator. More...
 
template<typename T , typename U , rel_enabler< T, U > = 0>
bool operator<= (const T &op1, const U &op2)
 Less-than or equal operator. More...
 
void pow_ui (mp_integer &rop, const mp_integer &base, unsigned long exp)
 Ternary exponentiation. More...
 
mp_integer pow_ui (const mp_integer &base, unsigned long exp)
 Binary exponentiation. More...
 
template<typename T , typename U >
common_t< T, U > pow (const T &base, const U &exp)
 Generic binary exponentiation. More...
 
void abs (mp_integer &rop, const mp_integer &n)
 Binary absolute value. More...
 
mp_integer abs (const mp_integer &n)
 Unary absolute value. More...
 
std::size_t hash (const mp_integer &n)
 Hash value. More...
 
void nextprime (mp_integer &rop, const mp_integer &n)
 Compute next prime number (binary version). More...
 
mp_integer nextprime (const mp_integer &n)
 Compute next prime number (unary version). More...
 
int probab_prime_p (const mp_integer &n, int reps=25)
 Test primality. More...
 
void sqrt (mp_integer &rop, const mp_integer &n)
 Integer square root (binary version). More...
 
mp_integer sqrt (const mp_integer &n)
 Integer square root (unary version). More...
 
bool odd_p (const mp_integer &n)
 Test if integer is odd. More...
 
bool even_p (const mp_integer &n)
 Test if integer is even. More...
 
void fac_ui (mp_integer &rop, unsigned long n)
 Factorial. More...
 
void bin_ui (mp_integer &rop, const mp_integer &n, unsigned long k)
 Binomial coefficient (ternary version). More...
 
mp_integer bin_ui (const mp_integer &n, unsigned long k)
 Binomial coefficient (binary version). More...
 
template<typename T , typename U , binomial_enabler< T, U > = 0>
mp_integer binomial (const T &n, const U &k)
 Generic binomial coefficient. More...
 
void divexact (mp_integer &rop, const mp_integer &n, const mp_integer &d)
 Exact division (ternary version). More...
 
mp_integer divexact (const mp_integer &n, const mp_integer &d)
 Exact division (binary version). More...
 
void gcd (mp_integer &rop, const mp_integer &op1, const mp_integer &op2)
 GCD (ternary version). More...
 
mp_integer gcd (const mp_integer &op1, const mp_integer &op2)
 GCD (binary version). More...
 
bool is_zero (const mp_integer &n)
 Test if an mppp::mp_integer is zero. More...
 
bool is_one (const mp_integer &n)
 Test if an mppp::mp_integer is equal to one. More...
 
bool is_negative_one (const mp_integer &n)
 Test if an mppp::mp_integer is equal to minus one. More...
 

Detailed Description

template<std::size_t SSize>
class mppp::mp_integer< SSize >

Multiprecision integer class.

This class represent arbitrary-precision signed integers. It acts as a wrapper around the GMP mpz_t type, with a small value optimisation: integers whose size is up to SSize limbs are stored directly in the storage occupied by the mp_integer object, without resorting to dynamic memory allocation. The value of SSize must be at least 1 and less than an implementation-defined upper limit.

When the value of an mp_integer is stored directly within the object, the storage type of the integer is said to be static. When the limb size of the integer exceeds the maximum value SSize, the storage types becomes dynamic. The transition from static to dynamic storage happens transparently whenever the integer value becomes large enough. The demotion from dynamic to static storage usually needs to be requested explicitly. For values of SSize of 1 and 2, optimised implementations of basic arithmetic operations are employed, if supported by the target architecture and if the storage type is static. For larger values of SSize, the mpn_ low-level functions of the GMP API are used if the storage type is static. If the storage type is dynamic, the usual mpz_ functions from the GMP API are used.

Interoperable types

This class has the look and feel of a C++ builtin type: it can interact with most of C++'s integral and floating-point primitive types, and it provides overloaded arithmetic operators. Differently from the builtin types, however, this class does not allow any implicit conversion to/from other types (apart from bool): construction from and conversion to primitive types must always be requested explicitly. As a side effect, syntax such as

mp_integer<1> n = 5;
int m = n;

will not work, and direct initialization and explicit casting should be used instead:

mp_integer<1> n{5};
int m = static_cast<int>(n);

The full list of interoperable builtin types is:

API

Most of the functionality of this class is exposed via inline friend functions, with the general convention that the functions are named after the corresponding GMP functions minus the leading mpz_ prefix. For instance, the GMP call

mpz_add(rop,a,b);

that writes the result of a+b into rop becomes simply

add(rop,a,b);

where the add() function is resolved via argument-dependent lookup. Function calls with overlapping arguments are allowed, unless noted otherwise.

Multiple overloads of the same functionality are often available. Binary functions in GMP are usually implemented via three-arguments functions, in which the first argument is a reference to the return value. The exponentiation function mpz_pow_ui(), for instance, takes three arguments: the return value, the base and the exponent. There are two overloads of the corresponding pow_ui() function:

This allows to avoid having to set up a return value for one-off invocations of pow_ui() (the binary overload will do it for you). For example:

mp_integer<1> r1, r2, n{3};
pow_ui(r1,n,2); // Ternary pow_ui(): computes n**2 and stores the result in r1.
r2 = pow_ui(n,2); // Binary pow_ui(): returns n**2, which is then assigned to r2.

In case of unary functions, there are often three overloads available:

For instance, here are three possible ways of computing the absolute value:

mp_integer<1> r1, r2, n{-5};
abs(r1,n); // Binary abs(): computes and stores the absolute value of n into r1.
r2 = abs(n); // Unary abs(): returns the absolute value of n, which is then assigned to r2.
n.abs(); // Member function abs(): replaces the value of n with its absolute value.

Note that at this time only a small subset of the GMP API has been wrapped by mp_integer.

Overloaded operators

This class provides overloaded operators for the basic arithmetic operations, including bit shifting. The binary operators are implemented as inline friend functions, the in-place operators are implemented as member functions. The overloaded operators are resolved via argument-dependent lookup whenever at least one argument is of type mp_integer, and the other argument is either another mp_integer or an instance of an interoperable type.

For the common arithmetic operations (+, -, * and /), the type promotion rules are a natural extension of the corresponding rules for native C++ types: if the other argument is a C++ integral, the result will be of type mp_integer, if the other argument is a C++ floating-point the result will be of the same floating-point type. For example:

mp_integer<1> n1{1}, n2{2};
auto res1 = n1 + n2; // res1 is an mp_integer
auto res2 = n1 * 2; // res2 is an mp_integer
auto res3 = 2 - n2; // res3 is an mp_integer
auto res4 = n1 / 2.f // res4 is a float
auto res5 = 12. / n1 // res5 is a double

The modulo operator % accepts only mp_integer and interoperable integral types as arguments, and it always returns mp_integer as result. The bit shifting operators << and >> accept only interoperable integral types as shift arguments, and they always return mp_integer as result.

The relational operators, ==, !=, <, >, <= and >= will promote the arguments to a common type before comparing them. The promotion rules are the same as in the arithmetic operators (that is, both arguments are promoted to mp_integer if they are both integral types, otherwise they are promoted to the type of the floating-point argument).

Interfacing with GMP

This class provides facilities to interface with the GMP library. Specifically, mp_integer features:

The mpz_view class represent a read-only view of an mp_integer object which is implicitly convertible to the type const mpz_t and which is thus usable as an argument to GMP functions. For example:

mpz_t m;
mpz_init_set_si(m,1); // Create an mpz_t with the value 1.
mp_integer<1> n{1}; // Initialize an mp_integer with the value 1.
mpz_add(m,m,n.get_mpz_view()); // Compute the result of n + m and store it in m,
// using the GMP API.

See the documentation of mp_integer::get_mpz_view() for more details about the mpz_view class.

Hashing

This class provides a hash() function to compute a hash value for an integer. A specialisation of the standard std::hash functor is also provided, so that it is possible to use mp_integer in standard unordered associative containers out of the box.

Definition at line 869 of file mp++.hpp.

Constructor & Destructor Documentation

◆ mp_integer() [1/7]

template<std::size_t SSize>
mppp::mp_integer< SSize >::mp_integer ( )
default

Default constructor.

The default constructor initialises an integer with static storage type and value 0.

◆ mp_integer() [2/7]

template<std::size_t SSize>
mppp::mp_integer< SSize >::mp_integer ( const mp_integer< SSize > &  other)
default

Copy constructor.

The copy constructor deep-copies other into this, preserving the original storage type.

Parameters
otherthe object that will be copied into this.

◆ mp_integer() [3/7]

template<std::size_t SSize>
mppp::mp_integer< SSize >::mp_integer ( mp_integer< SSize > &&  other)
default

Move constructor.

The move constructor will leave other in an unspecified but valid state. The storage type of this will be the same as other's.

Parameters
otherthe object that will be moved into this.

◆ mp_integer() [4/7]

template<std::size_t SSize>
template<typename T , generic_ctor_enabler< T > = 0>
mppp::mp_integer< SSize >::mp_integer ( x)
inlineexplicit

Generic constructor.

NOTE: this constructor is enabled only if T is one of the interoperable types.

This constructor will initialize an integer with the value of x. The initialization is always successful if T is an integral type (construction from bool yields 1 for true, 0 for false). If T is a floating-point type, the construction will fail if x is not finite. Construction from a floating-point type yields the truncated counterpart of the input value.

Parameters
xvalue that will be used to initialize this.
Exceptions
std::domain_errorif x is a non-finite floating-point value.

Definition at line 1186 of file mp++.hpp.

◆ mp_integer() [5/7]

template<std::size_t SSize>
mppp::mp_integer< SSize >::mp_integer ( const char *  s,
int  base = 10 
)
inlineexplicit

Constructor from C string.

This constructor will initialize this from the null-terminated string s, which must represent an integer value in base base. The expected format is the same as specified by the mpz_set_str() GMP function. base may vary from 2 to 62, or be zero. In the latter case, the base is inferred from the leading characters of the string.

Parameters
sthe input string.
basethe base used in the string representation.
Exceptions
std::invalid_argumentif the base parameter is invalid or if s is not a valid string representation of an integer in the specified base.
See also
https://gmplib.org/manual/Assigning-Integers.html

Definition at line 1205 of file mp++.hpp.

◆ mp_integer() [6/7]

template<std::size_t SSize>
mppp::mp_integer< SSize >::mp_integer ( const std::string &  s,
int  base = 10 
)
inlineexplicit

Constructor from C++ string (equivalent to the constructor from C string).

Parameters
sthe input string.
basethe base used in the string representation.
Exceptions
unspecifiedany exception thrown by the constructor from C string.

Definition at line 1231 of file mp++.hpp.

◆ mp_integer() [7/7]

template<std::size_t SSize>
mppp::mp_integer< SSize >::mp_integer ( const ::mpz_t  n)
inlineexplicit

Constructor from mpz_t.

This constructor will initialize this with the value of the GMP integer n. The storage type of this will be static if n fits in the static storage, otherwise it will be dynamic.

NOTE: it is up to the user to ensure that n has been correctly initialized. Calling this constructor with an uninitialized n is undefined behaviour.

Parameters
nthe input GMP integer.

Definition at line 1244 of file mp++.hpp.

Member Function Documentation

◆ _get_union() [1/2]

template<std::size_t SSize>
mppp_impl::integer_union<SSize>& mppp::mp_integer< SSize >::_get_union ( )
inline

Return a reference to the internal union.

This method returns a reference to the union used internally to implement the mp_integer class.

Returns
a reference to the internal union member.

Definition at line 5070 of file mp++.hpp.

◆ _get_union() [2/2]

template<std::size_t SSize>
const mppp_impl::integer_union<SSize>& mppp::mp_integer< SSize >::_get_union ( ) const
inline

Return a const reference to the internal union.

This method returns a const reference to the union used internally to implement the mp_integer class.

Returns
a const reference to the internal union member.

Definition at line 5080 of file mp++.hpp.

◆ abs()

template<std::size_t SSize>
mp_integer& mppp::mp_integer< SSize >::abs ( )
inline

In-place absolute value.

This method will set this to its absolute value.

Returns
reference to this.

Definition at line 4420 of file mp++.hpp.

◆ demote()

template<std::size_t SSize>
bool mppp::mp_integer< SSize >::demote ( )
inline

Demote to static storage.

This method will demote the storage type of this from dynamic to static.

Returns
false if the storage type of this is already static and no demotion takes place, or if the current value of this does not fit in static storage, true otherwise.

Definition at line 1581 of file mp++.hpp.

◆ even_p()

template<std::size_t SSize>
bool mppp::mp_integer< SSize >::even_p ( ) const
inline

Test if value is even.

Returns
true if this is even, false otherwise.

Definition at line 4684 of file mp++.hpp.

◆ get_mpz_t()

template<std::size_t SSize>
std::remove_extent<::mpz_t>::type* mppp::mp_integer< SSize >::get_mpz_t ( )
inline

Get a pointer to the dynamic storage.

This method will first promote this to dynamic storage (if this is not already employing dynamic storage), and it will then return a pointer to the internal mpz_t structure. The returned pointer can be used as an argument for the functions of the GMP API.

NOTE: the returned pointer is tied to the lifetime of this. Calling demote() or assigning an mp_integer with static storage to this will invalidate the returned pointer.

Returns
a pointer to the internal mpz_t structure.

Definition at line 5095 of file mp++.hpp.

◆ get_mpz_view()

template<std::size_t SSize>
mpz_view mppp::mp_integer< SSize >::get_mpz_view ( ) const
inline

Get an mpz_t view.

This method will return an object of an unspecified type mpz_view which is implicitly convertible to a const pointer to an mpz_t struct (and which can thus be used as a const mpz_t parameter in GMP functions). In addition to the implicit conversion operator, the const mpz_t object can also be retrieved via the get() method of the mpz_view class. The view provides a read-only GMP-compatible representation of the integer stored in this.

NOTE: it is important to keep in mind the following facts about the returned mpz_view object:

  • mpz_view objects are strictly read-only: it is impossible to alter this through an mpz_view, and mpz_view objects can be used in the GMP API only where a const mpz_t parameter is expected;
  • mpz_view objects can only be move-constructed (the other constructors and the assignment operators are disabled);
  • the returned object and the pointer returned by its get() method might reference internal data belonging to this, and they can thus be used safely only during the lifetime of this;
  • the lifetime of the pointer returned by the get() method is tied to the lifetime of the mpz_view object (that is, if the mpz_view object is destroyed, any pointer previously returned by get() becomes invalid);
  • any modification to this will also invalidate the view and the pointer.
Returns
an mpz view of this.

Definition at line 1652 of file mp++.hpp.

◆ is_dynamic()

template<std::size_t SSize>
bool mppp::mp_integer< SSize >::is_dynamic ( ) const
inline

Check for dynamic storage.

Returns
true if the storage type is dynamic, false otherwise.

Definition at line 1300 of file mp++.hpp.

◆ is_negative_one()

template<std::size_t SSize>
bool mppp::mp_integer< SSize >::is_negative_one ( ) const
inline

Test if the value is equal to minus one.

Returns
true if the value represented by this is -1, false otherwise.

Definition at line 5155 of file mp++.hpp.

◆ is_one()

template<std::size_t SSize>
bool mppp::mp_integer< SSize >::is_one ( ) const
inline

Test if the value is equal to one.

Returns
true if the value represented by this is 1, false otherwise.

Definition at line 5137 of file mp++.hpp.

◆ is_static()

template<std::size_t SSize>
bool mppp::mp_integer< SSize >::is_static ( ) const
inline

Test for static storage.

Returns
true if the storage type is static, false otherwise.

Definition at line 1292 of file mp++.hpp.

◆ is_zero()

template<std::size_t SSize>
bool mppp::mp_integer< SSize >::is_zero ( ) const
inline

Test if the value is zero.

Returns
true if the value represented by this is zero, false otherwise.

Definition at line 5104 of file mp++.hpp.

◆ nbits()

template<std::size_t SSize>
std::size_t mppp::mp_integer< SSize >::nbits ( ) const
inline

Size in bits.

Returns
the number of bits needed to represent this. If this is zero, zero will be returned.

Definition at line 1592 of file mp++.hpp.

◆ neg()

template<std::size_t SSize>
mp_integer& mppp::mp_integer< SSize >::neg ( )
inline

Negate in-place.

This method will set this to -this.

Returns
a reference to this.

Definition at line 1662 of file mp++.hpp.

◆ nextprime()

template<std::size_t SSize>
mp_integer& mppp::mp_integer< SSize >::nextprime ( )
inline

Compute next prime number (in-place version).

This method will set this to the first prime number greater than the current value.

Returns
a reference to this.

Definition at line 4531 of file mp++.hpp.

◆ odd_p()

template<std::size_t SSize>
bool mppp::mp_integer< SSize >::odd_p ( ) const
inline

Test if value is odd.

Returns
true if this is odd, false otherwise.

Definition at line 4662 of file mp++.hpp.

◆ operator T()

template<std::size_t SSize>
template<typename T , generic_conversion_enabler< T > = 0>
mppp::mp_integer< SSize >::operator T ( ) const
inlineexplicit

Generic conversion operator.

NOTE: this operator is enabled only if T is an interoperable type.

This operator will convert this to the type T. Conversion to bool yields false if this is zero, true otherwise. Conversion to other integral types yields the exact result, if representable by the target type T. Conversion to floating-point types might yield inexact values and infinities.

Returns
this converted to T.
Exceptions
std::overflow_errorif T is an integral type and the value of this cannot be represented by T.

Definition at line 1550 of file mp++.hpp.

◆ operator%=()

template<std::size_t SSize>
template<typename T , in_place_mod_enabler< T > = 0>
mp_integer& mppp::mp_integer< SSize >::operator%= ( const T &  d)
inline

In-place modulo operator.

Parameters
dthe divisor.
Returns
a reference to this..
Exceptions
zero_division_errorif d is zero.

Definition at line 3510 of file mp++.hpp.

◆ operator*=()

template<std::size_t SSize>
template<typename T , in_place_enabler< T > = 0>
mp_integer& mppp::mp_integer< SSize >::operator*= ( const T &  op)
inline

In-place multiplication operator.

Parameters
opthe multiplicand.
Returns
a reference to this.
Exceptions
unspecifiedany exception thrown by the assignment of a floating-point value to mp_integer, iff T is a floating-point type.

Definition at line 2814 of file mp++.hpp.

◆ operator+()

template<std::size_t SSize>
mp_integer mppp::mp_integer< SSize >::operator+ ( ) const
inline

Identity operator.

Returns
a copy of this.

Definition at line 2111 of file mp++.hpp.

◆ operator++() [1/2]

template<std::size_t SSize>
mp_integer& mppp::mp_integer< SSize >::operator++ ( )
inline

Prefix increment.

Increment this by one.

Returns
reference to this after the increment.

Definition at line 2190 of file mp++.hpp.

◆ operator++() [2/2]

template<std::size_t SSize>
mp_integer mppp::mp_integer< SSize >::operator++ ( int  )
inline

Suffix increment.

Increment this by one and return a copy of this as it was before the increment.

Returns
a copy of this before the increment.

Definition at line 2201 of file mp++.hpp.

◆ operator+=()

template<std::size_t SSize>
template<typename T , in_place_enabler< T > = 0>
mp_integer& mppp::mp_integer< SSize >::operator+= ( const T &  op)
inline

In-place addition operator.

Parameters
opthe addend.
Returns
a reference to this.
Exceptions
unspecifiedany exception thrown by the assignment of a floating-point value to mp_integer, iff T is a floating-point type.

Definition at line 2137 of file mp++.hpp.

◆ operator-()

template<std::size_t SSize>
mp_integer mppp::mp_integer< SSize >::operator- ( ) const
inline

Negated copy.

Returns
a negated copy of this.

Definition at line 2233 of file mp++.hpp.

◆ operator--() [1/2]

template<std::size_t SSize>
mp_integer& mppp::mp_integer< SSize >::operator-- ( )
inline

Prefix decrement.

Decrement this by one.

Returns
reference to this after the decrement.

Definition at line 2300 of file mp++.hpp.

◆ operator--() [2/2]

template<std::size_t SSize>
mp_integer mppp::mp_integer< SSize >::operator-- ( int  )
inline

Suffix decrement.

Decrement this by one and return a copy of this as it was before the decrement.

Returns
a copy of this before the decrement.

Definition at line 2314 of file mp++.hpp.

◆ operator-=()

template<std::size_t SSize>
template<typename T , in_place_enabler< T > = 0>
mp_integer& mppp::mp_integer< SSize >::operator-= ( const T &  op)
inline

In-place subtraction operator.

Parameters
opthe subtrahend.
Returns
a reference to this.
Exceptions
unspecifiedany exception thrown by the assignment of a floating-point value to mp_integer, iff T is a floating-point type.

Definition at line 2261 of file mp++.hpp.

◆ operator/=()

template<std::size_t SSize>
template<typename T , in_place_enabler< T > = 0>
mp_integer& mppp::mp_integer< SSize >::operator/= ( const T &  d)
inline

In-place division operator.

Parameters
dthe divisor.
Returns
a reference to this. The result is truncated.
Exceptions
zero_division_errorif d is zero and only integral types are involved in the division.
unspecifiedany exception thrown by the assignment of a floating-point value to mp_integer, iff T is a floating-point type.

Definition at line 3453 of file mp++.hpp.

◆ operator<<=()

template<std::size_t SSize>
template<typename T , shift_op_enabler< T > = 0>
mp_integer& mppp::mp_integer< SSize >::operator<<= ( s)
inline

In-place left shift operator.

Parameters
sthe bit shift value.
Returns
a reference to this.
Exceptions
std::domain_errorif s is negative or larger than an implementation-defined value.

Definition at line 3761 of file mp++.hpp.

◆ operator=() [1/3]

template<std::size_t SSize>
mp_integer& mppp::mp_integer< SSize >::operator= ( const mp_integer< SSize > &  other)
default

Copy assignment operator.

This operator will perform a deep copy of other, preserving its storage type as well.

Parameters
otherthe assignment argument.
Returns
a reference to this.

◆ operator=() [2/3]

template<std::size_t SSize>
mp_integer& mppp::mp_integer< SSize >::operator= ( mp_integer< SSize > &&  other)
default

Move assignment operator.

After the move, other will be in an unspecified but valid state, and the storage type of this will be other's original storage type.

Parameters
otherthe assignment argument.
Returns
a reference to this.

◆ operator=() [3/3]

template<std::size_t SSize>
template<typename T , generic_assignment_enabler< T > = 0>
mp_integer& mppp::mp_integer< SSize >::operator= ( const T &  x)
inline

Generic assignment operator.

NOTE: this assignment operator is enabled only if T is an interoperable type.

The body of this operator is equivalent to:

return *this = mp_integer{x};

That is, a temporary integer is constructed from x and it is then move-assigned to this.

Parameters
xthe assignment argument.
Returns
a reference to this.
Exceptions
unspecifiedany exception thrown by the generic constructor.

Definition at line 1284 of file mp++.hpp.

◆ operator>>=()

template<std::size_t SSize>
template<typename T , shift_op_enabler< T > = 0>
mp_integer& mppp::mp_integer< SSize >::operator>>= ( s)
inline

In-place right shift operator.

Parameters
sthe bit shift value.
Returns
a reference to this.
Exceptions
std::domain_errorif s is negative or larger than an implementation-defined value.

Definition at line 3952 of file mp++.hpp.

◆ probab_prime_p()

template<std::size_t SSize>
int mppp::mp_integer< SSize >::probab_prime_p ( int  reps = 25) const
inline

Test primality.

This method will run a series of probabilistic tests to determine if this is a prime number. It will return 2 if this is definitely a prime, 1 if this is probably a prime and 0 if this is definitely not-prime.

Parameters
repsthe number of tests to run.
Returns
an integer indicating if this is a prime.
Exceptions
std::invalid_argumentif reps is less than 1 or if this is negative.

Definition at line 4548 of file mp++.hpp.

◆ promote()

template<std::size_t SSize>
bool mppp::mp_integer< SSize >::promote ( )
inline

Promote to dynamic storage.

This method will promote the storage type of this from static to dynamic.

Returns
false if the storage type of this is already dynamic and no promotion takes place, true otherwise.

Definition at line 1566 of file mp++.hpp.

◆ sgn()

template<std::size_t SSize>
int mppp::mp_integer< SSize >::sgn ( ) const
inline

Sign.

Returns
0 if this is zero, 1 if this is positive, -1 if this is negative.

Definition at line 1611 of file mp++.hpp.

◆ size()

template<std::size_t SSize>
std::size_t mppp::mp_integer< SSize >::size ( ) const
inline

Size in limbs.

Returns
the number of limbs needed to represent this. If this is zero, zero will be returned.

Definition at line 1600 of file mp++.hpp.

◆ sqrt()

template<std::size_t SSize>
mp_integer& mppp::mp_integer< SSize >::sqrt ( )
inline

Integer square root (in-place version).

This method will set this to its integer square root.

Returns
a reference to this.
Exceptions
std::domain_errorif this is negative.

Definition at line 4626 of file mp++.hpp.

◆ to_string()

template<std::size_t SSize>
std::string mppp::mp_integer< SSize >::to_string ( int  base = 10) const
inline

Conversion to string.

This method will convert this into a string in base base using the GMP function mpz_get_str().

Parameters
basethe desired base.
Returns
a string representation of this.
Exceptions
std::invalid_argumentif base is smaller than 2 or greater than 62.
See also
https://gmplib.org/manual/Converting-Integers.html

Definition at line 1350 of file mp++.hpp.

Friends And Related Function Documentation

◆ abs [1/2]

template<std::size_t SSize>
void abs ( mp_integer< SSize > &  rop,
const mp_integer< SSize > &  n 
)
friend

Binary absolute value.

This function will set rop to the absolute value of n.

Parameters
ropthe return value.
nthe argument.

Definition at line 4438 of file mp++.hpp.

◆ abs [2/2]

template<std::size_t SSize>
mp_integer abs ( const mp_integer< SSize > &  n)
friend

Unary absolute value.

Parameters
nthe argument.
Returns
the absolute value of n.

Definition at line 4449 of file mp++.hpp.

◆ add

template<std::size_t SSize>
void add ( mp_integer< SSize > &  rop,
const mp_integer< SSize > &  op1,
const mp_integer< SSize > &  op2 
)
friend

Ternary add.

This function will set rop to op1 + op2.

Parameters
ropthe return value.
op1the first argument.
op2the second argument.

Definition at line 2093 of file mp++.hpp.

◆ add_ui

template<std::size_t SSize>
void add_ui ( mp_integer< SSize > &  rop,
const mp_integer< SSize > &  op1,
unsigned long  op2 
)
friend

Ternary add with unsigned long.

This function will set rop to op1 + op2.

Parameters
ropthe return value.
op1the first argument.
op2the second argument.

Definition at line 2492 of file mp++.hpp.

◆ addmul

template<std::size_t SSize>
void addmul ( mp_integer< SSize > &  rop,
const mp_integer< SSize > &  op1,
const mp_integer< SSize > &  op2 
)
friend

Ternary multiply–accumulate.

This function will set rop to rop + op1 * op2.

Parameters
ropthe return value.
op1the first argument.
op2the second argument.

Definition at line 3066 of file mp++.hpp.

◆ bin_ui [1/2]

template<std::size_t SSize>
void bin_ui ( mp_integer< SSize > &  rop,
const mp_integer< SSize > &  n,
unsigned long  k 
)
friend

Binomial coefficient (ternary version).

This function will set rop to the binomial coefficient of n and k. Negative values of n are supported.

Parameters
ropthe return value.
nthe top argument.
kthe bottom argument.

Definition at line 4735 of file mp++.hpp.

◆ bin_ui [2/2]

template<std::size_t SSize>
mp_integer bin_ui ( const mp_integer< SSize > &  n,
unsigned long  k 
)
friend

Binomial coefficient (binary version).

Parameters
nthe top argument.
kthe bottom argument.
Returns
the binomial coefficient of n and k.

Definition at line 4752 of file mp++.hpp.

◆ binomial

template<std::size_t SSize>
template<typename T , typename U , binomial_enabler< T, U > = 0>
mp_integer binomial ( const T &  n,
const U &  k 
)
friend

Generic binomial coefficient.

NOTE: this function is enabled only in the following cases:

This function will compute the binomial coefficient \( {{n}\choose{k}} \), supporting integral input values. The implementation can handle positive and negative values for both the top and the bottom argument. Internally, the mp_integer::bin_ui() function will be employed.

See http://arxiv.org/abs/1105.3689/.

Parameters
nthe top argument.
kthe bottom argument.
Returns
\( {{n}\choose{k}} \).
Exceptions
std::overflow_errorif k is greater than an implementation-defined value.
Note
This function is enabled only if binomial_impl<T,U>{}(x,y) is a valid expression, returning a type which satisfies piranha::is_returnable.

Will return the generalised binomial coefficient:

\[ {x \choose y}. \]

The actual implementation of this function is in the piranha::math::binomial_impl functor. The body of this function is equivalent to:

return binomial_impl<T,U>{}(x,y);
Parameters
xtop number.
ybottom number.
Returns
x choose y.
Exceptions
unspecifiedany exception thrown by the call operator of piranha::math::binomial_impl.

Definition at line 4833 of file mp++.hpp.

◆ cmp

template<std::size_t SSize>
int cmp ( const mp_integer< SSize > &  op1,
const mp_integer< SSize > &  op2 
)
friend

Comparison function for mp_integer.

Parameters
op1first argument.
op2second argument.
Returns
0 if op1 == op2, a negative value if op1 < op2, a positive value if op1 > op2.

Definition at line 4140 of file mp++.hpp.

◆ divexact [1/2]

template<std::size_t SSize>
void divexact ( mp_integer< SSize > &  rop,
const mp_integer< SSize > &  n,
const mp_integer< SSize > &  d 
)
friend

Exact division (ternary version).

This function will set rop to the quotient of n and d.

NOTE: if d does not divide n exactly, the behaviour will be undefined.

Parameters
ropthe return value.
nthe dividend.
dthe divisor.

Definition at line 4949 of file mp++.hpp.

◆ divexact [2/2]

template<std::size_t SSize>
mp_integer divexact ( const mp_integer< SSize > &  n,
const mp_integer< SSize > &  d 
)
friend

Exact division (binary version).

NOTE: if d does not divide n exactly, the behaviour will be undefined.

Parameters
nthe dividend.
dthe divisor.
Returns
the quotient of n and d.

Definition at line 4971 of file mp++.hpp.

◆ even_p

template<std::size_t SSize>
bool even_p ( const mp_integer< SSize > &  n)
friend

Test if integer is even.

Parameters
nthe argument.
Returns
true if n is even, false otherwise.

Definition at line 4694 of file mp++.hpp.

◆ fac_ui

template<std::size_t SSize>
void fac_ui ( mp_integer< SSize > &  rop,
unsigned long  n 
)
friend

Factorial.

This function will set rop to the factorial of n.

Parameters
ropthe return value.
nthe argument for the factorial.
Exceptions
std::invalid_argumentif n is larger than an implementation-defined limit.

Definition at line 4707 of file mp++.hpp.

◆ gcd [1/2]

template<std::size_t SSize>
void gcd ( mp_integer< SSize > &  rop,
const mp_integer< SSize > &  op1,
const mp_integer< SSize > &  op2 
)
friend

GCD (ternary version).

This function will set rop to the GCD of op1 and op2. The result is always positive. If both operands are zero, zero is returned.

Parameters
ropthe return value.
op1the first operand.
op2the second operand.

Definition at line 5038 of file mp++.hpp.

◆ gcd [2/2]

template<std::size_t SSize>
mp_integer gcd ( const mp_integer< SSize > &  op1,
const mp_integer< SSize > &  op2 
)
friend

GCD (binary version).

Parameters
op1the first operand.
op2the second operand.
Returns
the GCD of op1 and op2.

Definition at line 5058 of file mp++.hpp.

◆ hash

template<std::size_t SSize>
std::size_t hash ( const mp_integer< SSize > &  n)
friend

Hash value.

This function will return a hash value for n. The hash value depends only on the value of n.

Parameters
nmp_integer whose hash value will be computed.
Returns
a hash value for n.

Definition at line 4463 of file mp++.hpp.

◆ is_negative_one

template<std::size_t SSize>
bool is_negative_one ( const mp_integer< SSize > &  n)
friend

Test if an mppp::mp_integer is equal to minus one.

Parameters
nthe mppp::mp_integer to be tested.
Returns
true if n is equal to -1, false otherwise.

Definition at line 5165 of file mp++.hpp.

◆ is_one

template<std::size_t SSize>
bool is_one ( const mp_integer< SSize > &  n)
friend

Test if an mppp::mp_integer is equal to one.

Parameters
nthe mppp::mp_integer to be tested.
Returns
true if n is equal to 1, false otherwise.

Definition at line 5147 of file mp++.hpp.

◆ is_zero

template<std::size_t SSize>
bool is_zero ( const mp_integer< SSize > &  n)
friend

Test if an mppp::mp_integer is zero.

Parameters
nthe mppp::mp_integer to be tested.
Returns
true if n is zero, false otherwise.

Definition at line 5114 of file mp++.hpp.

◆ mul

template<std::size_t SSize>
void mul ( mp_integer< SSize > &  rop,
const mp_integer< SSize > &  op1,
const mp_integer< SSize > &  op2 
)
friend

Ternary multiplication.

This function will set rop to op1 * op2.

Parameters
ropthe return value.
op1the first argument.
op2the second argument.

Definition at line 2772 of file mp++.hpp.

◆ mul_2exp

template<std::size_t SSize>
void mul_2exp ( mp_integer< SSize > &  rop,
const mp_integer< SSize > &  n,
::mp_bitcnt_t  s 
)
friend

Ternary left shift.

This function will set rop to n multiplied by 2**s.

Parameters
ropthe return value.
nthe multiplicand.
sthe bit shift value.

Definition at line 3712 of file mp++.hpp.

◆ neg [1/2]

template<std::size_t SSize>
void neg ( mp_integer< SSize > &  rop,
const mp_integer< SSize > &  n 
)
friend

Binary negation.

This method will set rop to -n.

Parameters
ropthe return value.
nthe integer that will be negated.

Definition at line 1678 of file mp++.hpp.

◆ neg [2/2]

template<std::size_t SSize>
mp_integer neg ( const mp_integer< SSize > &  n)
friend

Unary negation.

Parameters
nthe integer that will be negated.
Returns
-n.

Definition at line 1689 of file mp++.hpp.

◆ nextprime [1/2]

template<std::size_t SSize>
void nextprime ( mp_integer< SSize > &  rop,
const mp_integer< SSize > &  n 
)
friend

Compute next prime number (binary version).

This function will set rop to the first prime number greater than n. Note that for negative values of n this function always returns 2.

Parameters
ropthe return value.
nthe mp_integer argument.

Definition at line 4508 of file mp++.hpp.

◆ nextprime [2/2]

template<std::size_t SSize>
mp_integer nextprime ( const mp_integer< SSize > &  n)
friend

Compute next prime number (unary version).

Parameters
nthe mp_integer argument.
Returns
the first prime number greater than n.

Definition at line 4519 of file mp++.hpp.

◆ odd_p

template<std::size_t SSize>
bool odd_p ( const mp_integer< SSize > &  n)
friend

Test if integer is odd.

Parameters
nthe argument.
Returns
true if n is odd, false otherwise.

Definition at line 4676 of file mp++.hpp.

◆ operator!=

template<std::size_t SSize>
template<typename T , typename U , rel_enabler< T, U > = 0>
bool operator!= ( const T &  op1,
const U &  op2 
)
friend

Inequality operator.

Parameters
op1first argument.
op2second argument.
Returns
true if op1 != op2, false otherwise.

Definition at line 4177 of file mp++.hpp.

◆ operator%

template<std::size_t SSize>
template<typename T , typename U >
common_mod_t<T, U> operator% ( const T &  n,
const U &  d 
)
friend

Binary modulo operator.

Parameters
nthe dividend.
dthe divisor.
Returns
n % d.
Exceptions
zero_division_errorif d is zero.

Definition at line 3497 of file mp++.hpp.

◆ operator*

template<std::size_t SSize>
template<typename T , typename U >
common_t<T, U> operator* ( const T &  op1,
const U &  op2 
)
friend

Binary multiplication operator.

Parameters
op1the first argument.
op2the second argument.
Returns
op1 * op2.

Definition at line 2800 of file mp++.hpp.

◆ operator*=

template<std::size_t SSize>
template<typename T , in_place_lenabler< T > = 0>
T& operator*= ( T &  x,
const mp_integer< SSize > &  n 
)
friend

In-place multiplication for interoperable types.

NOTE: this operator is enabled only if T is an interoperable type for mppp::mp_integer.

The body of this operator is equivalent to:

return x = static_cast<T>(x * n);

That is, the result of the corresponding binary operation is cast back to T and assigned to x.

Parameters
xthe first argument.
nthe second argument.
Returns
a reference to x.
Exceptions
unspecifiedany exception thrown by the conversion operator of mppp::mp_integer.

Definition at line 2842 of file mp++.hpp.

◆ operator+

template<std::size_t SSize>
template<typename T , typename U >
common_t<T, U> operator+ ( const T &  op1,
const U &  op2 
)
friend

Binary addition operator.

Parameters
op1the first argument.
op2the second argument.
Returns
op1 + op2.

Definition at line 2123 of file mp++.hpp.

◆ operator+=

template<std::size_t SSize>
template<typename T , in_place_lenabler< T > = 0>
T& operator+= ( T &  x,
const mp_integer< SSize > &  n 
)
friend

In-place addition for interoperable types.

NOTE: this operator is enabled only if T is an interoperable type for mppp::mp_integer.

The body of this operator is equivalent to:

return x = static_cast<T>(x + n);

That is, the result of the corresponding binary operation is cast back to T and assigned to x.

Parameters
xthe first argument.
nthe second argument.
Returns
a reference to x.
Exceptions
unspecifiedany exception thrown by the conversion operator of mppp::mp_integer.

Definition at line 2176 of file mp++.hpp.

◆ operator-

template<std::size_t SSize>
template<typename T , typename U >
common_t<T, U> operator- ( const T &  op1,
const U &  op2 
)
friend

Binary subtraction operator.

Parameters
op1the first argument.
op2the second argument.
Returns
op1 - op2.

Definition at line 2247 of file mp++.hpp.

◆ operator-=

template<std::size_t SSize>
template<typename T , in_place_lenabler< T > = 0>
T& operator-= ( T &  x,
const mp_integer< SSize > &  n 
)
friend

In-place subtraction for interoperable types.

NOTE: this operator is enabled only if T is an interoperable type for mppp::mp_integer.

The body of this operator is equivalent to:

return x = static_cast<T>(x - n);

That is, the result of the corresponding binary operation is cast back to T and assigned to x.

Parameters
xthe first argument.
nthe second argument.
Returns
a reference to x.
Exceptions
unspecifiedany exception thrown by the conversion operator of mppp::mp_integer.

Definition at line 2289 of file mp++.hpp.

◆ operator/

template<std::size_t SSize>
template<typename T , typename U >
common_t<T, U> operator/ ( const T &  n,
const U &  d 
)
friend

Binary division operator.

Parameters
nthe dividend.
dthe divisor.
Returns
n / d. The result is truncated if only integral types are involved in the division.
Exceptions
zero_division_errorif d is zero and only integral types are involved in the division.

Definition at line 3438 of file mp++.hpp.

◆ operator/=

template<std::size_t SSize>
template<typename T , in_place_lenabler< T > = 0>
T& operator/= ( T &  x,
const mp_integer< SSize > &  n 
)
friend

In-place division for interoperable types.

NOTE: this operator is enabled only if T is an interoperable type for mppp::mp_integer.

The body of this operator is equivalent to:

return x = static_cast<T>(x / n);

That is, the result of the corresponding binary operation is cast back to T and assigned to x.

Parameters
xthe first argument.
nthe second argument.
Returns
a reference to x.
Exceptions
unspecifiedany exception thrown by the conversion operator of mppp::mp_integer or by mppp::mp_integer::operator/().

Definition at line 3482 of file mp++.hpp.

◆ operator<

template<std::size_t SSize>
template<typename T , typename U , rel_enabler< T, U > = 0>
bool operator< ( const T &  op1,
const U &  op2 
)
friend

Less-than operator.

Parameters
op1first argument.
op2second argument.
Returns
true if op1 < op2, false otherwise.

Definition at line 4194 of file mp++.hpp.

◆ operator<< [1/2]

template<std::size_t SSize>
std::ostream& operator<< ( std::ostream &  os,
const mp_integer< SSize > &  n 
)
friend

Output stream operator for mp_integer.

This operator will print to the stream os the mp_integer n in base 10. Internally it uses the mp_integer::to_string() method.

Parameters
osthe target stream.
nthe input integer.
Returns
a reference to os.
Exceptions
unspecifiedany exception thrown by mp_integer::to_string().

Definition at line 1316 of file mp++.hpp.

◆ operator<< [2/2]

template<std::size_t SSize>
template<typename T , shift_op_enabler< T > = 0>
mp_integer operator<< ( const mp_integer< SSize > &  n,
s 
)
friend

Left shift operator.

Parameters
nthe multiplicand.
sthe bit shift value.
Returns
n times 2**s.
Exceptions
std::domain_errorif s is negative or larger than an implementation-defined value.

Definition at line 3741 of file mp++.hpp.

◆ operator<=

template<std::size_t SSize>
template<typename T , typename U , rel_enabler< T, U > = 0>
bool operator<= ( const T &  op1,
const U &  op2 
)
friend

Less-than or equal operator.

Parameters
op1first argument.
op2second argument.
Returns
true if op1 <= op2, false otherwise.

Definition at line 4245 of file mp++.hpp.

◆ operator==

template<std::size_t SSize>
template<typename T , typename U , rel_enabler< T, U > = 0>
bool operator== ( const T &  op1,
const U &  op2 
)
friend

Equality operator.

Parameters
op1first argument.
op2second argument.
Returns
true if op1 == op2, false otherwise.

Definition at line 4160 of file mp++.hpp.

◆ operator>

template<std::size_t SSize>
template<typename T , typename U , rel_enabler< T, U > = 0>
bool operator> ( const T &  op1,
const U &  op2 
)
friend

Greater-than operator.

Parameters
op1first argument.
op2second argument.
Returns
true if op1 > op2, false otherwise.

Definition at line 4228 of file mp++.hpp.

◆ operator>=

template<std::size_t SSize>
template<typename T , typename U , rel_enabler< T, U > = 0>
bool operator>= ( const T &  op1,
const U &  op2 
)
friend

Greater-than or equal operator.

Parameters
op1first argument.
op2second argument.
Returns
true if op1 >= op2, false otherwise.

Definition at line 4211 of file mp++.hpp.

◆ operator>> [1/2]

template<std::size_t SSize>
std::istream& operator>> ( std::istream &  is,
mp_integer< SSize > &  n 
)
friend

Input stream operator for mp_integer.

Equivalent to extracting a line from the stream and then assigning it to n.

Parameters
isinput stream.
ninteger to which the contents of the stream will be assigned.
Returns
reference to is.
Exceptions
unspecifiedany exception thrown by the constructor from string of mp_integer.

Definition at line 1331 of file mp++.hpp.

◆ operator>> [2/2]

template<std::size_t SSize>
template<typename T , shift_op_enabler< T > = 0>
mp_integer operator>> ( const mp_integer< SSize > &  n,
s 
)
friend

Right shift operator.

Parameters
nthe dividend.
sthe bit shift value.
Returns
n divided by 2**s. The result will be truncated.
Exceptions
std::domain_errorif s is negative or larger than an implementation-defined value.

Definition at line 3932 of file mp++.hpp.

◆ pow

template<std::size_t SSize>
template<typename T , typename U >
common_t<T, U> pow ( const T &  base,
const U &  exp 
)
friend

Generic binary exponentiation.

NOTE: this function is enabled only if at least one argument is an mppp::mp_integer and the other argument is either an mppp::mp_integer or an interoperable type for mppp::mp_integer.

This function will raise base to the power exp, and return the result. If one of the arguments is a floating-point value, then the result will be computed via std::pow() and it will also be a floating-point value. Otherwise, the result is computed via mppp::mp_integer::pow_ui() and its type is mppp::mp_integer. In case of a negative integral exponent and integral base, the result will be zero unless the absolute value of base is 1.

Parameters
basethe base.
expthe exponent.
Returns
base**exp.
Exceptions
std::overflow_errorif base and exp are integrals and exp is non-negative and outside the range of unsigned long.
zero_division_errorif base and exp are integrals and base is zero and exp is negative.
Note
This function is enabled only if the expression pow_impl<T, U>{}(x, y) is valid, returning a type which satisfies piranha::is_returnable.

Return x to the power of y. The actual implementation of this function is in the piranha::math::pow_impl functor's call operator. The body of this function is equivalent to:

return pow_impl<T, U>{}(x, y);
Parameters
xbase.
yexponent.
Returns
x to the power of y.
Exceptions
unspecifiedany exception thrown by the call operator of the piranha::math::pow_impl functor.

Definition at line 4410 of file mp++.hpp.

◆ pow_ui [1/2]

template<std::size_t SSize>
void pow_ui ( mp_integer< SSize > &  rop,
const mp_integer< SSize > &  base,
unsigned long  exp 
)
friend

Ternary exponentiation.

This function will set rop to base**exp.

Parameters
ropthe return value.
basethe base.
expthe exponent.

Definition at line 4258 of file mp++.hpp.

◆ pow_ui [2/2]

template<std::size_t SSize>
mp_integer pow_ui ( const mp_integer< SSize > &  base,
unsigned long  exp 
)
friend

Binary exponentiation.

Parameters
basethe base.
expthe exponent.
Returns
base**exp.

Definition at line 4275 of file mp++.hpp.

◆ probab_prime_p

template<std::size_t SSize>
int probab_prime_p ( const mp_integer< SSize > &  n,
int  reps = 25 
)
friend

Test primality.

This is the free-function version of mp_integer::probab_prime_p().

Parameters
nthe mp_integer whose primality will be tested.
repsthe number of tests to run.
Returns
an integer indicating if this is a prime.
Exceptions
unspecifiedany exception thrown by mp_integer::probab_prime_p().

Definition at line 4570 of file mp++.hpp.

◆ sgn

template<std::size_t SSize>
int sgn ( const mp_integer< SSize > &  n)
friend

Sign function.

Parameters
nthe mp_integer whose sign will be computed.
Returns
0 if this is zero, 1 if this is positive, -1 if this is negative.

Definition at line 1626 of file mp++.hpp.

◆ sqrt [1/2]

template<std::size_t SSize>
void sqrt ( mp_integer< SSize > &  rop,
const mp_integer< SSize > &  n 
)
friend

Integer square root (binary version).

This method will set rop to the integer square root of n.

Parameters
ropthe return value.
nthe mp_integer whose integer square root will be computed.
Exceptions
std::domain_errorif n is negative.

Definition at line 4640 of file mp++.hpp.

◆ sqrt [2/2]

template<std::size_t SSize>
mp_integer sqrt ( const mp_integer< SSize > &  n)
friend

Integer square root (unary version).

Parameters
nthe mp_integer whose integer square root will be computed.
Returns
the integer square root of n.
Exceptions
std::domain_errorif n is negative.

Definition at line 4652 of file mp++.hpp.

◆ sub

template<std::size_t SSize>
void sub ( mp_integer< SSize > &  rop,
const mp_integer< SSize > &  op1,
const mp_integer< SSize > &  op2 
)
friend

Ternary subtraction.

This function will set rop to op1 - op2.

Parameters
ropthe return value.
op1the first argument.
op2the second argument.

Definition at line 2215 of file mp++.hpp.

◆ tdiv_q_2exp

template<std::size_t SSize>
void tdiv_q_2exp ( mp_integer< SSize > &  rop,
const mp_integer< SSize > &  n,
::mp_bitcnt_t  s 
)
friend

Ternary right shift.

This function will set rop to n divided by 2**s. rop will be the truncated result of the division.

Parameters
ropthe return value.
nthe dividend.
sthe bit shift value.

Definition at line 3906 of file mp++.hpp.

◆ tdiv_qr

template<std::size_t SSize>
void tdiv_qr ( mp_integer< SSize > &  q,
mp_integer< SSize > &  r,
const mp_integer< SSize > &  n,
const mp_integer< SSize > &  d 
)
friend

Ternary truncated division.

This function will set q to the truncated quotient n / d and r to n % d. The remainder r has the same sign as n. q and r must be two distinct objects.

Parameters
qthe quotient.
rthe remainder.
nthe dividend.
dthe divisor.
Exceptions
std::invalid_argumentif q and r are the same object.
zero_division_errorif d is zero.

Definition at line 3405 of file mp++.hpp.


The documentation for this class was generated from the following file: