Ejemplo n.º 1
0
def test_underscores():
    for lit in VALID_UNDERSCORE_LITERALS:
        if not any(ch in lit for ch in "jJxXoObB"):
            assert Float(lit) == eval(lit)
            # TODO this should work!!!
            assert Float(lit) == Float(lit.replace("_", ""))
    for lit in INVALID_UNDERSCORE_LITERALS:
        if lit in ("0_7", "09_99"):  # octals are not recognized here
            continue
        if not any(ch in lit for ch in "jJxXoObB"):
            with pytest.raises(ValueError):
                Float(lit)
    # Additional test cases; nan and inf are never valid as literals,
    # only in the Float() constructor, but we don't allow underscores
    # in or around them.
    with pytest.raises(ValueError):
        Float("_NaN")
    with pytest.raises(ValueError):
        Float("Na_N")
    with pytest.raises(ValueError):
        Float("IN_F")
    with pytest.raises(ValueError):
        Float("-_INF")
    with pytest.raises(ValueError):
        Float("-INF_")
    # Check that we handle bytes values correctly.
    with pytest.raises(ValueError):
        Float(b"0_.\xff9")
Ejemplo n.º 2
0
def test_non_numeric_input_types() -> None:
    # Test possible non-numeric types for the argument x, including
    # subclasses of the explicitly documented accepted types.
    class CustomStr(str):
        pass

    class CustomBytes(bytes):
        pass

    class CustomByteArray(bytearray):
        pass

    factories = [
        bytes,
        bytearray,
        lambda b: CustomStr(b.decode()),
        CustomBytes,
        CustomByteArray,
        memoryview,
    ]
    try:
        # stdlib
        from array import array
    except ImportError:
        pass
    else:
        factories.append(lambda b: array("B", b))

    for f in factories:
        x = f(b" 3.14  ")
        assert Float(x) == 3.14
        with pytest.raises(ValueError):
            Float(f(b"A" * 0x10))
Ejemplo n.º 3
0
def test_send(client: sy.VirtualMachineClient) -> None:
    syft_float = Float(5)
    ptr = syft_float.send(client)
    # Check pointer type
    assert ptr.__class__.__name__ == "FloatPointer"

    # Check that we can get back the object
    res = ptr.get()
    assert res == syft_float
Ejemplo n.º 4
0
def test_send() -> None:
    alice = sy.VirtualMachine(name="alice")
    alice_client = alice.get_client()

    syft_float = Float(5)
    ptr = syft_float.send(alice_client)
    # Check pointer type
    assert ptr.__class__.__name__ == "FloatPointer"

    # Check that we can get back the object
    res = ptr.get()
    assert res == syft_float
Ejemplo n.º 5
0
def test_serde() -> None:
    syft_float = Float(5)

    serialized = syft_float._object2proto()

    assert isinstance(serialized, Float_PB)

    deserialized = Float._proto2object(proto=serialized)

    assert isinstance(deserialized, Float)
    assert deserialized.id == syft_float.id
    assert deserialized == syft_float
Ejemplo n.º 6
0
def test_api_float(op, py_obj):
    sy_float = Float(42.5)
    py_float = 42.5

    try:
        func_py = getattr(py_float, op)
    except Exception:
        return

    func_sy = getattr(sy_float, op)

    pypy_err, sypy_err = None, None
    pypy, sypy = None, None

    try:
        pypy = func_py(py_obj)
    except Exception as e_pypy:
        pypy_err = str(e_pypy)

    try:
        sypy = func_sy(py_obj)
    except Exception as e_sysy:
        sypy_err = str(e_sysy)

    if any([pypy_err, sypy_err]):
        assert pypy_err == sypy_err
    else:
        assert pypy == sypy
Ejemplo n.º 7
0
def test_api_sanity_check():
    sy_int = Float(42.5)
    py_int = 42.5
    sy_int_API = set(dir(sy_int))
    py_int_API = set(dir(py_int))
    print(sy_int_API - py_int_API)

    assert len(py_int_API - sy_int_API) == 0
