├── APFF.py └── README.md /APFF.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | class BigInt: 4 | def __init__(self, num_str="0"): 5 | self.digits = [int(digit) for digit in num_str] 6 | 7 | def __str__(self): 8 | return "".join(str(digit) for digit in self.digits) 9 | 10 | def __float__(self): 11 | return float(str(self)) 12 | 13 | def __int__(self): 14 | return int(str(self)) 15 | 16 | def __add__(self, other): 17 | return self.addition(self, other) 18 | 19 | def __sub__(self, other): 20 | return self.subtraction(self, other) 21 | 22 | def __mul__(self, other): 23 | return self.multiplication(self, other) 24 | 25 | def __truediv__(self, other): 26 | return self.division(self, other) 27 | 28 | def __pow__(self, other): 29 | if not isinstance(other, int): 30 | raise ValueError("Exponent must be an integer.") 31 | return BigInt(str(int(self) ** other)) 32 | 33 | def root(self, n): 34 | if not isinstance(n, int) or n <= 0: 35 | raise ValueError("Root must be a positive integer.") 36 | return BigInt(str(int(self) ** (1/n))) 37 | 38 | def addition(self, num1, num2): 39 | result = [] 40 | carry = 0 41 | i, j = len(num1.digits) - 1, len(num2.digits) - 1 42 | 43 | while i >= 0 or j >= 0 or carry: 44 | d1 = num1.digits[i] if i >= 0 else 0 45 | d2 = num2.digits[j] if j >= 0 else 0 46 | total = d1 + d2 + carry 47 | carry, digit = divmod(total, 10) 48 | result.insert(0, digit) 49 | i, j = i - 1, j - 1 50 | 51 | return BigInt("".join(str(digit) for digit in result)) 52 | 53 | def subtraction(self, num1, num2): 54 | if num1 < num2: 55 | return "-" + self.subtraction(num2, num1).__str__() 56 | 57 | result = [] 58 | borrow = 0 59 | i, j = len(num1.digits) - 1, len(num2.digits) - 1 60 | 61 | while i >= 0: 62 | d1 = num1.digits[i] 63 | d2 = num2.digits[j] if j >= 0 else 0 64 | diff = d1 - d2 - borrow 65 | 66 | if diff < 0: 67 | diff += 10 68 | borrow = 1 69 | else: 70 | borrow = 0 71 | 72 | result.insert(0, diff) 73 | i, j = i - 1, j - 1 74 | 75 | # Remove leading zeros 76 | while result[0] == 0 and len(result) > 1: 77 | result.pop(0) 78 | 79 | return BigInt("".join(str(digit) for digit in result)) 80 | 81 | def multiplication(self, num1, num2): 82 | result = BigInt("0") 83 | 84 | for i, digit in enumerate(reversed(num2.digits)): 85 | temp = [] 86 | carry = 0 87 | 88 | for j in reversed(num1.digits): 89 | prod = digit * j + carry 90 | carry, val = divmod(prod, 10) 91 | temp.insert(0, val) 92 | 93 | if carry: 94 | temp.insert(0, carry) 95 | 96 | for _ in range(i): 97 | temp.append(0) 98 | 99 | result = result.addition(result, BigInt("".join(str(digit) for digit in temp))) 100 | 101 | return result 102 | 103 | def division(self, num1, num2): 104 | if int(num2) == 0: 105 | raise ValueError("Division by zero is not allowed.") 106 | 107 | dividend = int(num1) 108 | divisor = int(num2) 109 | 110 | quotient = dividend // divisor 111 | remainder = dividend % divisor 112 | 113 | return BigInt(str(quotient)), BigInt(str(remainder)) 114 | 115 | 116 | def main(): 117 | try: 118 | num1 = input("Enter the first number: ") 119 | num2 = input("Enter the second number: ") 120 | operator = input("Enter the operator (+, -, *, /, ^, root, to_celsius, to_fahrenheit, " 121 | "memory_store, memory_recall, memory_clear): ") 122 | 123 | big_num1 = BigInt(num1) 124 | big_num2 = BigInt(num2) 125 | 126 | if operator == '+': 127 | result = big_num1 + big_num2 128 | elif operator == '-': 129 | result = big_num1 - big_num2 130 | elif operator == '*': 131 | result = big_num1 * big_num2 132 | elif operator == '/': 133 | quotient, remainder = big_num1 / big_num2 134 | print("Quotient:", quotient) 135 | print("Remainder:", remainder) 136 | return 137 | elif operator == '^': 138 | exponent = int(input("Enter the exponent: ")) 139 | result = big_num1 ** exponent 140 | elif operator == 'root': 141 | root_val = int(input("Enter the root value: ")) 142 | result = big_num1.root(root_val) 143 | elif operator == 'to_celsius': 144 | result = big_num1.to_celsius() 145 | elif operator == 'to_fahrenheit': 146 | result = big_num1.to_fahrenheit() 147 | elif operator == 'memory_store': 148 | big_num1.memory_store() 149 | print("Number stored in memory.") 150 | return 151 | elif operator == 'memory_recall': 152 | result = big_num1.memory_recall() 153 | elif operator == 'memory_clear': 154 | big_num1.memory_clear() 155 | print("Memory cleared.") 156 | return 157 | else: 158 | print("Invalid operator.") 159 | return 160 | 161 | print("Result:", result) 162 | 163 | except ValueError as e: 164 | print("Invalid input:", e) 165 | except Exception as e: 166 | print("An error occurred:", e) 167 | 168 | if __name__ == "__main__": 169 | main() -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
The Arbitrary Precision Four-Function Calculator is a custom implementation capable of handling large integers with arbitrary precision. It allows performing basic arithmetic operations such as addition subtraction multiplication and division on numbers of any size.
4 | 5 | 6 | 7 |