Ejemplo n.º 1
0
def real_power(x, y):
    x = g_mpf(x)
    y = g_mpf(y)
    prec = min(x.getprec(), y.getprec())
    with workprec(prec):
        x = gmpy2mpmath(x)
        y = gmpy2mpmath(y)
        return mpmath2gmpy(x ** y)
Ejemplo n.º 2
0
def real_power(x, y):
    x = g_mpf(x)
    y = g_mpf(y)
    prec = min(x.getprec(), y.getprec())
    with workprec(prec):
        x = gmpy2mpmath(x)
        y = gmpy2mpmath(y)
        return mpmath2gmpy(x**y)
Ejemplo n.º 3
0
def gmpy2mpmath(value):
    if isinstance(value, mpcomplex):
        return value.to_mpmath()
    else:
        if get_type(value) != 'f':
            value = g_mpf(value)
        with workprec(value.getprec()):
            value = str(g_mpf(value))
            if value and value[0] == '-':
                return -mp_mpf(value[1:])
            else:
                return mp_mpf(value)
Ejemplo n.º 4
0
def mpq2mpf(x, y):
    # Kind of a bug in gmpy: when combining mpq with mpf, an mpq is returned!
    type_x = get_type(x)
    type_y = get_type(y)
    if set((type_x, type_y)) == set(('f', 'q')):
        if type_x == 'f':
            prec = x.getprec()
            y = g_mpf(y, prec)
        else:
            prec = y.getprec()
            x = g_mpf(x, prec)
    return x, y
Ejemplo n.º 5
0
def gmpy2mpmath(value):
    if isinstance(value, mpcomplex):
        return value.to_mpmath()
    else:
        if get_type(value) != 'f':
            value = g_mpf(value)
        with workprec(value.getprec()):
            value = str(g_mpf(value))
            if value and value[0] == '-':
                return -mp_mpf(value[1:])
            else:
                return mp_mpf(value)
Ejemplo n.º 6
0
def mpq2mpf(x, y):
    # Kind of a bug in gmpy: when combining mpq with mpf, an mpq is returned!
    type_x = get_type(x)
    type_y = get_type(y)
    if set((type_x, type_y)) == set(('f', 'q')):
        if type_x == 'f':
            prec = x.getprec()
            y = g_mpf(y, prec)
        else:
            prec = y.getprec()
            x = g_mpf(x, prec)
    return x, y
Ejemplo n.º 7
0
def mpmath2gmpy(value):
    if isinstance(value, mpc):
        return mpcomplex(value)
    else:
        value = str(value)
        if value in ('+inf', '-inf'):
            raise SpecialValueError('ComplexInfinity')
        return g_mpf(value.replace('+', ''), mp.prec)
Ejemplo n.º 8
0
def mpmath2gmpy(value):
    if isinstance(value, mpc):
        return mpcomplex(value)
    else:
        value = str(value)
        if value in ('+inf', '-inf'):
            raise SpecialValueError('ComplexInfinity')
        return g_mpf(value.replace('+', ''), mp.prec)
Ejemplo n.º 9
0
def unpickle_mp(value):
    type, value = value
    if type == 'z':
        return mpz(value)
    elif type == 'q':
        return mpq(value)
    elif type == 'f':
        return g_mpf(value)
    else:
        return value
Ejemplo n.º 10
0
def unpickle_mp(value):
    type, value = value
    if type == 'z':
        return mpz(value)
    elif type == 'q':
        return mpq(value)
    elif type == 'f':
        return g_mpf(value)
    else:
        return value