piranha
0.10
|
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_integer & | operator= (const mp_integer &other)=default |
Copy assignment operator. More... | |
mp_integer & | operator= (mp_integer &&other)=default |
Move assignment operator. More... | |
template<typename T , generic_assignment_enabler< T > = 0> | |
mp_integer & | operator= (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_integer & | neg () |
Negate in-place. More... | |
mp_integer | operator+ () const |
Identity operator. More... | |
template<typename T , in_place_enabler< T > = 0> | |
mp_integer & | operator+= (const T &op) |
In-place addition operator. More... | |
mp_integer & | operator++ () |
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_integer & | operator-= (const T &op) |
In-place subtraction operator. More... | |
mp_integer & | operator-- () |
Prefix decrement. More... | |
mp_integer | operator-- (int) |
Suffix decrement. More... | |
template<typename T , in_place_enabler< T > = 0> | |
mp_integer & | operator*= (const T &op) |
In-place multiplication operator. More... | |
template<typename T , in_place_enabler< T > = 0> | |
mp_integer & | operator/= (const T &d) |
In-place division operator. More... | |
template<typename T , in_place_mod_enabler< T > = 0> | |
mp_integer & | operator%= (const T &d) |
In-place modulo operator. More... | |
template<typename T , shift_op_enabler< T > = 0> | |
mp_integer & | operator<<= (T s) |
In-place left shift operator. More... | |
template<typename T , shift_op_enabler< T > = 0> | |
mp_integer & | operator>>= (T s) |
In-place right shift operator. More... | |
mp_integer & | abs () |
In-place absolute value. More... | |
mp_integer & | nextprime () |
Compute next prime number (in-place version). More... | |
int | probab_prime_p (int reps=25) const |
Test primality. More... | |
mp_integer & | sqrt () |
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... | |
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.
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
will not work, and direct initialization and explicit casting should be used instead:
The full list of interoperable builtin types is:
bool
,char
, signed char
and unsigned char
,short
and unsigned short
,int
and unsigned
,long
and unsigned long
,long long
and unsigned long long
,float
, double
and long double
(long double
requires the MPFR library).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
that writes the result of a+b
into rop
becomes simply
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:
mpz_pow_ui()
,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:
In case of unary functions, there are often three overloads available:
For instance, here are three possible ways of computing the absolute value:
Note that at this time only a small subset of the GMP API has been wrapped by mp_integer.
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:
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).
This class provides facilities to interface with the GMP library. Specifically, mp_integer features:
mpz_t
,this
to dynamic storage and returns a pointer to the internal mpz_t
instance,mpz_view
class, an instance of which can be requested via the mp_integer::get_mpz_view() method, which allows to use mp_integer in the GMP API as a drop-in replacement for const mpz_t
function arguments.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:
See the documentation of mp_integer::get_mpz_view() for more details about the mpz_view
class.
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.
|
default |
Default constructor.
The default constructor initialises an integer with static storage type and value 0.
|
default |
Copy constructor.
The copy constructor deep-copies other
into this
, preserving the original storage type.
other | the object that will be copied into this . |
|
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.
other | the object that will be moved into this . |
|
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.
x | value that will be used to initialize this . |
std::domain_error | if x is a non-finite floating-point value. |
|
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.
s | the input string. |
base | the base used in the string representation. |
std::invalid_argument | if the base parameter is invalid or if s is not a valid string representation of an integer in the specified base. |
|
inlineexplicit |
|
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.
n | the input GMP integer. |
|
inline |
Return a reference to the internal union.
This method returns a reference to the union used internally to implement the mp_integer class.
|
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.
|
inline |
|
inline |
Demote to static storage.
This method will demote the storage type of this
from dynamic to static.
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.
|
inline |
|
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.
mpz_t
structure.
|
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);get()
method might reference internal data belonging to this
, and they can thus be used safely only during the lifetime of this
;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);this
will also invalidate the view and the pointer.mpz
view of this
.
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
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.
this
converted to T
.std::overflow_error | if T is an integral type and the value of this cannot be represented by T . |
|
inline |
In-place modulo operator.
d | the divisor. |
this
..zero_division_error | if d is zero. |
|
inline |
In-place multiplication operator.
op | the multiplicand. |
this
.unspecified | any exception thrown by the assignment of a floating-point value to mp_integer, iff T is a floating-point type. |
|
inline |
|
inline |
|
inline |
|
inline |
In-place addition operator.
op | the addend. |
this
.unspecified | any exception thrown by the assignment of a floating-point value to mp_integer, iff T is a floating-point type. |
|
inline |
|
inline |
|
inline |
|
inline |
In-place subtraction operator.
op | the subtrahend. |
this
.unspecified | any exception thrown by the assignment of a floating-point value to mp_integer, iff T is a floating-point type. |
|
inline |
In-place division operator.
d | the divisor. |
this
. The result is truncated.zero_division_error | if d is zero and only integral types are involved in the division. |
unspecified | any exception thrown by the assignment of a floating-point value to mp_integer, iff T is a floating-point type. |
|
inline |
|
default |
Copy assignment operator.
This operator will perform a deep copy of other
, preserving its storage type as well.
other | the assignment argument. |
this
.
|
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.
other | the assignment argument. |
this
.
|
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:
That is, a temporary integer is constructed from x
and it is then move-assigned to this
.
x | the assignment argument. |
this
.unspecified | any exception thrown by the generic constructor. |
|
inline |
|
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.
reps | the number of tests to run. |
this
is a prime.std::invalid_argument | if reps is less than 1 or if this is negative. |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
Conversion to string.
This method will convert this
into a string in base base
using the GMP function mpz_get_str()
.
base | the desired base. |
this
.std::invalid_argument | if base is smaller than 2 or greater than 62. |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
Generic binomial coefficient.
NOTE: this function is enabled only in the following cases:
T
and U
are both mppp::mp_integer,T
is an mppp::mp_integer and U
is an integral interoperable type for mppp::mp_integer,U
is an mppp::mp_integer and T
is an integral interoperable type for mppp::mp_integer.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/.
n | the top argument. |
k | the bottom argument. |
std::overflow_error | if k is greater than an implementation-defined value. |
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:
x | top number. |
y | bottom number. |
x
choose y
.unspecified | any exception thrown by the call operator of piranha::math::binomial_impl. |
|
friend |
Comparison function for mp_integer.
op1 | first argument. |
op2 | second argument. |
0
if op1 == op2
, a negative value if op1 < op2
, a positive value if op1 > op2
.
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
Hash value.
This function will return a hash value for n
. The hash value depends only on the value of n
.
n | mp_integer whose hash value will be computed. |
n
.
|
friend |
Test if an mppp::mp_integer is equal to minus one.
n | the mppp::mp_integer to be tested. |
true
if n
is equal to -1, false
otherwise.
|
friend |
Test if an mppp::mp_integer is equal to one.
n | the mppp::mp_integer to be tested. |
true
if n
is equal to 1, false
otherwise.
|
friend |
Test if an mppp::mp_integer is zero.
n | the mppp::mp_integer to be tested. |
true
if n
is zero, false
otherwise.
|
friend |
|
friend |
|
friend |
|
friend |
|
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.
rop | the return value. |
n | the mp_integer argument. |
|
friend |
Compute next prime number (unary version).
n | the mp_integer argument. |
n
.
|
friend |
|
friend |
|
friend |
Binary modulo operator.
n | the dividend. |
d | the divisor. |
n % d
.zero_division_error | if d is zero. |
|
friend |
|
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:
That is, the result of the corresponding binary operation is cast back to T
and assigned to x
.
x | the first argument. |
n | the second argument. |
x
.unspecified | any exception thrown by the conversion operator of mppp::mp_integer. |
|
friend |
|
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:
That is, the result of the corresponding binary operation is cast back to T
and assigned to x
.
x | the first argument. |
n | the second argument. |
x
.unspecified | any exception thrown by the conversion operator of mppp::mp_integer. |
|
friend |
|
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:
That is, the result of the corresponding binary operation is cast back to T
and assigned to x
.
x | the first argument. |
n | the second argument. |
x
.unspecified | any exception thrown by the conversion operator of mppp::mp_integer. |
|
friend |
Binary division operator.
n | the dividend. |
d | the divisor. |
n / d
. The result is truncated if only integral types are involved in the division.zero_division_error | if d is zero and only integral types are involved in the division. |
|
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:
That is, the result of the corresponding binary operation is cast back to T
and assigned to x
.
x | the first argument. |
n | the second argument. |
x
.unspecified | any exception thrown by the conversion operator of mppp::mp_integer or by mppp::mp_integer::operator/(). |
|
friend |
|
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.
os | the target stream. |
n | the input integer. |
os
.unspecified | any exception thrown by mp_integer::to_string(). |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
Input stream operator for mp_integer.
Equivalent to extracting a line from the stream and then assigning it to n
.
is | input stream. |
n | integer to which the contents of the stream will be assigned. |
is
.unspecified | any exception thrown by the constructor from string of mp_integer. |
|
friend |
|
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.
base | the base. |
exp | the exponent. |
base**exp
.std::overflow_error | if base and exp are integrals and exp is non-negative and outside the range of unsigned long . |
zero_division_error | if base and exp are integrals and base is zero and exp is negative. |
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:
x | base. |
y | exponent. |
x
to the power of y
.unspecified | any exception thrown by the call operator of the piranha::math::pow_impl functor. |
|
friend |
|
friend |
|
friend |
Test primality.
This is the free-function version of mp_integer::probab_prime_p().
n | the mp_integer whose primality will be tested. |
reps | the number of tests to run. |
this
is a prime.unspecified | any exception thrown by mp_integer::probab_prime_p(). |
|
friend |
Sign function.
n | the mp_integer whose sign will be computed. |
this
is zero, 1 if this
is positive, -1 if this
is negative.
|
friend |
Integer square root (binary version).
This method will set rop
to the integer square root of n
.
rop | the return value. |
n | the mp_integer whose integer square root will be computed. |
std::domain_error | if n is negative. |
|
friend |
Integer square root (unary version).
n | the mp_integer whose integer square root will be computed. |
n
.std::domain_error | if n is negative. |
|
friend |
|
friend |
|
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.
q | the quotient. |
r | the remainder. |
n | the dividend. |
d | the divisor. |
std::invalid_argument | if q and r are the same object. |
zero_division_error | if d is zero. |