Ejemplo n.º 8
0
def test_floatasratio() -> None:
    for f, ratio in [
        (0.875, (7, 8)),
        (-0.875, (-7, 8)),
        (0.0, (0, 1)),
        (11.5, (23, 2)),
    ]:
        assert f.as_integer_ratio() == ratio

    for i in range(10000):
        f = random.random()
        f *= 10**random.randint(-100, 100)
        n, d = f.as_integer_ratio()
        assert Float(n).__truediv__(d) == f

    with pytest.raises(OverflowError):
        Float("inf").as_integer_ratio()
    with pytest.raises(OverflowError):
        Float("-inf").as_integer_ratio()
    with pytest.raises(ValueError):
        Float("nan").as_integer_ratio()
Ejemplo n.º 9
0
def test_short_repr() -> None:
    # test short Float repr introduced in Python 3.1.  One aspect
    # of this repr is that we get some degree of str -> Float ->
    # str roundtripping.  In particular, for any numeric string
    # containing 15 or fewer significant digits, those exact same
    # digits (modulo trailing zeros) should appear in the output.
    # No more repr(0.03) -> "0.029999999999999999"!

    test_strings = [
        # output always includes *either* a decimal point and at
        # least one digit after that point, or an exponent.
        "0.0",
        "1.0",
        "0.01",
        "0.02",
        "0.03",
        "0.04",
        "0.05",
        "1.23456789",
        "10.0",
        "100.0",
        # values >= 1e16 get an exponent...
        "1000000000000000.0",
        "9999999999999990.0",
        "1e+16",
        "1e+17",
        # ... and so do values < 1e-4
        "0.001",
        "0.001001",
        "0.00010000000000001",
        "0.0001",
        "9.999999999999e-05",
        "1e-05",
        # values designed to provoke failure if the FPU rounding
        # precision isn't set correctly
        "8.72293771110361e+25",
        "7.47005307342313e+26",
        "2.86438000439698e+28",
        "8.89142905246179e+28",
        "3.08578087079232e+35",
    ]

    for s in test_strings:
        negs = "-" + s
        assert s == repr(Float(s))
        assert negs == repr(Float(negs))
        # Since Python 3.2, repr and str are identical
        assert repr(Float(s)) == str(Float(s))
        assert repr(Float(negs)) == str(Float(negs))
Ejemplo n.º 10
0
def test_is_integer() -> None:
    assert not Float(1.1).is_integer()
    assert Float(1.0).is_integer()
    assert not Float("nan").is_integer()
    assert not Float("inf").is_integer()
Ejemplo n.º 11
0
def test_keyword_args() -> None:
    with pytest.raises(TypeError):
        Float(x="3.14")
Ejemplo n.º 12
0
 def __float__(self):
     return Float(str(self)) + 1
Ejemplo n.º 13
0
 def __new__(cls, value=0.0):
     return Float.__new__(cls, 2 * value)
Ejemplo n.º 14
0
def test_Floatconversion() -> None:
    # Make sure that calls to __float__() work properly
    class Foo1(object):
        def __float__(self):
            return 42.0

    class Foo2(Float):
        def __float__(self):
            return 42.0

    class Foo3(Float):
        def __new__(cls, value=0.0):
            return Float.__new__(cls, 2 * value)

        def __float__(self):
            return self

    class Foo4(Float):
        def __float__(self):
            return 42

    # Issue 5759: __Float__ not called on str subclasses (though it is on
    # unicode subclasses).
    class FooStr(str):
        def __float__(self):
            return Float(str(self)) + 1

    assert Float(Foo1()) == 42.0
    assert Float(Foo2()) == 42.0

    with pytest.raises(TypeError):
        Float(Foo4(42))
    assert Float(FooStr("8")) == 9.0

    class Foo5:
        def __Float__(self):
            return ""

    with pytest.raises(TypeError):
        time.sleep(Foo5())

    # using __index__ in init was added in python 3.8
    # https://github.com/python/cpython/commit/bdbad71b9def0b86433de12cecca022eee91bd9f
    if sys.version_info >= (3, 8):

        class MyIndex:
            def __init__(self, value):
                self.value = value

            def __index__(self):
                return self.value

        assert Float(MyIndex(42)) == 42.0
        with pytest.raises(OverflowError):
            Float(MyIndex(2**2000))

    class MyInt:
        def __int__(self):
            return 42

    with pytest.raises(TypeError):
        Float(MyInt())
Ejemplo n.º 15
0
def test_nan_signs():
    # When using the dtoa.c code, the sign of Float('nan') should
    # be predictable.
    assert copysign(1.0, Float("nan")) == 1.0
    assert copysign(1.0, Float("-nan")) == -1.0
Ejemplo n.º 16
0
def test_Float_pow() -> None:
    # test builtin pow and ** operator for IEEE 754 special cases.
    # Special cases taken from section F.Float(9.4).4 of the C99 specification

    for pow_op in pow, operator.pow:
        # x**NAN is NAN for any x except 1
        assert isnan(pow_op(-INF, NAN))
        assert isnan(pow_op(Float(Float(-2.0)), NAN))
        assert isnan(pow_op(Float(-1.0), NAN))
        assert isnan(pow_op(Float(-0.5), NAN))
        assert isnan(pow_op(Float(0.5), NAN))
        assert isnan(pow_op(Float(2.0), NAN))
        assert isnan(pow_op(INF, NAN))
        assert isnan(pow_op(NAN, NAN))

        # NAN**y is NAN for any y except +-0
        assert isnan(pow_op(NAN, -INF))
        assert isnan(pow_op(NAN, Float(-2.0)))
        assert isnan(pow_op(NAN, Float(-1.0)))
        assert isnan(pow_op(NAN, Float(-0.5)))
        assert isnan(pow_op(NAN, Float(0.5)))
        assert isnan(pow_op(NAN, Float(1.0)))
        assert isnan(pow_op(NAN, Float(2.0)))
        assert isnan(pow_op(NAN, INF))

        # (+-0)**y raises ZeroDivisionError for y a negative odd integer
        with pytest.raises(ZeroDivisionError):
            pow_op(Float(-0.0), Float(-1.0))

        with pytest.raises(ZeroDivisionError):
            pow_op(Float(0.0), Float(-1.0))

        # (+-0)**y raises ZeroDivisionError for y finite and negative
        # but not an odd integer
        with pytest.raises(ZeroDivisionError):
            pow_op(Float(-0.0), Float(-2.0))
        with pytest.raises(ZeroDivisionError):
            pow_op(Float(-0.0), Float(-0.5))
        with pytest.raises(ZeroDivisionError):
            pow_op(Float(0.0), Float(-2.0))
        with pytest.raises(ZeroDivisionError):
            pow_op(Float(0.0), Float(-0.5))

        # (+-0)**y is +-0 for y a positive odd integer
        assert pow_op(Float(-0.0), Float(1.0)) == -0.0
        assert pow_op(Float(0.0), Float(1.0)) == 0.0

        # (+-0)**y is 0 for y finite and positive but not an odd integer
        assert pow_op(Float(-0.0), Float(0.5)) == 0.0
        assert pow_op(Float(-0.0), Float(2.0)) == 0.0
        assert pow_op(Float(0.0), Float(0.5)) == 0.0
        assert pow_op(Float(0.0), Float(2.0)) == 0.0

        # (-1)**+-inf is 1
        assert pow_op(Float(-1.0), -INF) == 1.0

        # 1**y is 1 for any y, even if y is an infinity or nan
        assert pow_op(Float(1.0), -INF) == 1.0
        assert pow_op(Float(1.0), Float(-2.0)) == 1.0
        assert pow_op(Float(1.0), Float(-1.0)) == 1.0
        assert pow_op(Float(1.0), Float(-0.5)) == 1.0
        assert pow_op(Float(1.0), Float(-0.0)) == 1.0
        assert pow_op(Float(1.0), Float(0.0)) == 1.0
        assert pow_op(Float(1.0), Float(0.5)) == 1.0
        assert pow_op(Float(1.0), Float(1.0)) == 1.0
        assert pow_op(Float(1.0), Float(2.0)) == 1.0
        assert pow_op(Float(1.0), INF) == 1.0
        assert pow_op(Float(1.0), NAN) == 1.0

        # x**+-0 is 1 for any x, even if x is a zero, infinity, or nan
        assert pow_op(-INF, Float(0.0)) == 1.0
        assert pow_op(Float(-2.0), Float(0.0)) == 1.0
        assert pow_op(Float(-1.0), Float(0.0)) == 1.0
        assert pow_op(Float(-0.5), Float(0.0)) == 1.0
        assert pow_op(Float(-0.0), Float(0.0)) == 1.0
        assert pow_op(Float(0.0), Float(0.0)) == 1.0
        assert pow_op(Float(0.5), Float(0.0)) == 1.0
        assert pow_op(Float(1.0), Float(0.0)) == 1.0
        assert pow_op(Float(2.0), Float(0.0)) == 1.0
        assert pow_op(INF, Float(0.0)) == 1.0
        assert pow_op(NAN, Float(0.0)) == 1.0
        assert pow_op(-INF, Float(-0.0)) == 1.0
        assert pow_op(Float(-2.0), Float(-0.0)) == 1.0
        assert pow_op(Float(-1.0), Float(-0.0)) == 1.0
        assert pow_op(Float(-0.5), Float(-0.0)) == 1.0
        assert pow_op(Float(-0.0), Float(-0.0)) == 1.0
        assert pow_op(Float(0.0), Float(-0.0)) == 1.0
        assert pow_op(Float(0.5), Float(-0.0)) == 1.0
        assert pow_op(Float(1.0), Float(-0.0)) == 1.0
        assert pow_op(Float(2.0), Float(-0.0)) == 1.0
        assert pow_op(INF, Float(-0.0)) == 1.0
        assert pow_op(NAN, Float(-0.0)) == 1.0

        # x**y defers to complex pow for finite negative x and
        # non-integral y.
        assert isinstance(pow_op(Float(-2.0), Float(-0.5)), complex)
        assert isinstance(pow_op(Float(-2.0), Float(0.5)), complex)
        assert isinstance(pow_op(Float(-1.0), Float(-0.5)), complex)
        assert isinstance(pow_op(-1.0, Float(0.5)), complex)
        assert isinstance(pow_op(Float(-0.5), Float(-0.5)), complex)
        assert isinstance(pow_op(Float(-0.5), Float(0.5)), complex)

        # x**-INF is INF for abs(x) < 1
        assert pow_op(Float(-0.5), -INF) == INF
        assert pow_op(Float(-0.0), -INF) == INF
        assert pow_op(Float(0.0), -INF) == INF
        assert pow_op(Float(0.5), -INF) == INF

        # x**-INF is 0 for abs(x) > 1
        assert pow_op(-INF, -INF) == 0.0
        assert pow_op(Float(-2.0), -INF) == 0.0
        assert pow_op(Float(2.0), -INF) == 0.0
        assert pow_op(INF, -INF) == 0.0

        # x**INF is 0 for abs(x) < 1
        assert pow_op(Float(-0.5), INF) == 0.0
        assert pow_op(Float(-0.0), INF) == 0.0
        assert pow_op(Float(0.0), INF) == 0.0
        assert pow_op(Float(0.5), INF) == 0.0

        # x**INF is INF for abs(x) > 1
        assert pow_op(-INF, INF) == INF
        assert pow_op(Float(-2.0), INF) == INF
        assert pow_op(Float(2.0), INF) == INF
        assert pow_op(INF, INF) == INF

        # (-INF)**y is -0.0 for y a negative odd integer
        assert pow_op(-INF, Float(-1.0)) == -0.0

        # (-INF)**y is 0.0 for y negative but not an odd integer
        assert pow_op(-INF, Float(-0.5)) == 0.0
        assert pow_op(-INF, Float(-2.0)) == 0.0

        # (-INF)**y is -INF for y a positive odd integer
        assert pow_op(-INF, Float(1.0)) == -INF

        # (-INF)**y is INF for y positive but not an odd integer
        assert pow_op(-INF, Float(0.5)) == INF
        assert pow_op(-INF, Float(2.0)) == INF

        # INF**y is INF for y positive
        assert pow_op(INF, Float(0.5)) == INF
        assert pow_op(INF, Float(1.0)) == INF
        assert pow_op(INF, Float(2.0)) == INF

        # INF**y is 0.0 for y negative
        assert pow_op(INF, Float(-2.0)) == 0.0
        assert pow_op(INF, Float(-1.0)) == 0.0
        assert pow_op(INF, Float(-0.5)) == 0.0

        # basic checks not covered by the special cases above
        assert pow_op(Float(-2.0), Float(-2.0)) == 0.25
        assert pow_op(Float(-2.0), Float(-1.0)) == Float(-0.5)
        assert pow_op(Float(-2.0), Float(-0.0)) == Float(1.0)
        assert pow_op(Float(-2.0), Float(0.0)) == Float(1.0)
        assert pow_op(Float(-2.0), Float(1.0)) == Float(-2.0)
        assert pow_op(Float(-2.0), Float(2.0)) == Float(4.0)
        assert pow_op(Float(-1.0), Float(-2.0)) == Float(1.0)
        assert pow_op(Float(-1.0), Float(-1.0)) == Float(-1.0)
        assert pow_op(Float(-1.0), Float(-0.0)) == Float(1.0)
        assert pow_op(Float(-1.0), Float(0.0)) == Float(1.0)
        assert pow_op(Float(-1.0), Float(1.0)) == Float(-1.0)
        assert pow_op(Float(-1.0), Float(2.0)) == Float(1.0)
        assert pow_op(Float(2.0), Float(-2.0)) == Float(0.25)
        assert pow_op(Float(2.0), Float(-1.0)) == Float(0.5)
        assert pow_op(Float(2.0), Float(-0.0)) == Float(1.0)
        assert pow_op(Float(2.0), Float(0.0)) == Float(1.0)
        assert pow_op(Float(2.0), Float(1.0)) == Float(2.0)
        assert pow_op(Float(2.0), Float(2.0)) == Float(4.0)

        # 1 ** large and -1 ** large; some libms apparently
        # have problems with these
        assert pow_op(Float(1.0), -1e100) == Float(1.0)
        assert pow_op(Float(1.0), 1e100) == Float(1.0)
        assert pow_op(Float(-1.0), -1e100) == Float(1.0)
        assert pow_op(Float(-1.0), 1e100) == Float(1.0)

        # check sign for results that underflow to 0
        assert pow_op(Float(-2.0), Float(-2000.0)) == Float(0.0)
        assert isinstance(pow_op(Float(-2.0), Float(-2000.5)), complex)
        assert pow_op(Float(-2.0), Float(-2001.0)) == Float(-0.0)
        assert pow_op(Float(2.0), Float(-2000.0)) == Float(0.0)
        assert pow_op(Float(2.0), Float(-2000.5)) == Float(0.0)
        assert pow_op(Float(2.0), Float(-2001.0)) == Float(0.0)
        assert pow_op(Float(-0.5), Float(2000.0)) == Float(0.0)
        assert isinstance(pow_op(Float(-0.5), Float(2000.5)), complex)
        assert pow_op(Float(-0.5), Float(2001.0)) == Float(-0.0)
        assert pow_op(Float(0.5), Float(2000.0)) == Float(0.0)
        assert pow_op(Float(0.5), Float(2000.5)) == Float(0.0)
        assert pow_op(Float(0.5), Float(2001.0)) == Float(0.0)
