Multiple-precision Reals
mpfr Type
- class gmpy2.mpfr(n=0, /, precision=0)
- class gmpy2.mpfr(n, /, precision, context)
- class gmpy2.mpfr(s, /, precision=0, base=0)
- class gmpy2.mpfr(s, /, precision, base, context)
Return a floating-point number after converting a numeric value n or a string s made of digits in the given base.
A string can be with fraction-part (with a period as a separator) and/or exponent-part with an exponent marker ‘e’ or ‘E’ for bases up to 10, else ‘@’ in any base. In bases 2 and 16, the exponent prefix can also be ‘p’ or ‘P’, in which case the exponent indicates a multiplication by a power of 2 instead of the base. The value of an exponent is always written in base 10. The fractional-part digits are parsed the same as the
mpztype constructor does and both the whole number and exponent-part optionally can be preceded by ‘+’ or ‘-’. Every input, accepted by thefloattype constructor or thefloat.fromhexmethod is also accepted.If a precision greater than or equal to 2 is specified, then it is used. A precision of 0 (the default) implies the precision of either the specified context or the current context is used. A precision of 1 minimizes the loss of precision by following these rules:
If n is a radix-2 floating-point number, then the full precision of n is retained.
If n is an integer, then the precision is the bit length of the integer.
- __format__(fmt, /)
Return a Python string by formatting self using the format string ‘fmt’.
The format specification adopts the same general form as Python’s Format Specification Mini-Language. All of Python’s format types are supported, with the exception of ‘n’. Format types ‘a’ and ‘A’ (use uppercase digits) allow to represent floating-point number as a C99-style hexadecimal string. Format type ‘b’ allows format number in binary.
Five rounding modes are supported, as for
context.round:‘U’: rounding towards plus infinity
‘D’: rounding towards minus infinity
‘Y’: rounding away from zero
‘Z’: rounding towards zero
‘N’: rounding to nearest
The rounding option must be set right before the presentation type. If it’s not specified, the context’s rounding mode is used.
The default format is like str(self) output, as altered by the other format modifiers.
- as_integer_ratio()
Return the exact rational equivalent of an
mpfr. Value is atuplefor compatibility with Python’sfloat.as_integer_ratio.
- as_simple_fraction(precision=0)
Return a simple rational approximation to self. The result will be accurate to ‘precision’ bits. If ‘precision’ is 0, the precision of ‘self’ will be used.
- conjugate()
Return the conjugate of self (which is just a new reference to self since self is not a complex number).
- digits(base=10, prec=0, /)
Returns up to ‘prec’ digits in the given base. If ‘prec’ is 0, as many digits that are available are returned. No more digits than available given self’s precision are returned. ‘base’ must be between 2 and 62, inclusive. The result is a three element
tuplecontaining the mantissa, the exponent, and the number of bits of precision.
- is_finite()
Return
Trueif self is an actual number (i.e. non NaN or Infinity). If self is anmpc, returnTrueif both self.real and self.imag are finite.
- is_infinite()
Return
Trueif self is +Infinity or -Infinity. If self is anmpc, returnTrueif either self.real or self.imag is infinite. Otherwise returnFalse.
- is_zero()
Return
Trueif self is equal to 0. If self is anmpc, returnTrueif both self.real and self.imag are equal to 0.
- imag
imaginary component
- precision
precision in bits
- rc
return code
- real
real component
mpfr Functions
- gmpy2.agm(x, y, /)
Return arithmetic-geometric mean of x and y.
- gmpy2.ai(x, /)
Return Airy function of x.
- gmpy2.atan2(y, x, /)
Return arc-tangent of (y/x); result in radians.
- gmpy2.cbrt(x, /)
Return the cube root of x.
- gmpy2.ceil(x, /)
Return an ‘mpfr’ that is the smallest integer >= x.
- gmpy2.check_range(x, /)
Return a new
mpfrwith exponent that lies within the current range of emin and emax.
- gmpy2.cmp(x, y, /)
Return -1 if x < y; 0 if x = y; or 1 if x > y. Both x and y must be integer, rational or real. Note: 0 is returned (and exception flag set) if either argument is NaN.
- gmpy2.const_catalan(precision=0)
Return the catalan constant using the specified precision. If no precision is specified, the default precision is used.
- gmpy2.const_euler(precision=0)
Return the euler constant using the specified precision. If no precision is specified, the default precision is used.
- gmpy2.const_log2(precision=0)
Return the log2 constant using the specified precision. If no precision is specified, the default precision is used.
- gmpy2.const_pi(precision=0)
Return the constant pi using the specified precision. If no precision is specified, the default precision is used.
- gmpy2.cot(x, /)
Return cotangent of x; x in radians.
- gmpy2.coth(x, /)
Return hyperbolic cotangent of x.
- gmpy2.csc(x, /)
Return cosecant of x; x in radians.
- gmpy2.csch(x, /)
Return hyperbolic cosecant of x.
- gmpy2.degrees(x, /)
Convert angle x from radians to degrees. Note: In rare cases the result may not be correctly rounded.
- gmpy2.digamma(x, /)
Return digamma of x.
- gmpy2.eint(x, /)
Return exponential integral of x.
- gmpy2.erf(x, /)
Return error function of x.
- gmpy2.erfc(x, /)
Return complementary error function of x.
- gmpy2.exp10(x, /)
Return 10**x.
- gmpy2.exp2(x, /)
Return 2**x.
- gmpy2.expm1(x, /)
Return exp(x) - 1.
- gmpy2.factorial(n, /)
Return the floating-point approximation to the factorial of n.
See
fac()to get the exact integer result.
- gmpy2.fmma(x, y, z, t, /)
Return correctly rounded result of (x * y) + (z + t).
- gmpy2.fmms(x, y, z, t, /)
Return correctly rounded result of (x * y) - (z + t).
- gmpy2.fmod(x, y, /)
Return x - n*y where n is the integer quotient of x/y, rounded to 0.
- gmpy2.frac(x, /)
Return fractional part of x.
- gmpy2.fsum(iterable, /)
Return an accurate sum of the values in the iterable.
- gmpy2.gamma(x, /)
Return gamma of x.
- gmpy2.gamma_inc(a, x, /)
Return (upper) incomplete gamma of a and x.
- gmpy2.get_exp(x, /)
Return the exponent of x. Returns 0 for NaN or Infinity and sets the
context.erangeflag of the current context and will raise an exception ifcontext.trap_erangeis set.
- gmpy2.hypot(x, y, /)
Return square root of (x**2 + y**2).
- gmpy2.inf(n, /)
Return an
mpfrinitialized to Infinity with the same sign as n. If n is not given, +Infinity is returned.
- gmpy2.is_finite(x, /)
Return
Trueif x is an actual number (i.e. non NaN or Infinity). If x is anmpc, returnTrueif both x.real and x.imag are finite.
- gmpy2.is_infinite(x, /)
Return
Trueif x is +Infinity or -Infinity. If x is anmpc, returnTrueif either x.real or x.imag is infinite. Otherwise returnFalse.
- gmpy2.j0(x, /)
Return first kind Bessel function of order 0 of x.
- gmpy2.j1(x, /)
Return first kind Bessel function of order 1 of x.
- gmpy2.jn(n, x, /)
Return the first kind Bessel function of order n of x. Note: the order of the arguments changed in gmpy2 2.2.0a2
- gmpy2.lgamma(x, /)
Return a
tuplecontaining the logarithm of the absolute value of gamma(x) and the sign of gamma(x)
- gmpy2.li2(x, /)
Return real part of dilogarithm of x.
- gmpy2.lngamma(x, /)
Return natural logarithm of gamma(x).
- gmpy2.log1p(x, /)
Return natural logarithm of (1+x).
- gmpy2.log2(x, /)
Return base-2 logarithm of x.
- gmpy2.maxnum(x, y, /)
Return the maximum number of x and y. If x and y are not
mpfr, they are converted tompfr. The result is rounded to match the current context. If only one of x or y is a number, then that number is returned.
- gmpy2.minnum(x, y, /)
Return the minimum number of x and y. If x and y are not
mpfr, they are converted tompfr. The result is rounded to match the current context. If only one of x or y is a number, then that number is returned.
- gmpy2.mpfr_grandom(random_state, /)
Return two random numbers with gaussian distribution.
- gmpy2.mpfr_nrandom(random_state, /)
Return a random number with gaussian distribution.
- gmpy2.mpfr_random(random_state, /)
Return uniformly distributed number between [0,1].
- gmpy2.radians(x, /)
Convert angle x from degrees to radians. Note: In rare cases the result may not be correctly rounded.
- gmpy2.rec_sqrt(x, /)
Return the reciprocal of the square root of x.
- gmpy2.reldiff(x, y, /)
Return the relative difference between x and y. Result is equal to abs(x-y)/x.
- gmpy2.remainder(x, y, /)
Return x - n*y where n is the integer quotient of x/y, rounded to the nearest integer and ties rounded to even.
- gmpy2.remquo(x, y, /)
Return a
tuplecontaining the remainder(x,y) and the low bits of the quotient.
- gmpy2.rint(x, /)
Return x rounded to the nearest integer using the current rounding mode.
- gmpy2.rint_ceil(x, /)
Return x rounded to the nearest integer by first rounding to the next higher or equal integer and then, if needed, using the current rounding mode.
- gmpy2.rint_floor(x, /)
Return x rounded to the nearest integer by first rounding to the next lower or equal integer and then, if needed, using the current rounding mode.
- gmpy2.rint_round(x, /)
Return x rounded to the nearest integer by first rounding to the nearest integer (ties away from 0) and then, if needed, using the current rounding mode.
- gmpy2.rint_trunc(x, /)
Return x rounded to the nearest integer by first rounding towards zero and then, if needed, using the current rounding mode.
- gmpy2.root(x, n, /)
Return n-th root of x. The result always an
mpfr. Note: not IEEE 754-2008 compliant; result differs when x = -0 and n is even. Seerootn().
- gmpy2.rootn(x, n, /)
Return n-th root of x. The result always an
mpfr. Note: this is IEEE 754-2008 compliant version ofroot().
- gmpy2.round2(x, n=0, /)
Return x rounded to n bits. Uses default precision if n is not specified. See
round_away()to access the mpfr_round() function of the MPFR.
- gmpy2.round_away(x, /)
Return an
mpfrthat is x rounded to the nearest integer, with ties rounded away from 0.
- gmpy2.sec(x, /)
Return secant of x; x in radians.
- gmpy2.sech(x, /)
Return hyperbolic secant of x.
- gmpy2.set_exp(x, n, /)
Set the exponent of x to n. If n is outside the range of valid exponents,
set_exp()will set thecontext.erangeflag of the current context and either return the original value or raise an exception ifcontext.trap_erangeis set.
- gmpy2.sign(x, /)
Return -1 if x < 0, 0 if x == 0, or +1 if x >0.
- gmpy2.trunc($module, x, /)
Return an
mpfrthat is x truncated towards 0. Same as x.floor() if x>=0 or x.ceil() if x<0.
- gmpy2.y0(x, /)
Return second kind Bessel function of order 0 of x.
- gmpy2.y1(x, /)
Return second kind Bessel function of order 1 of x.
- gmpy2.yn(n, x, /)
Return the second kind Bessel function of order n of x. Note: the order of the arguments changed in gmpy2 2.2.0a2
- gmpy2.zero(n, /)
Return an
mpfrinitialized to 0.0 with the same sign as n. If n is not given, +0.0 is returned.
- gmpy2.zeta(x, /)
Return Riemann zeta of x.
- gmpy2.get_max_precision()
Return the maximum bits of precision that can be used for calculations. Note: to allow extra precision for intermediate calculations, avoid setting precision close the maximum precision.
- gmpy2.can_round(b, err, rnd1, rnd2, prec, /)
Let b be an approximation to an unknown number x that is rounded according to rnd1. Assume the b has an error at most two to the power of E(b)-err where E(b) is the exponent of b. Then return
Trueif x can be rounded correctly to prec bits with rounding mode rnd2.
- gmpy2.free_cache()
Free the internal cache of constants maintained by MPFR.