1Multiple-precision Reals
2========================
3
4The *mpfr* type is based on the MPFR library. The new *mpfr* type supports
5correct rounding, selectable rounding modes, and many trigonometric,
6exponential, and special functions. A *context manager* is used to control
7precision, rounding modes, and the behavior of exceptions.
8
9The default precision of an *mpfr* is 53 bits - the same precision as Python's
10*float* type. If the precision is changed, then ``mpfr(float('1.2'))`` differs
11from ``mpfr('1.2')``. To take advantage of the higher precision provided by
12the *mpfr* type, always pass constants as strings.
13
14::
15
16    >>> import gmpy2
17    >>> from gmpy2 import mpfr
18    >>> mpfr('1.2')
19    mpfr('1.2')
20    >>> mpfr(float('1.2'))
21    mpfr('1.2')
22    >>> gmpy2.get_context().precision=100
23    >>> mpfr('1.2')
24    mpfr('1.2000000000000000000000000000006',100)
25    >>> mpfr(float('1.2'))
26    mpfr('1.1999999999999999555910790149937',100)
27    >>>
28
29Contexts
30--------
31
32A *context* is used to control the behavior of *mpfr* and *mpc* arithmetic.
33In addition to controlling the precision, the rounding mode can be specified,
34minimum and maximum exponent values can be changed, various exceptions can be
35raised or ignored, gradual underflow can be enabled, and returning complex
36results can be enabled.
37
38``gmpy2.context()`` creates a new context with all options set to default.
39``gmpy2.set_context(ctx)`` will set the active context to *ctx*.
40``gmpy2.get_context()`` will return a reference to the active context. Note
41that contexts are mutable: modifying the reference returned by get_context()
42will modify the active context until a new context is enabled with
43set_context(). The ``copy()`` method of a context will return a copy of the
44context.
45
46The following example just modifies the precision. The remaining options will
47be discussed later.
48
49::
50
51    >>> gmpy2.set_context(gmpy2.context())
52    >>> gmpy2.get_context()
53    context(precision=53, real_prec=Default, imag_prec=Default,
54            round=RoundToNearest, real_round=Default, imag_round=Default,
55            emax=1073741823, emin=-1073741823,
56            subnormalize=False,
57            trap_underflow=False, underflow=False,
58            trap_overflow=False, overflow=False,
59            trap_inexact=False, inexact=False,
60            trap_invalid=False, invalid=False,
61            trap_erange=False, erange=False,
62            trap_divzero=False, divzero=False,
63            trap_expbound=False,
64            allow_complex=False,
65            allow_release_gil=False)
66    >>> gmpy2.sqrt(5)
67    mpfr('2.2360679774997898')
68    >>> gmpy2.get_context().precision=100
69    >>> gmpy2.sqrt(5)
70    mpfr('2.2360679774997896964091736687316',100)
71    >>> gmpy2.get_context().precision+=20
72    >>> gmpy2.sqrt(5)
73    mpfr('2.2360679774997896964091736687312762351',120)
74    >>> ctx=gmpy2.get_context()
75    >>> ctx.precision+=20
76    >>> gmpy2.sqrt(5)
77    mpfr('2.2360679774997896964091736687312762354406182',140)
78    >>> gmpy2.set_context(gmpy2.context())
79    >>> gmpy2.sqrt(5)
80    mpfr('2.2360679774997898')
81    >>> ctx.precision+=20
82    >>> gmpy2.sqrt(5)
83    mpfr('2.2360679774997898')
84    >>> gmpy2.set_context(ctx)
85    >>> gmpy2.sqrt(5)
86    mpfr('2.2360679774997896964091736687312762354406183596116',160)
87    >>>
88
89Context Attributes
90------------------
91
92**precision**
93    This attribute controls the precision of an *mpfr* result. The precision
94    is specified in bits, not decimal digits. The maximum precision that can
95    be specified is platform dependent and can be retrieved with
96    **get_max_precision()**.
97
98.. note::
99    Specifying a value for precision that is too close to the maximum precision
100    will cause the MPFR library to fail.
101
102**real_prec**
103    This attribute controls the precision of the real part of an *mpc* result.
104    If the value is ``Default``, then the value of the precision attribute is
105    used.
106
107**imag_prec**
108    This attribute controls the precision of the imaginary part of an *mpc*
109    result. If the value is ``Default``, then the value of real_prec is used.
110
111**round**
112    There are five rounding modes available to *mpfr* types:
113
114    ``RoundAwayZero``
115        The result is rounded away from 0.0.
116
117    ``RoundDown``
118        The result is rounded towards -Infinity.
119
120    ``RoundToNearest``
121        Round to the nearest value; ties are rounded to an even value.
122
123    ``RoundToZero``
124        The result is rounded towards 0.0.
125
126    ``RoundUp``
127        The result is rounded towards +Infinity.
128
129**real_round**
130    This attribute controls the rounding mode for the real part of an *mpc*
131    result. If the value is ``Default``, then the value of the round attribute
132    is used. Note: ``RoundAwayZero`` is not a valid rounding mode for *mpc*.
133
134**imag_round**
135    This attribute controls the rounding mode for the imaginary part of an
136    *mpc* result. If the value is ``Default``, then the value of the real_round
137    attribute is used. Note: ``RoundAwayZero`` is not a valid rounding mode for
138    *mpc*.
139
140**emax**
141    This attribute controls the maximum allowed exponent of an *mpfr* result.
142    The maximum exponent is platform dependent and can be retrieved with
143    **get_emax_max()**.
144
145**emin**
146    This attribute controls the minimum allowed exponent of an *mpfr* result.
147    The minimum exponent is platform dependent and can be retrieved with
148    **get_emin_min()**.
149
150.. note::
151    It is possible to change the values of emin/emax such that previous *mpfr*
152    values are no longer valid numbers but should either underflow to +/-0.0 or
153    overflow to +/-Infinity. To raise an exception if this occurs, see
154    **trap_expbound**.
155
156**subnormalize**
157    The usual IEEE-754 floating point representation supports gradual underflow
158    when the minimum exponent is reached. The MFPR library does not enable
159    gradual underflow by default but it can be enabled to precisely mimic the
160    results of IEEE-754 floating point operations.
161
162**trap_underflow**
163    If set to ``False``, a result that is smaller than the smallest possible
164    *mpfr* given the current exponent range will be replaced by +/-0.0. If set
165    to ``True``, an ``UnderflowResultError`` exception is raised.
166
167**underflow**
168    This flag is not user controllable. It is automatically set if a result
169    underflowed to +/-0.0 and trap_underflow is ``False``.
170
171**trap_overflow**
172    If set to ``False``, a result that is larger than the largest possible
173    *mpfr* given the current exponent range will be replaced by +/-Infinity. If
174    set to ``True``, an ``OverflowResultError`` exception is raised.
175
176**overflow**
177    This flag is not user controllable. It is automatically set if a result
178    overflowed to +/-Infinity and trap_overflow is ``False``.
179
180**trap_inexact**
181    This attribute controls whether or not an ``InexactResultError`` exception
182    is raised if an inexact result is returned. To check if the result is
183    greater or less than the exact result, check the **rc** attribute of the
184    *mpfr* result.
185
186**inexact**
187    This flag is not user controllable. It is automatically set if an inexact
188    result is returned.
189
190**trap_invalid**
191    This attribute controls whether or not an ``InvalidOperationError``
192    exception is raised if a numerical result is not defined. A special
193    NaN (Not-A-Number) value will be returned if an exception is not raised.
194    The ``InvalidOperationError`` is a sub-class of Python's ``ValueError``.
195
196    For example, ``gmpy2.sqrt(-2)`` will normally return *mpfr('nan')*.
197    However, if allow_complex is set to ``True``, then an *mpc* result will
198    be returned.
199
200**invalid**
201    This flag is not user controllable. It is automatically set if an invalid
202    (Not-A-Number) result is returned.
203
204**trap_erange**
205    This attribute controls whether or not a ``RangeError`` exception is raised
206    when certain operations are performed on NaN and/or Infinity values.
207    Setting trap_erange to ``True`` can be used to raise an exception if
208    comparisons are attempted with a NaN.
209
210    ::
211
212        >>> gmpy2.set_context(gmpy2.context())
213        >>> mpfr('nan') == mpfr('nan')
214        False
215        >>> gmpy2.get_context().trap_erange=True
216        >>> mpfr('nan') == mpfr('nan')
217        Traceback (most recent call last):
218          File "<stdin>", line 1, in <module>
219        gmpy2.RangeError: comparison with NaN
220        >>>
221
222**erange**
223    This flag is not user controllable. It is automatically set if an erange
224    error occurred.
225
226**trap_divzero**
227    This attribute controls whether or not a ``DivisionByZeroError`` exception
228    is raised if division by 0 occurs. The ``DivisionByZeroError`` is a
229    sub-class of Python's ``ZeroDivisionError``.
230
231**divzero**
232    This flag is not user controllable. It is automatically set if a division
233    by zero occurred and NaN result was returned.
234
235**trap_expbound**
236    This attribute controls whether or not an ``ExponentOutOfBoundsError``
237    exception is raised if exponents in an operand are outside the current
238    emin/emax limits.
239
240**allow_complex**
241    This attribute controls whether or not an *mpc* result can be returned if
242    an *mpfr* result would normally not be possible.
243
244Context Methods
245---------------
246
247**clear_flags()**
248    Clear the underflow, overflow, inexact, invalid, erange, and divzero flags.
249
250**copy()**
251    Return a copy of the context.
252
253Contexts and the with statement
254-------------------------------
255
256Contexts can also be used in conjunction with Python's ``with ...`` statement to
257temporarily change the context settings for a block of code and then restore the
258original settings when the block of code exits.
259
260``gmpy2.local_context()`` first save the current context and then creates a new
261context based on a context passed as the first argument, or the current context
262if no context is passed. The new context is modified if any optional keyword
263arguments are given. The original active context is restored when the block
264completes.
265
266In the following example, the current context is saved by ``gmpy2.local_context()``
267and then the block begins with a copy of the default context and the precision
268set to 100. When the block is finished, the original context is restored.
269
270::
271
272    >>> with gmpy2.local_context(gmpy2.context(), precision=100) as ctx:
273    ...   print(gmpy2.sqrt(2))
274    ...   ctx.precision += 100
275    ...   print(gmpy2.sqrt(2))
276    ...
277    1.4142135623730950488016887242092
278    1.4142135623730950488016887242096980785696718753769480731766796
279    >>>
280
281A context object can also be used directly to create a context manager block.
282However, instead of restoring the context to the active context when the
283``with ...`` statement is executed, the restored context is the context used
284before any keyword argument modifications.
285
286The code:
287
288::
289    with gmpy2.ieee(64) as ctx:
290
291is equivalent to:
292
293::
294    gmpy2.set_context(gmpy2.ieee(64))
295    with gmpy2.local_context() as ctx:
296
297Contexts that implement the standard *single*, *double*, and *quadruple* precision
298floating point types can be created using **ieee()**.
299
300
301mpfr Methods
302------------
303
304**as_integer_ratio()**
305    Returns a 2-tuple containing the numerator and denominator after converting
306    the *mpfr* object into the exact rational equivalent. The return 2-tuple
307    is equivalent to Python's as_integer_ratio() method of built-in float
308    objects.
309
310**as_mantissa_exp()**
311    Returns a 2-tuple containing the mantissa and exponent.
312
313**as_simple_fraction()**
314    Returns an *mpq* containing the simplest rational value that approximates
315    the *mpfr* value with an error less than 1/(2**precision).
316
317**conjugate()**
318    Returns the complex conjugate. For *mpfr* objects, returns a copy of the
319    original object.
320
321**digits()**
322    Returns a 3-tuple containing the mantissa, the exponent, and the number
323    of bits of precision. The mantissa is represented as a string in the
324    specified base with up to 'prec' digits. If 'prec' is 0, as many digits
325    that are available are returned. No more digits than available given x's
326    precision are returned. 'base' must be between 2 and 62, inclusive.
327
328**is_integer()**
329    Returns True if the *mpfr* object is an integer.
330
331mpfr Attributes
332---------------
333
334**imag**
335    Returns the imaginary component. For *mpfr* objects, returns 0.
336
337**precision**
338    Returns the precision of the *mpfr* object.
339
340**rc**
341    The result code (also known as ternary value in the MPFR documentation)
342    is 0 if the value of the *mpfr* object is exactly equal to the exact,
343    infinite precision value. If the result code is 1, then the value of the
344    *mpfr* object is greater than the exact value. If the result code is -1,
345    then the value of the *mpfr* object is less than the exact, infinite
346    precision value.
347
348**real**
349    Returns the real component. For *mpfr* objects, returns a copy of the
350    original object.
351
352mpfr Functions
353--------------
354
355**acos(...)**
356    acos(x) returns the arc-cosine of x. x is measured in radians. If
357    context.allow_complex is True, then an *mpc* result will be returned for
358    abs(x) > 1.
359
360**acosh(...)**
361    acosh(x) returns the inverse hyperbolic cosine of x.
362
363**add(...)**
364    add(x, y) returns x + y. The type of the result is based on the types of
365    the arguments.
366
367**agm(...)**
368    agm(x, y) returns the arithmetic-geometric mean of x and y.
369
370**ai(...)**
371    ai(x) returns the Airy function of x.
372
373**asin(...)**
374    asin(x) returns the arc-sine of x. x is measured in radians. If
375    context.allow_complex is True, then an *mpc* result will be returned for
376    abs(x) > 1.
377
378**asinh(...)**
379    asinh(x) return the inverse hyperbolic sine of x.
380
381**atan(...)**
382    atan(x) returns the arc-tangent of x. x is measured in radians.
383
384**atan2(...)**
385    atan2(y, x) returns the arc-tangent of (y/x).
386
387**atanh(...)**
388    atanh(x) returns the inverse hyperbolic tangent of x. If
389    context.allow_complex is True, then an *mpc* result will be returned for
390    abs(x) > 1.
391
392**cbrt(...)**
393    cbrt(x) returns the cube root of x.
394
395**ceil(...)**
396    ceil(x) returns the 'mpfr' that is the smallest integer >= x.
397
398**check_range(...)**
399    check_range(x) return a new 'mpfr' with exponent that lies within the
400    current range of emin and emax.
401
402**const_catalan(...)**
403    const_catalan([precision=0]) returns the Catalan's constant using the
404    specified precision. If no precision is specified, the default precision
405    is used.
406
407**const_euler(...)**
408    const_euler([precision=0]) returns the Euler's constant using the specified
409    precision. If no precision is specified, the default precision is used.
410
411**const_log2(...)**
412    const_log2([precision=0]) returns the log2 constant using the specified
413    precision. If no precision is specified, the default precision is used.
414
415**const_pi(...)**
416    const_pi([precision=0]) returns the constant pi using the specified
417    precision. If no precision is specified, the default precision is used.
418
419**context(...)**
420    context() returns a new context manager controlling MPFR and MPC
421    arithmetic.
422
423**cos(...)**
424    cos(x) returns the cosine of x. x is measured in radians.
425
426**cosh(...)**
427    cosh(x) returns the hyperbolic cosine of x.
428
429**cot(...)**
430    cot(x) returns the cotangent of x. x is measured in radians.
431
432**coth(...)**
433    coth(x) returns the hyperbolic cotangent of x.
434
435**csc(...)**
436    csc(x) returns the cosecant of x. x is measured in radians.
437
438**csch(...)**
439    csch(x) returns the hyperbolic cosecant of x.
440
441**degrees(...)**
442    degrees(x) converts an angle measurement x from radians to degrees.
443
444**digamma(...)**
445    digamma(x) returns the digamma of x.
446
447**div(...)**
448    div(x, y) returns x / y. The type of the result is based on the types of
449    the arguments.
450
451**div_2exp(...)**
452    div_2exp(x, n) returns an 'mpfr' or 'mpc' divided by 2**n.
453
454**eint(...)**
455    eint(x) returns the exponential integral of x.
456
457**erf(...)**
458    erf(x) returns the error function of x.
459
460**erfc(...)**
461    erfc(x) returns the complementary error function of x.
462
463**exp(...)**
464    exp(x) returns e**x.
465
466**exp10(...)**
467    exp10(x) returns 10**x.
468
469**exp2(...)**
470    exp2(x) returns 2**x.
471
472**expm1(...)**
473    expm1(x) returns e**x - 1. expm1() is more accurate than exp(x) - 1 when
474    x is small.
475
476**f2q(...)**
477    f2q(x[,err]) returns the simplest *mpq* approximating x to within relative
478    error err. Default is the precision of x. Uses Stern-Brocot tree to find
479    the simplest approximation. An *mpz* is returned if the denominator
480    is 1. If err<0, error sought is 2.0 ** err.
481
482**factorial(...)**
483    factorial(n) returns the floating-point approximation to the factorial
484    of n.
485
486    See fac(n) to get the exact integer result.
487
488**floor(...)**
489    floor(x) returns the 'mpfr' that is the smallest integer <= x.
490
491**fma(...)**
492    fma(x, y, z) returns correctly rounded result of (x * y) + z.
493
494**fmma(...)**
495    fmma(x, y, z, t) returns correctly rounded result of (x * y) + (z * t).
496    Requires MPFR 4.
497
498**fmms(...)**
499    fmms(x, y, z, t) returns correctly rounded result of (x * y) - (z * t).
500    Requires MPFR 4.
501
502**fmod(...)**
503    fmod(x, y) returns x - n*y where n is the integer quotient of x/y, rounded
504    to 0.
505
506**fms(...)**
507    fms(x, y, z) returns correctly rounded result of (x * y) - z.
508
509**frac(...)**
510    frac(x) returns the fractional part of x.
511
512**frexp(...)**
513    frexp(x) returns a tuple containing the exponent and mantissa of x.
514
515**fsum(...)**
516    fsum(iterable) returns the accurate sum of the values in the iterable.
517
518**gamma(...)**
519    gamma(x) returns the gamma of x.
520
521**get_exp(...)**
522    get_exp(mpfr) returns the exponent of an *mpfr*. Returns 0 for NaN or
523    Infinity and sets the erange flag and will raise an exception if trap_erange
524    is set.
525
526**hypot(...)**
527    hypot(y, x) returns square root of (x**2 + y**2).
528
529**ieee(...)**
530    ieee(bitwidth) returns a context with settings for 32-bit (aka single),
531    64-bit (aka double), or 128-bit (aka quadruple) precision floating
532    point types.
533
534**inf(...)**
535    inf(n) returns an *mpfr* initialized to Infinity with the same sign as n.
536    If n is not given, +Infinity is returned.
537
538**is_finite(...)**
539    is_finite(x) returns True if x is an actual number (i.e. not NaN or
540    Infinity).
541
542**is_inf(...)**
543    is_inf(x) returns True if x is Infinity or -Infinity.
544
545    .. note::
546        **is_inf()** is deprecated; please use **if_infinite()**.
547
548**is_infinite(...)**
549    is_infinite(x) returns True if x Infinity or -Infinity.
550
551**is_nan(...)**
552    is_nan(x) returns True if x is NaN (Not-A-Number).
553
554**is_number(...)**
555    is_number(x) returns True if x is an actual number (i.e. not NaN or
556    Infinity).
557
558    .. note::
559        **is_number()** is deprecated; please use **is_finite()**.
560
561**is_regular(...)**
562    is_regular(x) returns True if x is not zero, NaN, or Infinity.
563
564**is_signed(...)**
565    is_signed(x) returns True if the sign bit of x is set.
566
567**is_unordered(...)**
568    is_unordered(x,y) returns True if either x and/or y is NaN.
569
570**is_zero(...)**
571    is_zero(x) returns True if x is zero.
572
573**j0(...)**
574    j0(x) returns the Bessel function of the first kind of order 0 of x.
575
576**j1(...)**
577    j1(x) returns the Bessel function of the first kind of order 1 of x.
578
579**jn(...)**
580    jn(x,n) returns the Bessel function of the first kind of order n of x.
581
582**lgamma(...)**
583    lgamma(x) returns a tuple containing the logarithm of the absolute value of
584    gamma(x) and the sign of gamma(x)
585
586**li2(...)**
587    li2(x) returns the real part of dilogarithm of x.
588
589**lngamma(...)**
590    lngamma(x) returns the logarithm of gamma(x).
591
592**log(...)**
593    log(x) returns the natural logarithm of x.
594
595**log10(...)**
596    log10(x) returns the base-10 logarithm of x.
597
598**log1p(...)**
599    log1p(x) returns the natural logarithm of (1+x).
600
601**log2(...)**
602    log2(x) returns the base-2 logarithm of x.
603
604**max2(...)**
605    max2(x, y) returns the maximum of x and y. The result may be rounded to
606    match the current context. Use the builtin max() to get an exact copy of
607    the largest object without any rounding.
608
609**min2(...)**
610    min2(x, y) returns the minimum of x and y. The result may be rounded to
611    match the current context. Use the builtin min() to get an exact copy of
612    the smallest object without any rounding.
613
614**modf(...)**
615    modf(x) returns a tuple containing the integer and fractional portions
616    of x.
617
618**mpfr(...)**
619    mpfr() returns and *mpfr* object set to 0.0.
620
621    mpfr(n[, precision=0]) returns an *mpfr* object after converting a numeric
622    value n. If no precision, or a precision of 0, is specified; the precision
623    is taken from the current context.
624
625    mpfr(s[, precision=0[, [base=0]]) returns an *mpfr* object after converting
626    a string 's' made up of digits in the given base, possibly with fractional
627    part (with period as a separator) and/or exponent (with exponent marker
628    'e' for base<=10, else '@'). If no precision, or a precision of 0, is
629    specified; the precision is taken from the current context. The base of the
630    string representation must be 0 or in the interval 2 ... 62. If the base
631    is 0, the leading digits of the string are used to identify the base: 0b
632    implies base=2, 0x implies base=16, otherwise base=10 is assumed.
633
634**mpfr_from_old_binary(...)**
635    mpfr_from_old_binary(string) returns an *mpfr* from a GMPY 1.x binary mpf
636    format. Please use to_binary()/from_binary() to convert GMPY2 objects to or
637    from a binary format.
638
639**mpfr_grandom(...)**
640    mpfr_grandom(random_state) returns two random numbers with Gaussian
641    distribution. The parameter *random_state* must be created by random_state()
642    first.
643
644**mpfr_random(...)**
645    mpfr_random(random_state) returns a uniformly distributed number between
646    [0,1]. The parameter *random_state* must be created by random_state() first.
647
648**mul(...)**
649    mul(x, y) returns x * y. The type of the result is based on the types of
650    the arguments.
651
652**mul_2exp(...)**
653    mul_2exp(x, n) returns 'mpfr' or 'mpc' multiplied by 2**n.
654
655**nan(...)**
656    nan() returns an 'mpfr' initialized to NaN (Not-A-Number).
657
658**next_above(...)**
659    next_above(x) returns the next 'mpfr' from x toward +Infinity.
660
661**next_below(...)**
662    next_below(x) returns the next 'mpfr' from x toward -Infinity.
663
664**radians(...)**
665    radians(x) converts an angle measurement x from degrees to radians.
666
667**rec_sqrt(...)**
668    rec_sqrt(x) returns the reciprocal of the square root of x.
669
670**reldiff(...)**
671    reldiff(x, y) returns the relative difference between x and y. Result is
672    equal to abs(x-y)/x.
673
674**remainder(...)**
675    remainder(x, y) returns x - n*y where n is the integer quotient of x/y,
676    rounded to the nearest integer and ties rounded to even.
677
678**remquo(...)**
679    remquo(x, y) returns a tuple containing the remainder(x,y) and the low bits
680    of the quotient.
681
682**rint(...)**
683    rint(x) returns x rounded to the nearest integer using the current rounding
684    mode.
685
686**rint_ceil(...)**
687    rint_ceil(x) returns x rounded to the nearest integer by first rounding to
688    the next higher or equal integer and then, if needed, using the current
689    rounding mode.
690
691**rint_floor(...)**
692    rint_floor(x) returns x rounded to the nearest integer by first rounding to
693    the next lower or equal integer and then, if needed, using the current
694    rounding mode.
695
696**rint_round(...)**
697    rint_round(x) returns x rounded to the nearest integer by first rounding to
698    the nearest integer (ties away from 0) and then, if needed, using the
699    current rounding mode.
700
701**rint_trunc(...)**
702    rint_trunc(x) returns x rounded to the nearest integer by first rounding
703    towards zero and then, if needed, using the current rounding mode.
704
705**root(...)**
706    root(x, n) returns n-th root of x. The result always an *mpfr*.
707
708**round2(...)**
709    round2(x[, n]) returns x rounded to n bits. Uses default precision if n is
710    not specified. See round_away() to access the mpfr_round() function. Use
711    the builtin round() to round x to n decimal digits.
712
713**round_away(...)**
714    round_away(x) returns an *mpfr* by rounding x the nearest integer, with
715    ties rounded away from 0.
716
717**sec(...)**
718    sec(x) returns the secant of x. x is measured in radians.
719
720**sech(...)**
721    sech(x) returns the hyperbolic secant of x.
722
723**set_exp(...)**
724    set_exp(x, n) sets the exponent of a given *mpfr* to n. If n is outside the
725    range of valid exponents, set_exp() will set the erange flag and either
726    return the original value or raise an exception if trap_erange is set.
727
728**set_sign(...)**
729    set_sign(x, bool) returns a copy of x with it's sign bit set if *bool*
730    evaluates to True.
731
732**sign(...)**
733    sign(x) returns -1 if x < 0, 0 if x == 0, or +1 if x >0.
734
735**sin(...)**
736    sin(x) returns the sine of x. x is measured in radians.
737
738**sin_cos(...)**
739    sin_cos(x) returns a tuple containing the sine and cosine of x. x is
740    measured in radians.
741
742**sinh(...)**
743    sinh(x) returns the hyberbolic sine of x.
744
745**sinh_cosh(...)**
746    sinh_cosh(x) returns a tuple containing the hyperbolic sine and cosine of
747    x.
748
749**sqrt(...)**
750    sqrt(x) returns the square root of x. If x is integer, rational, or real,
751    then an *mpfr* will be returned. If x is complex, then an *mpc* will
752    be returned. If context.allow_complex is True, negative values of x
753    will return an *mpc*.
754
755**square(...)**
756    square(x) returns x * x. The type of the result is based on the types of
757    the arguments.
758
759**sub(...)**
760    sub(x, y) returns x - y. The type of the result is based on the types of
761    the arguments.
762
763**tan(...)**
764    tan(x) returns the tangent of x. x is measured in radians.
765
766**tanh(...)**
767    tanh(x) returns the hyperbolic tangent of x.
768
769**trunc(...)**
770    trunc(x) returns an 'mpfr' that is x truncated towards 0. Same as
771    x.floor() if x>=0 or x.ceil() if x<0.
772
773**y0(...)**
774    y0(x) returns the Bessel function of the second kind of order 0 of x.
775
776**y1(...)**
777    y1(x) returns the Bessel function of the second kind of order 1 of x.
778
779**yn(...)**
780    yn(x,n) returns the Bessel function of the second kind of order n of x.
781
782**zero(...)**
783    zero(n) returns an *mpfr* initialized to 0.0 with the same sign as n.
784    If n is not given, +0.0 is returned.
785
786**zeta(...)**
787    zeta(x) returns the Riemann zeta of x.
788
789mpfr Formatting
790---------------
791
792The *mpfr* type supports the __format__() special method to allow custom output
793formatting.
794
795**__format__(...)**
796    x.__format__(fmt) returns a Python string by formatting 'x' using the
797    format string 'fmt'. A valid format string consists of:
798
799    |     optional alignment code:
800    |        '<' -> left shifted in field
801    |        '>' -> right shifted in field
802    |        '^' -> centered in field
803    |     optional leading sign code
804    |        '+' -> always display leading sign
805    |        '-' -> only display minus for negative values
806    |        ' ' -> minus for negative values, space for positive values
807    |     optional width.precision
808    |     optional rounding mode:
809    |        'U' -> round toward plus infinity
810    |        'D' -> round toward minus infinity
811    |        'Y' -> round away from zero
812    |        'Z' -> round toward zero
813    |        'N' -> round to nearest
814    |     optional conversion code:
815    |        'a','A' -> hex format
816    |        'b'     -> binary format
817    |        'e','E' -> scientific format
818    |        'f','F' -> fixed point format
819    |        'g','G' -> fixed or scientific format
820
821    .. note::
822        The formatting codes must be specified in the order shown above.
823
824::
825
826    >>> import gmpy2
827    >>> from gmpy2 import mpfr
828    >>> a=mpfr("1.23456")
829    >>> "{0:15.3f}".format(a)
830    '          1.235'
831    >>> "{0:15.3Uf}".format(a)
832    '          1.235'
833    >>> "{0:15.3Df}".format(a)
834    '          1.234'
835    >>> "{0:.3Df}".format(a)
836    '1.234'
837    >>> "{0:+.3Df}".format(a)
838    '+1.234'
839
840
841
842