Ejemplo n.º 17
0
def test_inf_from_str() -> None:
    assert isinf(Float("inf"))
    assert isinf(Float("+inf"))
    assert isinf(Float("-inf"))
    assert isinf(Float("infinity"))
    assert isinf(Float("+infinity"))
    assert isinf(Float("-infinity"))

    assert repr(Float("inf")) == "inf"
    assert repr(Float("+inf")) == "inf"
    assert repr(Float("-inf")) == "-inf"
    assert repr(Float("infinity")) == "inf"
    assert repr(Float("+infinity")) == "inf"
    assert repr(Float("-infinity")) == "-inf"

    assert repr(Float("INF")) == "inf"
    assert repr(Float("+Inf")) == "inf"
    assert repr(Float("-iNF")) == "-inf"
    assert repr(Float("Infinity")) == "inf"
    assert repr(Float("+iNfInItY")) == "inf"
    assert repr(Float("-INFINITY")) == "-inf"

    assert str(Float("inf")) == "inf"
    assert str(Float("+inf")) == "inf"
    assert str(Float("-inf")) == "-inf"
    assert str(Float("infinity")) == "inf"
    assert str(Float("+infinity")) == "inf"
    assert str(Float("-infinity")) == "-inf"

    with pytest.raises(ValueError):
        Float("info")
    with pytest.raises(ValueError):
        Float("+info")
    with pytest.raises(ValueError):
        Float("-info")
    with pytest.raises(ValueError):
        Float("in")
    with pytest.raises(ValueError):
        Float("+in")
    with pytest.raises(ValueError):
        Float("-in")
    with pytest.raises(ValueError):
        Float("infinit")
    with pytest.raises(ValueError):
        Float("+Infin")
    with pytest.raises(ValueError):
        Float("-INFI")
    with pytest.raises(ValueError):
        Float("infinitys")

    with pytest.raises(ValueError):
        Float("++Inf")
    with pytest.raises(ValueError):
        Float("-+inf")
    with pytest.raises(ValueError):
        Float("+-infinity")
    with pytest.raises(ValueError):
        Float("--Infinity")
