<commit_before>import sys

from gmpy_cffi.mpz import mpz, _new_mpz, _del_mpz
from gmpy_cffi.mpq import mpq
from gmpy_cffi.interface import gmp, ffi
from gmpy_cffi.convert import _mpfr_to_str, _str_to_mpfr, _pyint_to_mpfr, _pylong_to_mpz

# from gmpy_cffi.convert import _pyint_to_mpz, _pylong_to_mpz, _mpz_to_pylong, _mpz_to_str, MAX_UI

if sys.version > '3':
    long = int
    xrange = range


cache_size = _incache = 100
_cache = []


def _init_cache():
    for _ in xrange(cache_size):
        mpfr = ffi.new("mpfr_t")
        gmp.mpfr_init(mpfr)
        _cache.append(mpfr)
_init_cache()


def _new_mpfr(prec=0):
    """Return an initialized mpfr_t."""
    global _incache

    if isinstance(prec, (int, long)):
        if not (prec == 0 or gmp.MPFR_PREC_MIN <= prec <= gmp.MPFR_PREC_MAX):
            raise ValueError( "invalid prec %i (wanted %s <= prec <= %s)" % (
                    prec, gmp.MPFR_PREC_MIN, gmp.MPFR_PREC_MAX))
    else:
        raise TypeError('an integer is required')

    if _incache:
        _incache -= 1
        # Set default precision
        if prec == 0:
            gmp.mpfr_set_prec(_cache[_incache], gmp.mpfr_get_default_prec())
        else:
            gmp.mpfr_set_prec(_cache[_incache], prec)
        return _cache[_incache]
    else:
        mpfr = ffi.new("mpfr_t")
        if prec == 0:
            gmp.mpfr_init(mpfr)
        else:
            gmp.mpfr_init2(mpfr, prec)
        return mpfr


def _del_mpfr(mpfr):
    global _incache

    if _incache < cache_size:
        _cache[_incache] = mpfr
        _incache += 1
    else:
        gmp.mpfr_clear(mpfr)


def _min_prec(*args):
    """
    Calculates the minimum precision of a given args

    returns None if precision is infinite
    """
    precs = []
    for arg in args:
        if isinstance(arg, mpfr):
            precs.append(gmp.mpfr_get_prec(arg._mpfr))
        elif isinstance(arg, float):
            precs.append(53)
    if precs:
        return min(precs)
    return None


class mpfr(object):
    _mpfr_str = _repr_str = None
    """
    mpfr() -> mpfr(0.0)

         If no argument is given, return mpfr(0.0).

    mpfr(n[, precison=0]) -> mpfr

         Return an 'mpfr' object after converting a numeric value. If
         no precision, or a precision of 0, is specified; the precison
         is taken from the current context.

    mpfr(s[, precision=0[, [base=0]]) -> mpfr

         Return 'mpfr' object after converting a string 's' made up of
         digits in the given base, possibly with fraction-part (with
         period as a separator) and/or exponent-part (with exponent
         marker 'e' for base<=10, else '@'). If no precision, or a
         precision of 0, is specified; the precison is taken from the
         current context. The base of the string representation must
         be 0 or in the interval 2 ... 62. If the base is 0, the leading
         digits of the string are used to identify the base: 0b implies
         base=2, 0x implies base=16, otherwise base=10 is assumed.
    """
    def __init__(self, *args):
        if len(args) == 1 and isinstance(args[0], self.__class__):
            self._mpfr = args[0]._mpfr
            return

        if len(args) > 3:
            raise TypeError("mpfr() requires 0 to 3 arguments")

        if len(args) == 2:
            a = self._mpfr = ffi.gc(_new_mpfr(prec=args[1]), _del_mpfr)
        else:<commit_msg>fix mpfr(str, prec, base) precision bug<commit_after>import sys

