예제 #1
0
def test_issues_13081_12583_12534():
    # 13081
    r = Rational('905502432259640373/288230376151711744')
    assert (r < pi) is S.false
    assert (r > pi) is S.true
    # 12583
    v = sqrt(2)
    u = sqrt(v) + 2 / sqrt(10 - 8 / sqrt(2 - v) + 4 * v *
                           (1 / sqrt(2 - v) - 1))
    assert (u >= 0) is S.true
    # 12534; Rational vs NumberSymbol
    # here are some precisions for which Rational forms
    # at a lower and higher precision bracket the value of pi
    # e.g. for p = 20:
    # Rational(pi.n(p + 1)).n(25) = 3.14159265358979323846 2834
    #                    pi.n(25) = 3.14159265358979323846 2643
    # Rational(pi.n(p    )).n(25) = 3.14159265358979323846 1987
    assert [
        p for p in range(20, 50)
        if (Rational(pi.n(p)) < pi) and (pi < Rational(pi.n(p + 1)))
    ] == [20, 24, 27, 33, 37, 43, 48]
    # pick one such precision and affirm that the reversed operation
    # gives the opposite result, i.e. if x < y is true then x > y
    # must be false
    for i in (20, 21):
        v = pi.n(i)
        assert rel_check(Rational(v), pi)
        assert rel_check(v, pi)
    assert rel_check(pi.n(20), pi.n(21))
    # Float vs Rational
    # the rational form is less than the floating representation
    # at the same precision
    assert [i for i in range(15, 50) if Rational(pi.n(i)) > pi.n(i)] == []
    # this should be the same if we reverse the relational
    assert [i for i in range(15, 50) if pi.n(i) < Rational(pi.n(i))] == []
예제 #2
0
def test_Float():
    def eq(a, b):
        t = Float("1.0E-15")
        return (-t < a - b < t)

    a = Float(2) ** Float(3)
    assert eq(a.evalf(), Float(8))
    assert eq((pi ** -1).evalf(), Float("0.31830988618379067"))
    a = Float(2) ** Float(4)
    assert eq(a.evalf(), Float(16))
    assert (S(.3) == S(.5)) is False
    x_str = Float((0, '13333333333333', -52, 53))
    x2_str = Float((0, '26666666666666', -53, 53))
    x_hex = Float((0, long(0x13333333333333), -52, 53))
    x_dec = Float((0, 5404319552844595, -52, 53))
    x2_hex = Float((0, long(0x13333333333333)*2, -53, 53))
    assert x_str == x_hex == x_dec == x2_hex == Float(1.2)
    # x2_str and 1.2 are superficially the same
    assert str(x2_str) == str(Float(1.2))
    # but are different at the mpf level
    assert Float(1.2)._mpf_ == (0, long(5404319552844595), -52, 53)
    assert x2_str._mpf_ == (0, long(10808639105689190), -53, 53)

    assert Float((0, long(0), -123, -1)) == Float('nan')
    assert Float((0, long(0), -456, -2)) == Float('inf') == Float('+inf')
    assert Float((1, long(0), -789, -3)) == Float('-inf')

    raises(ValueError, lambda: Float((0, 7, 1, 3), ''))

    assert Float('+inf').is_bounded is False
    assert Float('+inf').is_negative is False
    assert Float('+inf').is_positive is True
    assert Float('+inf').is_unbounded is True
    assert Float('+inf').is_zero is False

    assert Float('-inf').is_bounded is False
    assert Float('-inf').is_negative is True
    assert Float('-inf').is_positive is False
    assert Float('-inf').is_unbounded is True
    assert Float('-inf').is_zero is False

    assert Float('0.0').is_bounded is True
    assert Float('0.0').is_negative is False
    assert Float('0.0').is_positive is False
    assert Float('0.0').is_unbounded is False
    assert Float('0.0').is_zero is True

    # rationality properties
    assert Float(1).is_rational is None
    assert Float(1).is_irrational is None
    assert sqrt(2).n(15).is_rational is None
    assert sqrt(2).n(15).is_irrational is None

    # do not automatically evalf
    def teq(a):
        assert (a.evalf() == a) is False
        assert (a.evalf() != a) is True
        assert (a == a.evalf()) is False
        assert (a != a.evalf()) is True

    teq(pi)
    teq(2*pi)
    teq(cos(0.1, evaluate=False))

    i = 12345678901234567890
    assert _aresame(Float(12, ''), Float('12', ''))
    assert _aresame(Float(Integer(i), ''), Float(i, ''))
    assert _aresame(Float(i, ''), Float(str(i), 20))
    assert not _aresame(Float(str(i)), Float(i, ''))

    # inexact floats (repeating binary = denom not multiple of 2)
    # cannot have precision greater than 15
    assert Float(.125, 22) == .125
    assert Float(2.0, 22) == 2
    assert float(Float('.12500000000000001', '')) == .125
    raises(ValueError, lambda: Float(.12500000000000001, ''))

    # allow spaces
    Float('123 456.123 456') == Float('123456.123456')
    Integer('123 456') == Integer('123456')
    Rational('123 456.123 456') == Rational('123456.123456')
    assert Float(' .3e2') == Float('0.3e2')

    # allow auto precision detection
    assert Float('.1', '') == Float(.1, 1)
    assert Float('.125', '') == Float(.125, 3)
    assert Float('.100', '') == Float(.1, 3)
    assert Float('2.0', '') == Float('2', 2)

    raises(ValueError, lambda: Float("12.3d-4", ""))
    raises(ValueError, lambda: Float(12.3, ""))
    raises(ValueError, lambda: Float('.'))
    raises(ValueError, lambda: Float('-.'))

    zero = Float('0.0')
    assert Float('-0') == zero
    assert Float('.0') == zero
    assert Float('-.0') == zero
    assert Float('-0.0') == zero
    assert Float(0.0) == zero
    assert Float(0) == zero
    assert Float(0, '') == Float('0', '')
    assert Float(1) == Float(1.0)
    assert Float(S.Zero) == zero
    assert Float(S.One) == Float(1.0)

    assert Float(decimal.Decimal('0.1'), 3) == Float('.1', 3)

    assert '{0:.3f}'.format(Float(4.236622)) == '4.237'
    assert '{0:.35f}'.format(Float(pi.n(40), 40)) == '3.14159265358979323846264338327950288'