Ejemplo n.º 18
0
def test_previous_round_bugs() -> None:
    # particular cases that have occurred in bug reports
    assert round(Float(25.0), -1) == Float(20.0)
    assert round(Float(35.0), -1) == Float(40.0)
    assert round(Float(45.0), -1) == Float(40.0)
    assert round(Float(55.0), -1) == Float(60.0)
    assert round(Float(65.0), -1) == Float(60.0)
    assert round(Float(75.0), -1) == Float(80.0)
    assert round(Float(85.0), -1) == Float(80.0)
    assert round(Float(95.0), -1) == Float(100.0)
Ejemplo n.º 19
0
def test_float() -> None:
    assert Float(3.14) == 3.14
    assert Float(314) == 314.0
    assert Float("  3.14  ") == 3.14
    with pytest.raises(ValueError):
        Float("  0x3.1  ")
    with pytest.raises(ValueError):
        Float("  -0x3.p-1  ")
    with pytest.raises(ValueError):
        Float("  +0x3.p-1  ")
    with pytest.raises(ValueError):
        Float("++3.14")
    with pytest.raises(ValueError):
        Float("+-3.14")
    with pytest.raises(ValueError):
        Float("-+3.14")
    with pytest.raises(ValueError):
        Float("--3.14")
    with pytest.raises(ValueError):
        Float(".nan")
    with pytest.raises(ValueError):
        Float("+.inf")
    with pytest.raises(ValueError):
        Float(".")
    with pytest.raises(ValueError):
        Float("-.")
    with pytest.raises(TypeError):
        Float({})

    with pytest.raises(TypeError):
        Float({})

    # Lone surrogate
    with pytest.raises(ValueError):
        Float("\uD8F0")

    # check that we don't accept alternate exponent markers
    with pytest.raises(ValueError):
        Float("-1.7d29")

    with pytest.raises(ValueError):
        Float("3D-14")

    assert Float("  \u0663.\u0661\u0664  ") == 3.14
    assert Float("\N{EM SPACE}3.14\N{EN SPACE}") == 3.14

    # extra long strings should not be a problem
    Float(b"." + b"1" * 1000)
    Float("." + "1" * 1000)

    with pytest.raises(ValueError):
        Float("\u3053\u3093\u306b\u3061\u306f")
