Example #1
0
def _impl_int_int_pow(space, iv, iw, iz):
    if iw < 0:
        if iz != 0:
            raise OperationError(
                space.w_TypeError,
                space.wrap("pow() 2nd argument "
                           "cannot be negative when 3rd argument specified"))
        ## bounce it, since it always returns float
        raise FailedToImplementArgs(space.w_ValueError,
                                    space.wrap("integer exponentiation"))
    temp = iv
    ix = 1
    try:
        while iw > 0:
            if iw & 1:
                ix = ovfcheck(ix * temp)
            iw >>= 1  #/* Shift exponent down by 1 bit */
            if iw == 0:
                break
            temp = ovfcheck(temp * temp)  #/* Square the value of temp */
            if iz:
                #/* If we did a multiplication, perform a modulo */
                ix = ix % iz
                temp = temp % iz
        if iz:
            ix = ix % iz
    except OverflowError:
        raise FailedToImplementArgs(space.w_OverflowError,
                                    space.wrap("integer exponentiation"))
    return ix
Example #2
0
def _impl_pow(space, iv, w_int2, iz=r_longlong(0)):
    iw = w_int2.intval
    if iw < 0:
        if iz != 0:
            raise OperationError(
                space.w_TypeError,
                space.wrap("pow() 2nd argument "
                           "cannot be negative when 3rd argument specified"))
        ## bounce it, since it always returns float
        raise FailedToImplementArgs(space.w_ValueError,
                                    space.wrap("integer exponentiation"))
    temp = iv
    ix = r_longlong(1)
    try:
        while iw > 0:
            if iw & 1:
                ix = llong_mul_ovf(ix, temp)
            iw >>= 1  #/* Shift exponent down by 1 bit */
            if iw == 0:
                break
            temp = llong_mul_ovf(temp, temp)  #/* Square the value of temp */
            if iz:
                #/* If we did a multiplication, perform a modulo */
                ix = ix % iz
                temp = temp % iz
        if iz:
            ix = ix % iz
    except OverflowError:
        raise FailedToImplementArgs(space.w_OverflowError,
                                    space.wrap("integer exponentiation"))
    return W_SmallLongObject(ix)
Example #3
0
def truediv__Int_Int(space, w_int1, w_int2):
    x = float(w_int1.intval)
    y = float(w_int2.intval)
    if y == 0.0:
        raise FailedToImplementArgs(space.w_ZeroDivisionError,
                                    space.wrap("float division"))
    return space.wrap(x / y)
Example #4
0
def div__Float_Float(space, w_float1, w_float2):
    x = w_float1.floatval
    y = w_float2.floatval
    if y == 0.0:
        raise FailedToImplementArgs(space.w_ZeroDivisionError,
                                    space.wrap("float division"))
    return W_FloatObject(x / y)
Example #5
0
def neg__Int(space, w_int1):
    a = w_int1.intval
    try:
        x = ovfcheck(-a)
    except OverflowError:
        raise FailedToImplementArgs(space.w_OverflowError,
                                    space.wrap("integer negation"))
    return wrapint(space, x)
Example #6
0
def sub__Int_Int(space, w_int1, w_int2):
    x = w_int1.intval
    y = w_int2.intval
    try:
        z = ovfcheck(x - y)
    except OverflowError:
        raise FailedToImplementArgs(space.w_OverflowError,
                                    space.wrap("integer substraction"))
    return wrapint(space, z)
Example #7
0
def mul__Int_Int(space, w_int1, w_int2):
    x = w_int1.intval
    y = w_int2.intval
    try:
        z = ovfcheck(x * y)
    except OverflowError:
        raise FailedToImplementArgs(space.w_OverflowError,
                                    space.wrap("integer multiplication"))
    return wrapint(space, z)
Example #8
0
def mul__SmallLong_SmallLong(space, w_small1, w_small2):
    x = w_small1.longlong
    y = w_small2.longlong
    try:
        z = llong_mul_ovf(x, y)
    except OverflowError:
        raise FailedToImplementArgs(space.w_OverflowError,
                                    space.wrap("integer multiplication"))
    return W_SmallLongObject(z)
