Пример #1
0
    def set(self, _float: float):
        """
        float 값을 통해 float 를 받기 위한 함수

        float 값을 Bit를 통해 float로 어떻게 표현하는 지 로직 확인을 위한 함수
        deprecated
        """

        if _float < 0:
            self.sign = Bit(True)
            _float = -_float
        else:
            self.sign = Bit()

        bits = []
        int_val = int(_float)
        _float -= int_val
        while int_val > 0:
            bits.insert(0, Bit(bool(int_val % 2)))
            int_val //= 2

        del bits[0]
        exp = len(bits)
        exp = UnsignedInteger('127') + UnsignedInteger(str(exp))
        while _float > 0 and len(bits) < self.fraction_len:
            _float *= 2
            bits.append(Bit(int(_float) == 1))
            _float -= int(_float)

        for _ in range(self.fraction_len-len(bits)):
            bits.append(Bit())

        bits[-1] = Bit(bool(_float))
        self.exponents = exp.bits[-self.exponent_len:]
        self.fractions = bits[:self.fraction_len]
def test_unsigned_integer_min_value1():
    assert UnsignedInteger.min_value() == UnsignedInteger('0')
def test_unsigned_integer_min_value2():
    bits = UnsignedInteger.min_value().bits
    for bit in bits:
        assert bit == Bit(False)
def test_unsigned_integer_max_value1():
    assert UnsignedInteger.max_value() == UnsignedInteger(str(2**UnsignedInteger.bit_len-1))
def test_unsigned_integer_max_value2():
    bits = UnsignedInteger.max_value().bits
    for bit in bits:
        assert bit == Bit(True)
def test_unsigned_integer_division3():
    integer1 = UnsignedInteger.max_value()
    integer2 = UnsignedInteger('5')
    assert integer2 / integer1 == UnsignedInteger('0')
def test_unsigned_integer_division5():
    integer1 = UnsignedInteger('0')
    integer2 = UnsignedInteger('0')
    with pytest.raises(ZeroDivisionError):
        integer1 / integer2
def test_unsigned_integer_init1():
    unsigned_integer = UnsignedInteger('3')
    assert str(unsigned_integer) == '3'
def test_unsigned_integer_division2():
    integer1 = UnsignedInteger.max_value()
    integer2 = UnsignedInteger('5')
    assert integer1 / integer2 == UnsignedInteger('858993459')
Пример #10
0
def test_unsigned_integer_division1():
    unsigned_integer1 = UnsignedInteger('20')
    unsigned_integer2 = UnsignedInteger('4')
    assert unsigned_integer1 / unsigned_integer2 == UnsignedInteger('5')
Пример #11
0
def test_unsigned_integer_multiplication():
    unsigned_integer1 = UnsignedInteger('107')
    unsigned_integer2 = UnsignedInteger('97')
    assert unsigned_integer1 * unsigned_integer2 == UnsignedInteger('10379')
Пример #12
0
def test_unsigned_integer_plus4():
    unsigned_integer1 = UnsignedInteger('25')
    unsigned_integer2 = UnsignedInteger('-10')
    assert unsigned_integer1 + unsigned_integer2 == UnsignedInteger('15')
Пример #13
0
def test_unsigned_integer_plus1():
    unsigned_integer1 = UnsignedInteger('3')
    unsigned_integer2 = UnsignedInteger('5')
    assert unsigned_integer1 + unsigned_integer2 == UnsignedInteger('8')
Пример #14
0
def test_unsigned_integer_init2():
    unsigned_integer = UnsignedInteger('-1')
    assert unsigned_integer.val() == 2**UnsignedInteger.bit_len-1