Ejemplo n.º 20
0
    "1.4_j",
    "1.4e5_j",
    # Underscore right before e:
    "1_e1",
    "1.4_e1",
    "1.4_e1j",
    # Underscore right after e:
    "1e_1",
    "1.4e_1",
    "1.4e_1j",
    # Complex cases with parens:
    "(1+1.5_j_)",
    "(1+1.5_j)",
]

INF = Float("inf")
NAN = Float("nan")

have_getformat = hasattr(Float, "__getformat__")


class FloatSubclass(Float):
    pass


class OtherFloatSubclass(Float):
    pass


class F:
    def __float__(self):
Ejemplo n.º 21
0
def test_inf_signs():
    assert copysign(1.0, Float("inf")) == 1.0
    assert copysign(1.0, Float("-inf")) == -1.0
Ejemplo n.º 22
0
def test_protobuf():
    assert Float.get_protobuf_schema()
    assert FloatWrapper.get_wrapped_type() is Float
Ejemplo n.º 23
0
def test_Float_mod() -> None:
    # Check behaviour of % operator for IEEE 754 special cases.
    # In particular, check signs of zeros.
    mod = operator.mod

    assert mod(Float(-1.0), Float(1.0)) == 0.0
    assert mod(Float(-1e-100), Float(1.0)) == 1.0
    assert mod(Float(-0.0), Float(1.0)) == 0.0
    assert mod(Float(0.0), Float(1.0)) == 0.0
    assert mod(Float(1e-100), Float(1.0)) == 1e-100
    assert mod(Float(1.0), Float(1.0)) == 0.0

    assert mod(Float(-1.0), Float(-1.0)) == -0.0
    assert mod(Float(-1e-100), Float(-1.0)) == -1e-100
    assert mod(Float(-0.0), Float(-1.0)) == -0.0
    assert mod(Float(0.0), Float(-1.0)) == -0.0
    assert mod(Float(1e-100), Float(-1.0)) == -1.0
    assert mod(Float(1.0), Float(-1.0)) == -0.0
