Last active
March 10, 2018 05:04
-
-
Save komasaru/43c50d3f50b7baa5296f3d85da4deb5f to your computer and use it in GitHub Desktop.
Python script to compute big-digit values.(v2)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/local/bin/python3.6 | |
""" | |
Computation of big-digit values (Ver.2) | |
""" | |
import random | |
import sys | |
import traceback | |
class CalcBigDigits: | |
N_A = 1000 | |
N_B = 996 | |
LIMIT = 4 | |
RANGE = 10**LIMIT | |
SIZE_A = int((N_A-1) / LIMIT) + 1 | |
SIZE_B = int((N_B-1) / LIMIT) + 1 | |
def __init__(self): | |
self.a = [random.randrange(self.RANGE) for _ in range(self.SIZE_A)] | |
self.b = [random.randrange(self.RANGE) for _ in range(self.SIZE_B)] | |
self.c = random.randrange(self.RANGE) | |
print("A =") | |
self.__display(self.a) | |
print("B =") | |
self.__display(self.b) | |
print("C =\n{:04d}\n\n".format(self.c)) | |
def compute_add(self): | |
""" Addition """ | |
try: | |
print("A + B =") | |
self.__display(self.__long_add(self.a, self.b)) | |
except Exception as e: | |
raise | |
def compute_sub(self): | |
""" Subtraction """ | |
try: | |
print("A - B =") | |
self.__display(self.__long_sub(self.a, self.b)) | |
except Exception as e: | |
raise | |
# 計算 ( 乗算 ) | |
def compute_mul(self): | |
""" Multiplication """ | |
try: | |
print("A * C =") | |
self.__display(self.__long_mul(self.a, self.c)) | |
except Exception as e: | |
raise | |
# 計算 ( 除算 ) | |
def compute_div(self): | |
""" Division """ | |
try: | |
print("A / C =") | |
self.__display(self.__long_div(self.a, self.c)) | |
except Exception as e: | |
raise | |
def __long_add(self, a, b): | |
""" Computation of long + long | |
:param list a | |
:param list b | |
:return list z | |
""" | |
try: | |
z = [0 for _ in range(max(len(a), len(b)) + 1)] | |
for i in range(len(a)): | |
z[i] = a[i] | |
for i in range(len(b)): | |
z[i] += b[i] | |
if z[i] >= 10**self.LIMIT: | |
z[i] -= 10**self.LIMIT | |
z[i + 1] += 1 | |
return z | |
except Exception as e: | |
raise | |
def __long_sub(self, a, b): | |
""" Computation of long - long | |
:param list a | |
:param list b | |
:return list z | |
""" | |
try: | |
z = [0 for _ in range(max(len(a), len(b)))] | |
for i in range(len(a)): | |
z[i] = a[i] | |
for i in range(len(b)): | |
z[i] -= b[i] | |
if z[i] < 0: | |
z[i] += 10**self.LIMIT | |
z[i + 1] -= 1 | |
return z | |
except Exception as e: | |
raise | |
def __long_mul(self, a, c): | |
""" Computation of long * short | |
:param list a | |
:param list b | |
:return list z | |
""" | |
try: | |
z = [0 for _ in range(len(a) + 1)] | |
for i in range(len(a)): | |
z[i] += a[i] * c | |
if z[i] >= 10**self.LIMIT: | |
z[i + 1] += int(z[i] / 10**self.LIMIT) | |
z[i] %= 10**self.LIMIT | |
return z | |
except Exception as e: | |
raise | |
def __long_div(self, a, c): | |
""" Computation of long / short | |
:param list a | |
:param list b | |
:return list z | |
""" | |
try: | |
z = [0 for _ in range(len(a))] | |
for i in reversed(range(len(a))): | |
z[i] += int(a[i] / c) | |
if i > 0: | |
a[i - 1] += (a[i] % c) * 10**self.LIMIT | |
return z | |
except Exception as e: | |
raise | |
def __display(self, s): | |
""" Display | |
:param list s | |
""" | |
try: | |
size = len(s) | |
if s[size - 1] == 0: | |
size -= 1 | |
for i in reversed(range(size)): | |
print("{:04d} ".format(s[i]), end="") | |
if (size - i) % 10 == 0 and i != 0: | |
print() | |
print("\n") | |
except Exception as e: | |
raise | |
if __name__ == '__main__': | |
try: | |
obj = CalcBigDigits() | |
obj.compute_add() | |
obj.compute_sub() | |
obj.compute_mul() | |
obj.compute_div() | |
except Exception as e: | |
traceback.print_exc() | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment