示例#1
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, 0x13333333333333L, -52, 53))
    x_dec = Float((0, 5404319552844595L, -52, 53))
    x2_hex = Float((0, 0x13333333333333L * 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, 5404319552844595L, -52, 53)
    assert x2_str._mpf_ == (0, 10808639105689190L, -53, 53)

    # 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))

    assert Float(0) is S.Zero
    assert Float(1) is S.One

    assert Float(S.Zero) is S.Zero
    assert Float(S.One) is S.One
示例#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, 0x13333333333333L, -52, 53))
    x_dec = Float((0, 5404319552844595L, -52, 53))
    x2_hex = Float((0, 0x13333333333333L*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, 5404319552844595L, -52, 53)
    assert x2_str._mpf_ == (0, 10808639105689190L, -53, 53)

    # 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))

    assert Float(0) is S.Zero
    assert Float(1) is S.One

    assert Float(S.Zero) is S.Zero
    assert Float(S.One) is S.One
def _initial_evalf(w,n):
    """ 
    Convert every number to float with precision n
    If number ends in 5, convert to high precision Float
    and multiply by 1+epsilon so that it will round up correctly.
    
    """

    if w.is_Number:
        wstr=str(w)
        if "." in wstr:
            wstr = wstr.rstrip('0')
            if wstr[-1]=="5":
                # include extra call to str because bug
                # in sympy where this doesn't work with unicode
                w=Float(str(wstr), n+1)
                one_plus_epsilon = S.One.evalf(n)\
                                   +pow(10,1-n)
                w *= one_plus_epsilon
    try:
        return w.evalf(n)
    except:
        return w
示例#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(.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'
示例#5
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, 0x13333333333333L, -52, 53))
    x_dec = Float((0, 5404319552844595L, -52, 53))
    x2_hex = Float((0, 0x13333333333333L*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, 5404319552844595L, -52, 53)
    assert x2_str._mpf_ == (0, 10808639105689190L, -53, 53)

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

    # 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))

    assert Float(0) is S.Zero
    assert Float(1) is S.One

    assert Float(S.Zero) is S.Zero
    assert Float(S.One) is S.One

    i = 12345678901234567890
    assert _aresame(Float(12), Integer(12))
    assert _aresame(Float(12, ''), Float('12', ''))
    assert _aresame(Float(i), Integer(i))
    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')

    # 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('-.'))
    assert Float('-0') == Float('0.0')
    assert Float('.0') == Float('0.0')
    assert Float('-.0') == Float('-0.0')
    assert Float(' .3e2') == Float('0.3e2')
示例#6
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, 0x13333333333333L, -52, 53))
    x_dec = Float((0, 5404319552844595L, -52, 53))
    x2_hex = Float((0, 0x13333333333333L * 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, 5404319552844595L, -52, 53)
    assert x2_str._mpf_ == (0, 10808639105689190L, -53, 53)

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

    # 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))

    assert Float(0) is S.Zero
    assert Float(1) is S.One

    assert Float(S.Zero) is S.Zero
    assert Float(S.One) is S.One

    i = 12345678901234567890
    assert _aresame(Float(12), Integer(12))
    assert _aresame(Float(12, ''), Float('12', ''))
    assert _aresame(Float(i), Integer(i))
    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')

    # 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('-.'))
    assert Float('-0') == Float('0.0')
    assert Float('.0') == Float('0.0')
    assert Float('-.0') == Float('-0.0')
    assert Float(' .3e2') == Float('0.3e2')
示例#7
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, 0x13333333333333L, -52, 53))
    x_dec = Float((0, 5404319552844595L, -52, 53))
    x2_hex = Float((0, 0x13333333333333L * 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, 5404319552844595L, -52, 53)
    assert x2_str._mpf_ == (0, 10808639105689190L, -53, 53)

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

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

    assert Float("+inf").is_bounded is False
    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_unbounded is True
    assert Float("+inf").is_zero is False

    assert Float("-inf").is_bounded 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_unbounded is True
    assert Float("-inf").is_zero is False

    assert Float("0.0").is_bounded is True
    assert Float("0.0").is_finite is False
    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

    # 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(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)