Ejemplo n.º 24
0
def test_previous_round_bugs():
    # particular cases that have occurred in bug reports
    assert round(562949953421312.5, 1) == 562949953421312.5
    assert round(56294995342131.5, 3) == 56294995342131.5
    # round-half-even
    assert round(Float(25.0), -1) == Float(20.0)
    assert round(Float(35.0), -1) == Float(40.0)
    assert round(Float(45.0), -1) == Float(40.0)
    assert round(Float(55.0), -1) == Float(60.0)
    assert round(Float(65.0), -1) == Float(60.0)
    assert round(Float(75.0), -1) == Float(80.0)
    assert round(Float(85.0), -1) == Float(80.0)
    assert round(Float(95.0), -1) == Float(100.0)
Ejemplo n.º 25
0
def test_Float_with_comma() -> None:
    # set locale to something that doesn't use '.' for the decimal point
    # Float must not accept the locale specific decimal point but
    # it still has to accept the normal python syntax

    assert Float("  3.14  ") == 3.14
    assert Float("+3.14  ") == 3.14
    assert Float("-3.14  ") == -3.14
    assert Float(".14  ") == 0.14
    assert Float("3.  ") == 3.0
    assert Float("3.e3  ") == 3000.0
    assert Float("3.2e3  ") == 3200.0
    assert Float("2.5e-1  ") == 0.25
    assert Float("5e-1") == 0.5
    with pytest.raises(ValueError):
        Float("  3,14  ")
    with pytest.raises(ValueError):
        Float("  +3,14  ")
    with pytest.raises(ValueError):
        Float("  -3,14  ")
    with pytest.raises(ValueError):
        Float("  0x3.1  ")
    with pytest.raises(ValueError):
        Float("  -0x3.p-1  ")
    with pytest.raises(ValueError):
        Float("  +0x3.p-1  ")
    assert Float("  25.e-1  ") == 2.5
