Ejemplo n.º 1
0
    def descr_as_integer_ratio(self, space):
        """float.as_integer_ratio() -> (int, int)

        Return a pair of integers, whose ratio is exactly equal to the
        original float and with a positive denominator.  Raise
        OverflowError on infinities and a ValueError on NaNs.

        >>> (10.0).as_integer_ratio()
        (10, 1)
        >>> (0.0).as_integer_ratio()
        (0, 1)
        >>> (-.25).as_integer_ratio()
        (-1, 4)

        """
        value = self.floatval
        try:
            num, den = float_as_rbigint_ratio(value)
        except OverflowError:
            raise oefmt(space.w_OverflowError,
                        "cannot pass infinity to as_integer_ratio()")
        except ValueError:
            raise oefmt(space.w_ValueError,
                        "cannot pass nan to as_integer_ratio()")

        w_num = space.newlong_from_rbigint(num)
        w_den = space.newlong_from_rbigint(den)
        # Try to return int
        return space.newtuple([space.int(w_num), space.int(w_den)])
Ejemplo n.º 2
0
    def descr_as_integer_ratio(self, space):
        value = self.floatval
        try:
            num, den = float_as_rbigint_ratio(value)
        except OverflowError:
            raise oefmt(space.w_OverflowError, "cannot pass infinity to as_integer_ratio()")
        except ValueError:
            raise oefmt(space.w_ValueError, "cannot pass nan to as_integer_ratio()")

        w_num = space.newlong_from_rbigint(num)
        w_den = space.newlong_from_rbigint(den)
        # Try to return int
        return space.newtuple([space.int(w_num), space.int(w_den)])
Ejemplo n.º 3
0
    def descr_as_integer_ratio(self, space):
        value = self.floatval
        try:
            num, den = float_as_rbigint_ratio(value)
        except OverflowError:
            raise oefmt(space.w_OverflowError,
                        "cannot pass infinity to as_integer_ratio()")
        except ValueError:
            raise oefmt(space.w_ValueError,
                        "cannot pass nan to as_integer_ratio()")

        w_num = space.newlong_from_rbigint(num)
        w_den = space.newlong_from_rbigint(den)
        # Try to return int
        return space.newtuple([space.int(w_num), space.int(w_den)])
Ejemplo n.º 4
0
def float_as_integer_ratio__Float(space, w_float):
    value = w_float.floatval
    try:
        num, den = float_as_rbigint_ratio(value)
    except OverflowError:
        w_msg = space.wrap("cannot pass infinity to as_integer_ratio()")
        raise OperationError(space.w_OverflowError, w_msg)
    except ValueError:
        w_msg = space.wrap("cannot pass nan to as_integer_ratio()")
        raise OperationError(space.w_ValueError, w_msg)

    w_num = space.newlong_from_rbigint(num)
    w_den = space.newlong_from_rbigint(den)
    # Try to return int
    return space.newtuple([space.int(w_num), space.int(w_den)])
Ejemplo n.º 5
0
    def descr_as_integer_ratio(self, space):
        value = self.floatval
        try:
            num, den = float_as_rbigint_ratio(value)
        except OverflowError:
            raise oefmt(space.w_OverflowError,
                        "no puede pasar infinito a como_ratio_entero()")
        except ValueError:
            raise oefmt(space.w_ValueError,
                        "no puede pasar NuN a como_ratio_entero()")

        w_num = space.newlong_from_rbigint(num)
        w_den = space.newlong_from_rbigint(den)
        # Try to return int
        return space.newtuple([space.int(w_num), space.int(w_den)])
Ejemplo n.º 6
0
def test_float_as_rbigint_ratio():
    for f, ratio in [
        (0.875, (7, 8)),
        (-0.875, (-7, 8)),
        (0.0, (0, 1)),
        (11.5, (23, 2)),
    ]:
        num, den = float_as_rbigint_ratio(f)
        assert num.eq(rbigint.fromint(ratio[0]))
        assert den.eq(rbigint.fromint(ratio[1]))

    with py.test.raises(OverflowError):
        float_as_rbigint_ratio(float('inf'))
    with py.test.raises(OverflowError):
        float_as_rbigint_ratio(float('-inf'))
    with py.test.raises(ValueError):
        float_as_rbigint_ratio(float('nan'))
Ejemplo n.º 7
0
def test_float_as_rbigint_ratio():
    for f, ratio in [
        (0.875, (7, 8)),
        (-0.875, (-7, 8)),
        (0.0, (0, 1)),
        (11.5, (23, 2)),
        ]:
        num, den = float_as_rbigint_ratio(f)
        assert num.eq(rbigint.fromint(ratio[0]))
        assert den.eq(rbigint.fromint(ratio[1]))

    with py.test.raises(OverflowError):
        float_as_rbigint_ratio(float('inf'))
    with py.test.raises(OverflowError):
        float_as_rbigint_ratio(float('-inf'))
    with py.test.raises(ValueError):
        float_as_rbigint_ratio(float('nan'))