예제 #3
0
def test_issues_13081_12583_12534():
    # 13081
    r = Rational('905502432259640373/288230376151711744')
    assert (r < pi) is S.false
    assert (r > pi) is S.true
    # 12583
    v = sqrt(2)
    u = sqrt(v) + 2/sqrt(10 - 8/sqrt(2 - v) + 4*v*(1/sqrt(2 - v) - 1))
    assert (u >= 0) is S.true
    # 12534; Rational vs NumberSymbol
    # here are some precisions for which Rational forms
    # at a lower and higher precision bracket the value of pi
    # e.g. for p = 20:
    # Rational(pi.n(p + 1)).n(25) = 3.14159265358979323846 2834
    #                    pi.n(25) = 3.14159265358979323846 2643
    # Rational(pi.n(p    )).n(25) = 3.14159265358979323846 1987
    assert [p for p in range(20, 50) if
            (Rational(pi.n(p)) < pi) and
            (pi < Rational(pi.n(p + 1)))
        ] == [20, 24, 27, 33, 37, 43, 48]
    # pick one such precision and affirm that the reversed operation
    # gives the opposite result, i.e. if x < y is true then x > y
    # must be false
    p = 20
    # Rational vs NumberSymbol
    G = [Rational(pi.n(i)) > pi for i in (p, p + 1)]
    L = [Rational(pi.n(i)) < pi for i in (p, p + 1)]
    assert G == [False, True]
    assert all(i is not j for i, j in zip(L, G))
    # Float vs NumberSymbol
    G = [pi.n(i) > pi for i in (p, p + 1)]
    L = [pi.n(i) < pi for i in (p, p + 1)]
    assert G == [False, True]
    assert all(i is not j for i, j in zip(L, G))
    # Float vs Float
    G = [pi.n(p) > pi.n(p + 1)]
    L = [pi.n(p) < pi.n(p + 1)]
    assert G == [True]
    assert all(i is not j for i, j in zip(L, G))
    # Float vs Rational
    # the rational form is less than the floating representation
    # at the same precision
    assert [i for i in range(15, 50) if Rational(pi.n(i)) > pi.n(i)
        ] == []
    # this should be the same if we reverse the relational
    assert [i for i in range(15, 50) if pi.n(i) < Rational(pi.n(i))
        ] == []
