MPFR Functionality
==================

This file tests the mpfr() function that is used to create mpfr instances.

>>> import gmpy2
>>> from gmpy2 import mpz, mpq, mpfr
>>> import decimal
>>> import fractions
>>> from fractions import Fraction as F

Test creation using default context
-----------------------------------

>>> mpfr()
mpfr('0.0')
>>> mpfr(0)
mpfr('0.0')
>>> mpfr(1)
mpfr('1.0')
>>> mpfr(-1)
mpfr('-1.0')
>>> mpfr("123e17")
mpfr('1.23e+19')
>>> mpfr("foo")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid digits
>>> mpfr(1,100)
mpfr('1.0',100)
>>> mpfr("1",100)
mpfr('1.0',100)
>>> mpfr("1","hi")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: base for mpfr() must be an integer
>>> mpfr("-inf")
mpfr('-inf')
>>> mpfr("inf")
mpfr('inf')
>>> mpfr("nan")
mpfr('nan')
>>> mpfr(float("-inf"))
mpfr('-inf')
>>> mpfr(float("inf"))
mpfr('inf')
>>> mpfr(float("nan"))
mpfr('nan')
>>> mpfr(float("0"))
mpfr('0.0')
>>> mpfr(float("-0"))
mpfr('-0.0')
>>> mpfr("-0")
mpfr('-0.0')
>>> a = float("1.2345678901234567890")
>>> b = mpfr("1.2345678901234567890")
>>> a
1.2345678901234567
>>> b
mpfr('1.2345678901234567')
>>> a == b
True
>>> c = mpfr(b)
>>> b is c
True
>>> mpfr(F(0,1))
mpfr('0.0')
>>> mpfr(F(0,-1))
mpfr('0.0')
>>> mpfr(F(1,2))
mpfr('0.5')
>>> mpfr(-1)
mpfr('-1.0')
>>> mpfr(12345678901234567890)
mpfr('1.2345678901234567e+19')
>>> mpfr(mpz(12345678901234567890))
mpfr('1.2345678901234567e+19')
>>> mpfr(mpz(-1))
mpfr('-1.0')
>>> mpfr(2**15 - 1)
mpfr('32767.0')
>>> mpfr(2**15)
mpfr('32768.0')
>>> mpfr(2**15 + 1)
mpfr('32769.0')
>>> mpfr(2**30 - 1)
mpfr('1073741823.0')
>>> mpfr(2**30 )
mpfr('1073741824.0')
>>> mpfr(2**30 + 1)
mpfr('1073741825.0')
>>> ctx=gmpy2.get_context()
>>> ctx.clear_flags()
>>> a=mpfr("1.2")
>>> a.rc
-1
>>> ctx
context(precision=53, real_prec=Default, imag_prec=Default,
        round=RoundToNearest, real_round=Default, imag_round=Default,
        emax=1073741823, emin=-1073741823,
        subnormalize=False,
        trap_underflow=False, underflow=False,
        trap_overflow=False, overflow=False,
        trap_inexact=False, inexact=True,
        trap_invalid=False, invalid=False,
        trap_erange=False, erange=False,
        trap_divzero=False, divzero=False,
        allow_complex=False,
        rational_division=False)
>>> ctx.clear_flags()
>>> a=mpfr("1.25")
>>> a.rc
0
>>> ctx
context(precision=53, real_prec=Default, imag_prec=Default,
        round=RoundToNearest, real_round=Default, imag_round=Default,
        emax=1073741823, emin=-1073741823,
        subnormalize=False,
        trap_underflow=False, underflow=False,
        trap_overflow=False, overflow=False,
        trap_inexact=False, inexact=False,
        trap_invalid=False, invalid=False,
        trap_erange=False, erange=False,
        trap_divzero=False, divzero=False,
        allow_complex=False,
        rational_division=False)
>>> ctx.clear_flags()
>>> a=mpfr('nan')
>>> ctx
context(precision=53, real_prec=Default, imag_prec=Default,
        round=RoundToNearest, real_round=Default, imag_round=Default,
        emax=1073741823, emin=-1073741823,
        subnormalize=False,
        trap_underflow=False, underflow=False,
        trap_overflow=False, overflow=False,
        trap_inexact=False, inexact=False,
        trap_invalid=False, invalid=True,
        trap_erange=False, erange=False,
        trap_divzero=False, divzero=False,
        allow_complex=False,
        rational_division=False)
>>> ctx.clear_flags()
>>> mpfr(a)
mpfr('nan')
>>> ctx
context(precision=53, real_prec=Default, imag_prec=Default,
        round=RoundToNearest, real_round=Default, imag_round=Default,
        emax=1073741823, emin=-1073741823,
        subnormalize=False,
        trap_underflow=False, underflow=False,
        trap_overflow=False, overflow=False,
        trap_inexact=False, inexact=False,
        trap_invalid=False, invalid=False,
        trap_erange=False, erange=False,
        trap_divzero=False, divzero=False,
        allow_complex=False,
        rational_division=False)
>>> ctx.clear_flags()
>>> mpfr(float('nan'))
mpfr('nan')
>>> ctx
context(precision=53, real_prec=Default, imag_prec=Default,
        round=RoundToNearest, real_round=Default, imag_round=Default,
        emax=1073741823, emin=-1073741823,
        subnormalize=False,
        trap_underflow=False, underflow=False,
        trap_overflow=False, overflow=False,
        trap_inexact=False, inexact=False,
        trap_invalid=False, invalid=True,
        trap_erange=False, erange=False,
        trap_divzero=False, divzero=False,
        allow_complex=False,
        rational_division=False)

Create using extended precision
-------------------------------

>>> gmpy2.set_context(gmpy2.ieee(128))
>>> mpfr(1)/7
mpfr('0.14285714285714285714285714285714285',113)
>>> mpfr(1.0/7)
mpfr('0.142857142857142849212692681248881854',113)
>>> mpfr(1.0/7).digits(2)
('10010010010010010010010010010010010010010010010010010000000000000000000000000000000000000000000000000000000000000', -2, 113)
>>> gmpy2.set_context(gmpy2.ieee(32))
>>> mpfr(1)/7
mpfr('0.142857149',24)
>>> (mpfr(1)/7).digits(2)
('100100100100100100100101', -2, 24)
>>> mpfr(1.0/7)
mpfr('0.142857149',24)
>>> mpfr(1.0/7).digits(2)
('100100100100100100100101', -2, 24)
>>> mpfr(1.0/7, precision=0)
mpfr('0.142857149',24)
>>> mpfr(1.0/7, precision=1)
mpfr('0.14285714285714285')
>>> gmpy2.set_context(gmpy2.context())