from gmpy_cffi.mpz import mpz, _new_mpz, _del_mpz
from gmpy_cffi.mpq import mpq
from gmpy_cffi.interface import gmp, ffi
from gmpy_cffi.convert import _mpfr_to_str, _str_to_mpfr, _pyint_to_mpfr, _pylong_to_mpz

# from gmpy_cffi.convert import _pyint_to_mpz, _pylong_to_mpz, _mpz_to_pylong, _mpz_to_str, MAX_UI

if sys.version > '3':
    long = int
    xrange = range


cache_size = _incache = 100
_cache = []


def _init_cache():
    for _ in xrange(cache_size):
        mpfr = ffi.new("mpfr_t")
        gmp.mpfr_init(mpfr)
        _cache.append(mpfr)
_init_cache()


def _new_mpfr(prec=0):
    """Return an initialized mpfr_t."""
    global _incache

    if isinstance(prec, (int, long)):
        if not (prec == 0 or gmp.MPFR_PREC_MIN <= prec <= gmp.MPFR_PREC_MAX):
            raise ValueError( "invalid prec %i (wanted %s <= prec <= %s)" % (
                    prec, gmp.MPFR_PREC_MIN, gmp.MPFR_PREC_MAX))
    else:
        raise TypeError('an integer is required')

    if _incache:
        _incache -= 1
        # Set default precision
        if prec == 0:
            gmp.mpfr_set_prec(_cache[_incache], gmp.mpfr_get_default_prec())
        else:
            gmp.mpfr_set_prec(_cache[_incache], prec)
        return _cache[_incache]
    else:
        mpfr = ffi.new("mpfr_t")
        if prec == 0:
            gmp.mpfr_init(mpfr)
        else:
            gmp.mpfr_init2(mpfr, prec)
        return mpfr


def _del_mpfr(mpfr):
    global _incache

    if _incache < cache_size:
        _cache[_incache] = mpfr
        _incache += 1
    else:
        gmp.mpfr_clear(mpfr)


def _min_prec(*args):
    """
    Calculates the minimum precision of a given args

    returns None if precision is infinite
    """
    precs = []
    for arg in args:
        if isinstance(arg, mpfr):
            precs.append(gmp.mpfr_get_prec(arg._mpfr))
        elif isinstance(arg, float):
            precs.append(53)
    if precs:
        return min(precs)
    return None


class mpfr(object):
    _mpfr_str = _repr_str = None
    """
    mpfr() -> mpfr(0.0)

         If no argument is given, return mpfr(0.0).

    mpfr(n[, precison=0]) -> mpfr

         Return an 'mpfr' object after converting a numeric value. If
         no precision, or a precision of 0, is specified; the precison
         is taken from the current context.

    mpfr(s[, precision=0[, [base=0]]) -> mpfr

         Return 'mpfr' object after converting a string 's' made up of
         digits in the given base, possibly with fraction-part (with
         period as a separator) and/or exponent-part (with exponent
         marker 'e' for base<=10, else '@'). If no precision, or a
         precision of 0, is specified; the precison is taken from the
         current context. The base of the string representation must
         be 0 or in the interval 2 ... 62. If the base is 0, the leading
         digits of the string are used to identify the base: 0b implies
         base=2, 0x implies base=16, otherwise base=10 is assumed.
    """
    def __init__(self, *args):
        if len(args) == 1 and isinstance(args[0], self.__class__):
            self._mpfr = args[0]._mpfr
            return

        if len(args) > 3:
            raise TypeError("mpfr() requires 0 to 3 arguments")

        if len(args) >= 2:
            a = self._mpfr = ffi.gc(_new_mpfr(prec=args[1]), _del_mpfr)
        else:
{'fraction_non_alphanumeric': 0.09833187006145742, 'fraction_numerical': 0.012876792508047996, 'mean_word_length': 2.3211856171039846, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7103612', 'n_tokens_mistral': 2410, 'n_tokens_neox': 2315, 'n_words': 682}