Example #9
0
def lshift__Int_Int(space, w_int1, w_int2):
    a = w_int1.intval
    b = w_int2.intval
    if r_uint(b) < LONG_BIT:  # 0 <= b < LONG_BIT
        try:
            c = ovfcheck(a << b)
        except OverflowError:
            raise FailedToImplementArgs(space.w_OverflowError,
                                        space.wrap("integer left shift"))
        return wrapint(space, c)
    if b < 0:
        raise OperationError(space.w_ValueError,
                             space.wrap("negative shift count"))
    else:  #b >= LONG_BIT
        if a == 0:
            return get_integer(space, w_int1)
        raise FailedToImplementArgs(space.w_OverflowError,
                                    space.wrap("integer left shift"))
Example #10
0
def neg__SmallLong(space, w_small):
    a = w_small.longlong
    try:
        if a == LONGLONG_MIN:
            raise OverflowError
        x = -a
    except OverflowError:
        raise FailedToImplementArgs(space.w_OverflowError,
                                    space.wrap("integer negation"))
    return W_SmallLongObject(x)
Example #11
0
def sub__SmallLong_SmallLong(space, w_small1, w_small2):
    x = w_small1.longlong
    y = w_small2.longlong
    try:
        z = x - y
        if ((z ^ x) & (z ^ ~y)) < 0:
            raise OverflowError
    except OverflowError:
        raise FailedToImplementArgs(space.w_OverflowError,
                                    space.wrap("integer subtraction"))
    return W_SmallLongObject(z)
Example #12
0
def lshift__SmallLong_Int(space, w_small1, w_int2):
    a = w_small1.longlong
    b = w_int2.intval
    if r_uint(b) < LONGLONG_BIT:  # 0 <= b < LONGLONG_BIT
        try:
            c = a << b
            if a != (c >> b):
                raise OverflowError
        except OverflowError:
            raise FailedToImplementArgs(space.w_OverflowError,
                                        space.wrap("integer left shift"))
        return W_SmallLongObject(c)
    if b < 0:
        raise OperationError(space.w_ValueError,
                             space.wrap("negative shift count"))
    else:  #b >= LONGLONG_BIT
        if a == 0:
            return w_small1
        raise FailedToImplementArgs(space.w_OverflowError,
                                    space.wrap("integer left shift"))
Example #13
0
def test_failedtoimplement():
    f = FailedToImplement()
    assert f.get_w_type("space") is None
    assert f.get_w_value("space") is None
    f = FailedToImplementArgs("ab", "cd")
    assert f.get_w_type("space") == "ab"
    assert f.get_w_value("space") == "cd"
    # for testing it's good to get the following behavior:
    raises(AssertionError, FailedToImplement, "ab", "cd")
    # but the class FailedToImplement should have no __init__ for translation:
    assert '__init__' not in FailedToImplement.__dict__
Example #14
0
def mod__Int_Int(space, w_int1, w_int2):
    x = w_int1.intval
    y = w_int2.intval
    try:
        z = ovfcheck(x % y)
    except ZeroDivisionError:
        raise OperationError(space.w_ZeroDivisionError,
                             space.wrap("integer modulo by zero"))
    except OverflowError:
        raise FailedToImplementArgs(space.w_OverflowError,
                                    space.wrap("integer modulo"))
    return wrapint(space, z)
Example #15
0
def mod__SmallLong_SmallLong(space, w_small1, w_small2):
    x = w_small1.longlong
    y = w_small2.longlong
    try:
        if y == -1 and x == LONGLONG_MIN:
            raise OverflowError
        z = x % y
    except ZeroDivisionError:
        raise OperationError(space.w_ZeroDivisionError,
                             space.wrap("integer modulo by zero"))
    except OverflowError:
        raise FailedToImplementArgs(space.w_OverflowError,
                                    space.wrap("integer modulo"))
    return W_SmallLongObject(z)