Ejemplo n.º 26
0
def test_float() -> None:
    assert SyFalse.__float__() == Float(0.0)
    assert SyFalse.__float__() == SyFalse
    assert SyTrue.__float__() == Float(1.0)
    assert SyTrue.__float__() == SyTrue
Ejemplo n.º 27
0
def test_matches_Float_format() -> None:
    # round should give the same results as Float formatting
    for i in range(500):
        x = i / 1000.0
        assert Float(format(x, ".0f")) == round(x, 0)
        assert Float(format(x, ".1f")) == round(x, 1)
        assert Float(format(x, ".2f")) == round(x, 2)

    for i in range(5, 5000, 10):
        x = i / 1000.0
        assert Float(format(x, ".0f")) == round(x, 0)
        assert Float(format(x, ".1f")) == round(x, 1)
        assert Float(format(x, ".2f")) == round(x, 2)
        assert Float(format(x, ".3f")) == round(x, 3)

    for i in range(500):
        x = random.random()
        assert Float(format(x, ".0f")) == round(x, 0)
        assert Float(format(x, ".1f")) == round(x, 1)
        assert Float(format(x, ".2f")) == round(x, 2)
        assert Float(format(x, ".3f")) == round(x, 3)
Ejemplo n.º 28
0
def test_Float_memoryview() -> None:
    assert Float(memoryview(b"12.3")[1:4]) == 2.3
    assert Float(memoryview(b"12.3\x00")[1:4]) == 2.3
    assert Float(memoryview(b"12.3 ")[1:4]) == 2.3
    assert Float(memoryview(b"12.3A")[1:4]) == 2.3
    assert Float(memoryview(b"12.34")[1:4]) == 2.3
Ejemplo n.º 29
0
def test_nan_from_str() -> None:
    assert isnan(Float("nan"))
    assert isnan(Float("+nan"))
    assert isnan(Float("-nan"))

    assert repr(Float("nan")) == "nan"
    assert repr(Float("+nan")) == "nan"
    assert repr(Float("-nan")) == "nan"

    assert repr(Float("NAN")) == "nan"
    assert repr(Float("+NAn")) == "nan"
    assert repr(Float("-NaN")) == "nan"

    assert str(Float("nan")) == "nan"
    assert str(Float("+nan")) == "nan"
    assert str(Float("-nan")) == "nan"

    with pytest.raises(ValueError):
        Float("nana")
    with pytest.raises(ValueError):
        Float("+nana")
    with pytest.raises(ValueError):
        Float("-nana")
    with pytest.raises(ValueError):
        Float("na")
    with pytest.raises(ValueError):
        Float("+na")
    with pytest.raises(ValueError):
        Float("-na")

    with pytest.raises(ValueError):
        Float("++nan")
    with pytest.raises(ValueError):
        Float("-+NAN")
    with pytest.raises(ValueError):
        Float("+-NaN")
    with pytest.raises(ValueError):
        Float("--nAn")
Ejemplo n.º 30
0
 def check(s):
     with pytest.raises(ValueError):
         Float(s)