예제 #4
0
def test_Float():
    def eq(a, b):
        t = Float("1.0E-15")
        return -t < a - b < t

    a = Float(2) ** Float(3)
    assert eq(a.evalf(), Float(8))
    assert eq((pi ** -1).evalf(), Float("0.31830988618379067"))
    a = Float(2) ** Float(4)
    assert eq(a.evalf(), Float(16))
    assert (S(0.3) == S(0.5)) is False
    x_str = Float((0, "13333333333333", -52, 53))
    x2_str = Float((0, "26666666666666", -53, 53))
    x_hex = Float((0, long(0x13333333333333), -52, 53))
    x_dec = Float((0, 5404319552844595, -52, 53))
    x2_hex = Float((0, long(0x13333333333333) * 2, -53, 53))
    assert x_str == x_hex == x_dec == x2_hex == Float(1.2)
    # x2_str and 1.2 are superficially the same
    assert str(x2_str) == str(Float(1.2))
    # but are different at the mpf level
    assert Float(1.2)._mpf_ == (0, long(5404319552844595), -52, 53)
    assert x2_str._mpf_ == (0, long(10808639105689190), -53, 53)

    assert Float((0, long(0), -123, -1)) == Float("nan")
    assert Float((0, long(0), -456, -2)) == Float("inf") == Float("+inf")
    assert Float((1, long(0), -789, -3)) == Float("-inf")

    raises(ValueError, lambda: Float((0, 7, 1, 3), ""))

    assert Float("+inf").is_finite is False
    assert Float("+inf").is_negative is False
    assert Float("+inf").is_positive is True
    assert Float("+inf").is_infinite is True
    assert Float("+inf").is_zero is False

    assert Float("-inf").is_finite is False
    assert Float("-inf").is_negative is True
    assert Float("-inf").is_positive is False
    assert Float("-inf").is_infinite is True
    assert Float("-inf").is_zero is False

    assert Float("0.0").is_finite is True
    assert Float("0.0").is_negative is False
    assert Float("0.0").is_positive is False
    assert Float("0.0").is_infinite is False
    assert Float("0.0").is_zero is True

    # rationality properties
    assert Float(1).is_rational is None
    assert Float(1).is_irrational is None
    assert sqrt(2).n(15).is_rational is None
    assert sqrt(2).n(15).is_irrational is None

    # do not automatically evalf
    def teq(a):
        assert (a.evalf() == a) is False
        assert (a.evalf() != a) is True
        assert (a == a.evalf()) is False
        assert (a != a.evalf()) is True

    teq(pi)
    teq(2 * pi)
    teq(cos(0.1, evaluate=False))

    # long integer
    i = 12345678901234567890
    assert same_and_same_prec(Float(12, ""), Float("12", ""))
    assert same_and_same_prec(Float(Integer(i), ""), Float(i, ""))
    assert same_and_same_prec(Float(i, ""), Float(str(i), 20))
    assert same_and_same_prec(Float(str(i)), Float(i, ""))
    assert same_and_same_prec(Float(i), Float(i, ""))

    # inexact floats (repeating binary = denom not multiple of 2)
    # cannot have precision greater than 15
    assert Float(0.125, 22) == 0.125
    assert Float(2.0, 22) == 2
    assert float(Float(".12500000000000001", "")) == 0.125
    raises(ValueError, lambda: Float(0.12500000000000001, ""))

    # allow spaces
    Float("123 456.123 456") == Float("123456.123456")
    Integer("123 456") == Integer("123456")
    Rational("123 456.123 456") == Rational("123456.123456")
    assert Float(" .3e2") == Float("0.3e2")

    # allow auto precision detection
    assert Float(".1", "") == Float(0.1, 1)
    assert Float(".125", "") == Float(0.125, 3)
    assert Float(".100", "") == Float(0.1, 3)
    assert Float("2.0", "") == Float("2", 2)

    raises(ValueError, lambda: Float("12.3d-4", ""))
    raises(ValueError, lambda: Float(12.3, ""))
    raises(ValueError, lambda: Float("."))
    raises(ValueError, lambda: Float("-."))

    zero = Float("0.0")
    assert Float("-0") == zero
    assert Float(".0") == zero
    assert Float("-.0") == zero
    assert Float("-0.0") == zero
    assert Float(0.0) == zero
    assert Float(0) == zero
    assert Float(0, "") == Float("0", "")
    assert Float(1) == Float(1.0)
    assert Float(S.Zero) == zero
    assert Float(S.One) == Float(1.0)

    assert Float(decimal.Decimal("0.1"), 3) == Float(".1", 3)

    assert "{0:.3f}".format(Float(4.236622)) == "4.237"
    assert "{0:.35f}".format(Float(pi.n(40), 40)) == "3.14159265358979323846264338327950288"