コード例 #1
0
ファイル: arithmetic.py プロジェクト: HyeockJinKim/Study-CS
 def div_bits(a: List[Bit], b: List[Bit], length: int) -> List[Bit]:
     """
     Bit List의 길이를 length로 맞춘 후 값을 나누는 ( / ) 함수
     이진수를 나누는 함수
     raw_div_bits 함수를 통해 계산
     :param a: / 앞의 Bit List
     :param b: 나눌 Bit List
     :param length: 원하는 Bit List의 길이
     :return: a / b의 값인 Bit List
     """
     a, b = BitOperation.equalize_bit_length(a, b, length)
     return Arithmetic.raw_div_bits(a, b)
コード例 #2
0
ファイル: arithmetic.py プロジェクト: HyeockJinKim/Study-CS
 def mul_bits(a: List[Bit], b: List[Bit], length: int) -> List[Bit]:
     """
     Bit List의 길이를 length로 맞춘 후 값을 곱하는 ( * ) 함수
     이진수를 곱하는 함수
     raw_mul_bits 함수를 통해 계산
     :param a: * 앞의 Bit List
     :param b: 곱할 Bit List
     :param length: 원하는 Bit List의 길이
     :return: a * b의 값인 Bit List
     """
     a, b = BitOperation.equalize_bit_length(a, b, length)
     return Arithmetic.raw_mul_bits(a, b)
コード例 #3
0
ファイル: arithmetic.py プロジェクト: HyeockJinKim/Study-CS
 def sub_bits(a: List[Bit], b: List[Bit], length: int) -> (List[Bit], Bit):
     """
     Bit List의 길이를 length로 맞춘 후 값을 빼는 ( - ) 함수
     이진수를 빼는 함수
     overflow 될 경우 overflow 값을 함께 return
     raw_sub_bits 함수를 통해 계산
     :param a: - 앞의 Bit List
     :param b: 뺄 Bit List
     :param length: 원하는 Bit List의 길이
     :return: a - b의 값인 Bit List, overflow 된 Bit
     """
     a, b = BitOperation.equalize_bit_length(a, b, length)
     return Arithmetic.raw_sub_bits(a, b)
コード例 #4
0
ファイル: arithmetic.py プロジェクト: HyeockJinKim/Study-CS
 def add_bits(a: List[Bit], b: List[Bit], length: int) -> (List[Bit], Bit):
     """
     Bit List의 길이를 length로 맞춘 후 값을 더하는 ( + ) 함수
     이진수를 더하는 함수
     overflow 될 경우 overflow 값을 함께 return
     raw_add_bits 함수를 통해 계산
     :param a: 더할 Bit List
     :param b: 더할 Bit List
     :param length: 원하는 Bit List의 길이
     :return: a + b의 값인 Bit List, overflow 된 Bit
     """
     a, b = BitOperation.equalize_bit_length(a, b, length)
     return Arithmetic.raw_add_bits(a, b)
コード例 #5
0
ファイル: arithmetic.py プロジェクト: HyeockJinKim/Study-CS
    def str_to_minor(real: List[Bit], val: str, digit: int,
                     length: int) -> (List[Bit], int):
        """
        소수점 아래의 값을 표현하는 문자열을 읽어 Bit List를 생성하는 함수

        :param real: 소수점 위의 값의 Bit List
        :param val: 소수점 아래의 값을 표현하는 문자열
        :param digit: 소수점 위의 자리수
        :param length: 원하는 Bit List의 길이
        :return: 문자열 실수값에 해당하는 Bit List, 자리수
        """
        real, ten = BitOperation.equalize_bit_length(
            real, BitOperation.num_map['10'], length)
        base = BitOperation.fit_bits(BitOperation.num_map['1'], length)
        twenty = BitOperation.raw_lshift_bits(ten, 1)
        remain = BitOperation.empty_bits(length)
        shift = 1
        index = 0
        while True:
            if index < len(val) and index < 6:
                remain = Arithmetic.raw_mul_bits(remain, twenty)
                next_digit = BitOperation.lshift_bits(
                    BitOperation.num_map[val[index]], shift, length)
                remain, _ = Arithmetic.raw_add_bits(remain, next_digit)
                index += 1
                shift += 1
                base = Arithmetic.raw_mul_bits(base, ten)
            else:
                remain = BitOperation.raw_lshift_bits(remain, 1)
                if BitOperation.is_empty(BitOperation.raw_or_bits(
                        remain, real)):
                    return real, -127

            real = BitOperation.raw_lshift_bits(real, 1)
            if BitOperation.raw_ge_bits(remain, base):
                real, _ = Arithmetic.add_bits(real, BitOperation.num_map['1'],
                                              length)
                remain, _ = Arithmetic.sub_bits(remain, base, length)

            if BitOperation.first_bit_index(real) == 0:
                remain = BitOperation.raw_lshift_bits(remain, 1)
                if BitOperation.raw_ge_bits(remain, base):
                    real = BitOperation.or_bits(real,
                                                BitOperation.num_map['1'],
                                                length)
                break
            elif BitOperation.is_empty(real):
                digit -= 1

        return real, digit
コード例 #6
0
ファイル: arithmetic.py プロジェクト: HyeockJinKim/Study-CS
    def str_to_integer(val: str, length: int) -> List[Bit]:
        """
        문자열을 읽어 Bit List를 생성하는 함수
        10진수에서 2진수로 변환하는 함수

        :param val: 숫자를 표현하는 정수 문자열
        :param length: 원하는 Bit List의 길이
        :return: 문자열 숫자에 해당하는 Bit List
        """
        res, ten = BitOperation.equalize_bit_length(BitOperation.num_map['0'],
                                                    BitOperation.num_map['10'],
                                                    length)
        for c in val:
            res = Arithmetic.raw_mul_bits(res, ten)
            res, _ = Arithmetic.add_bits(res, BitOperation.num_map[c], length)
        return res
コード例 #7
0
ファイル: arithmetic.py プロジェクト: HyeockJinKim/Study-CS
    def str_to_integer_until_overflow(val: str,
                                      length: int) -> (List[Bit], int):
        """
        문자열을 읽어 overflow가 발생할 때까지 Bit List를 생성하는 함수
        :param val: 숫자를 표현하는 정수 문자열
        :param length: 원하는 Bit List의 길이
        :return: 문자열 실수값에 해당하는 Bit List, 자리수
        """
        res, ten = BitOperation.equalize_bit_length(BitOperation.num_map['0'],
                                                    BitOperation.num_map['10'],
                                                    length)
        for i, c in enumerate(val):
            res = Arithmetic.raw_mul_bits(res, ten)
            res, res2 = Arithmetic.add_bits(res, BitOperation.num_map[c],
                                            length)

            if res2:
                return res, len(val) - i
        return res, length - BitOperation.first_bit_index(res) - 1