Example #16
0
def divmod__Int_Int(space, w_int1, w_int2):
    x = w_int1.intval
    y = w_int2.intval
    try:
        z = ovfcheck(x // y)
    except ZeroDivisionError:
        raise OperationError(space.w_ZeroDivisionError,
                             space.wrap("integer divmod by zero"))
    except OverflowError:
        raise FailedToImplementArgs(space.w_OverflowError,
                                    space.wrap("integer modulo"))
    # no overflow possible
    m = x % y
    w = space.wrap
    return space.newtuple([w(z), w(m)])
Example #17
0
def mod__Float_Float(space, w_float1, w_float2):
    x = w_float1.floatval
    y = w_float2.floatval
    if y == 0.0:
        raise FailedToImplementArgs(space.w_ZeroDivisionError,
                                    space.wrap("float modulo"))
    try:
        mod = math.fmod(x, y)
    except ValueError:
        mod = rfloat.NAN
    else:
        if (mod and ((y < 0.0) != (mod < 0.0))):
            mod += y

    return W_FloatObject(mod)
Example #18
0
def divmod__SmallLong_SmallLong(space, w_small1, w_small2):
    x = w_small1.longlong
    y = w_small2.longlong
    try:
        if y == -1 and x == LONGLONG_MIN:
            raise OverflowError
        z = x // y
    except ZeroDivisionError:
        raise OperationError(space.w_ZeroDivisionError,
                             space.wrap("integer divmod by zero"))
    except OverflowError:
        raise FailedToImplementArgs(space.w_OverflowError,
                                    space.wrap("integer modulo"))
    # no overflow possible
    m = x % y
    return space.newtuple([W_SmallLongObject(z), W_SmallLongObject(m)])
Example #19
0
def _divmod_w(space, w_float1, w_float2):
    x = w_float1.floatval
    y = w_float2.floatval
    if y == 0.0:
        raise FailedToImplementArgs(space.w_ZeroDivisionError,
                                    space.wrap("float modulo"))
    try:
        mod = math.fmod(x, y)
    except ValueError:
        return [W_FloatObject(rfloat.NAN), W_FloatObject(rfloat.NAN)]
    # fmod is typically exact, so vx-mod is *mathematically* an
    # exact multiple of wx.  But this is fp arithmetic, and fp
    # vx - mod is an approximation; the result is that div may
    # not be an exact integral value after the division, although
    # it will always be very close to one.
    div = (x - mod) / y
    if (mod):
        # ensure the remainder has the same sign as the denominator
        if ((y < 0.0) != (mod < 0.0)):
            mod += y
            div -= 1.0
    else:
        # the remainder is zero, and in the presence of signed zeroes
        # fmod returns different results across platforms; ensure
        # it has the same sign as the denominator; we'd like to do
        # "mod = wx * 0.0", but that may get optimized away
        mod *= mod  # hide "mod = +0" from optimizer
        if y < 0.0:
            mod = -mod
    # snap quotient to nearest integral value
    if div:
        floordiv = math.floor(div)
        if (div - floordiv > 0.5):
            floordiv += 1.0
    else:
        # div is zero - get the same sign as the true quotient
        div *= div  # hide "div = +0" from optimizers
        floordiv = div * x / y  # zero w/ sign of vx/wx

    return [W_FloatObject(floordiv), W_FloatObject(mod)]
Example #20
0
def mod__Float_Float(space, w_float1, w_float2):
    x = w_float1.floatval
    y = w_float2.floatval
    if y == 0.0:
        raise FailedToImplementArgs(space.w_ZeroDivisionError,
                                    space.wrap("float modulo"))
    try:
        mod = math.fmod(x, y)
    except ValueError:
        mod = rfloat.NAN
    else:
        if mod:
            # ensure the remainder has the same sign as the denominator
            if (y < 0.0) != (mod < 0.0):
                mod += y
        else:
            # the remainder is zero, and in the presence of signed zeroes
            # fmod returns different results across platforms; ensure
            # it has the same sign as the denominator; we'd like to do
            # "mod = y * 0.0", but that may get optimized away
            mod = copysign(0.0, y)

    return W_FloatObject(mod)
Example #21
0
def pow__Long_Long_None(space, w_long1, w_long2, w_long3):
    # XXX need to replicate some of the logic, to get the errors right
    if w_long2.num.lt(rbigint.fromint(0)):
        raise FailedToImplementArgs(space.w_ValueError,
                                    space.wrap("long pow() too negative"))
    return W_LongObject(w_long1.num.pow(w_long2.num, None))
Example #22
0
def pow__Float_Float_ANY(space, w_float1, w_float2, thirdArg):
    # This raises FailedToImplement in cases like overflow where a
    # (purely theoretical) big-precision float implementation would have
    # a chance to give a result, and directly OperationError for errors
    # that we want to force to be reported to the user.
    if not space.is_w(thirdArg, space.w_None):
        raise OperationError(
            space.w_TypeError,
            space.wrap(
                "pow() 3rd argument not allowed unless all arguments are integers"
            ))
    x = w_float1.floatval
    y = w_float2.floatval

    # Sort out special cases here instead of relying on pow()
    if y == 2.0:  # special case for performance:
        return W_FloatObject(x * x)  # x * x is always correct
    if y == 0.0:
        # x**0 is 1, even 0**0
        return W_FloatObject(1.0)
    if isnan(x):
        # nan**y = nan, unless y == 0
        return W_FloatObject(x)
    if isnan(y):
        # x**nan = nan, unless x == 1; x**nan = x
        if x == 1.0:
            return W_FloatObject(1.0)
        else:
            return W_FloatObject(y)
    if isinf(y):
        # x**inf is: 0.0 if abs(x) < 1; 1.0 if abs(x) == 1; inf if
        # abs(x) > 1 (including case where x infinite)
        #
        # x**-inf is: inf if abs(x) < 1; 1.0 if abs(x) == 1; 0.0 if
        # abs(x) > 1 (including case where v infinite)
        x = abs(x)
        if x == 1.0:
            return W_FloatObject(1.0)
        elif (y > 0.0) == (x > 1.0):
            return W_FloatObject(INFINITY)
        else:
            return W_FloatObject(0.0)
    if isinf(x):
        # (+-inf)**w is: inf for w positive, 0 for w negative; in oth
        # cases, we need to add the appropriate sign if w is an odd
        # integer.
        y_is_odd = math.fmod(abs(y), 2.0) == 1.0
        if y > 0.0:
            if y_is_odd:
                return W_FloatObject(x)
            else:
                return W_FloatObject(abs(x))
        else:
            if y_is_odd:
                return W_FloatObject(copysign(0.0, x))
            else:
                return W_FloatObject(0.0)

    if x == 0.0:
        if y < 0.0:
            raise OperationError(
                space.w_ZeroDivisionError,
                space.wrap("0.0 cannot be raised to "
                           "a negative power"))

    negate_result = False
    # special case: "(-1.0) ** bignum" should not raise ValueError,
    # unlike "math.pow(-1.0, bignum)".  See http://mail.python.org/
    # -           pipermail/python-bugs-list/2003-March/016795.html
    if x < 0.0:
        if isnan(y):
            return W_FloatObject(NAN)
        if math.floor(y) != y:
            raise OperationError(
                space.w_ValueError,
                space.wrap("negative number cannot be "
                           "raised to a fractional power"))
        # y is an exact integer, albeit perhaps a very large one.
        # Replace x by its absolute value and remember to negate the
        # pow result if y is odd.
        x = -x
        negate_result = math.fmod(abs(y), 2.0) == 1.0

    if x == 1.0:
        # (-1) ** large_integer also ends up here
        if negate_result:
            return W_FloatObject(-1.0)
        else:
            return W_FloatObject(1.0)

    try:
        # We delegate to our implementation of math.pow() the error detection.
        z = math.pow(x, y)
    except OverflowError:
        raise FailedToImplementArgs(space.w_OverflowError,
                                    space.wrap("float power"))
    except ValueError:
        raise OperationError(space.w_ValueError, space.wrap("float power"))

    if negate_result:
        z = -z
    return W_FloatObject(z)