|
| 1 | +# ===----------------------------------------------------------------------=== # |
| 2 | +# Copyright 2025 Yuhao Zhu |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# ===----------------------------------------------------------------------=== # |
| 16 | + |
| 17 | +""" |
| 18 | +Implements basic arithmetic functions for the BigInt type. |
| 19 | +""" |
| 20 | + |
| 21 | +import time |
| 22 | +import testing |
| 23 | + |
| 24 | +from decimojo.bigint.bigint import BigInt |
| 25 | +import decimojo.bigint.comparison |
| 26 | +from decimojo.rounding_mode import RoundingMode |
| 27 | + |
| 28 | + |
| 29 | +fn add(x1: BigInt, x2: BigInt) raises -> BigInt: |
| 30 | + """Returns the sum of two BigInts. |
| 31 | +
|
| 32 | + Args: |
| 33 | + x1: The first BigInt operand. |
| 34 | + x2: The second BigInt operand. |
| 35 | +
|
| 36 | + Returns: |
| 37 | + The sum of the two BigInts. |
| 38 | + """ |
| 39 | + |
| 40 | + # If one of the numbers is zero, return the other number |
| 41 | + if len(x1.words) == 1 and x1.words[0] == 0: |
| 42 | + return x2 |
| 43 | + if len(x2.words) == 1 and x2.words[0] == 0: |
| 44 | + return x1 |
| 45 | + |
| 46 | + # If signs are different, we use `subtract` instead |
| 47 | + if x1.sign != x2.sign: |
| 48 | + return subtract(x1, -x2) |
| 49 | + |
| 50 | + # At this point, both numbers have the same sign |
| 51 | + # The result will have the same sign as the operands |
| 52 | + # The result will have at most one more word than the longer operand |
| 53 | + var result = BigInt( |
| 54 | + empty=True, capacity=max(len(x1.words), len(x2.words)) + 1 |
| 55 | + ) |
| 56 | + result.sign = x1.sign # Result has the same sign as the operands |
| 57 | + |
| 58 | + var carry: UInt32 = 0 |
| 59 | + var ith: Int = 0 |
| 60 | + var sum_of_words: UInt32 = 0 |
| 61 | + |
| 62 | + # Add corresponding words from both numbers |
| 63 | + while ith < len(x1.words) or ith < len(x2.words): |
| 64 | + sum_of_words = carry |
| 65 | + |
| 66 | + # Add x1's word if available |
| 67 | + if ith < len(x1.words): |
| 68 | + sum_of_words += x1.words[ith] |
| 69 | + |
| 70 | + # Add x2's word if available |
| 71 | + if ith < len(x2.words): |
| 72 | + sum_of_words += x2.words[ith] |
| 73 | + |
| 74 | + # Compute new word and carry |
| 75 | + carry = UInt32(sum_of_words // 1_000_000_000) |
| 76 | + result.words.append(UInt32(sum_of_words % 1_000_000_000)) |
| 77 | + |
| 78 | + ith += 1 |
| 79 | + |
| 80 | + # Handle final carry if it exists |
| 81 | + if carry > 0: |
| 82 | + result.words.append(carry) |
| 83 | + |
| 84 | + return result |
| 85 | + |
| 86 | + |
| 87 | +fn subtract(x1: BigInt, x2: BigInt) raises -> BigInt: |
| 88 | + """Returns the difference of two numbers. |
| 89 | +
|
| 90 | + Args: |
| 91 | + x1: The first number (minuend). |
| 92 | + x2: The second number (subtrahend). |
| 93 | +
|
| 94 | + Returns: |
| 95 | + The result of subtracting x2 from x1. |
| 96 | + """ |
| 97 | + # If the subtrahend is zero, return the minuend |
| 98 | + if x2.is_zero(): |
| 99 | + return x1 |
| 100 | + # If the minuend is zero, return the negated subtrahend |
| 101 | + if x1.is_zero(): |
| 102 | + return -x2 |
| 103 | + |
| 104 | + # If signs are different, we use `add` instead |
| 105 | + if x1.sign != x2.sign: |
| 106 | + return add(x1, -x2) |
| 107 | + |
| 108 | + # At this point, both numbers have the same sign |
| 109 | + # We need to determine which number has the larger absolute value |
| 110 | + var comparison_result = decimojo.bigint.comparison.compare_absolute(x1, x2) |
| 111 | + |
| 112 | + if comparison_result == 0: |
| 113 | + # |x1| = |x2| |
| 114 | + return BigInt() # Return zero |
| 115 | + |
| 116 | + # The result will have no more words than the larger operand |
| 117 | + var result = BigInt(empty=True, capacity=max(len(x1.words), len(x2.words))) |
| 118 | + var borrow: Int32 = 0 |
| 119 | + var ith: Int = 0 |
| 120 | + var difference: Int32 = 0 # Int32 is sufficient for the difference |
| 121 | + |
| 122 | + if comparison_result > 0: |
| 123 | + # |x1| > |x2| |
| 124 | + result.sign = x1.sign |
| 125 | + while ith < len(x1.words): |
| 126 | + # Subtract the borrow |
| 127 | + difference = Int32(x1.words[ith]) - borrow |
| 128 | + # Subtract smaller's word if available |
| 129 | + if ith < len(x2.words): |
| 130 | + difference -= Int32(x2.words[ith]) |
| 131 | + # Handle borrowing if needed |
| 132 | + if difference < Int32(0): |
| 133 | + difference += Int32(1_000_000_000) |
| 134 | + borrow = Int32(1) |
| 135 | + else: |
| 136 | + borrow = Int32(0) |
| 137 | + result.words.append(UInt32(difference)) |
| 138 | + ith += 1 |
| 139 | + |
| 140 | + else: |
| 141 | + # |x1| < |x2| |
| 142 | + # Same as above, but we swap x1 and x2 |
| 143 | + result.sign = not x2.sign |
| 144 | + while ith < len(x2.words): |
| 145 | + difference = Int32(x2.words[ith]) - borrow |
| 146 | + if ith < len(x1.words): |
| 147 | + difference -= Int32(x1.words[ith]) |
| 148 | + if difference < Int32(0): |
| 149 | + difference += Int32(1_000_000_000) |
| 150 | + borrow = Int32(1) |
| 151 | + else: |
| 152 | + borrow = Int32(0) |
| 153 | + result.words.append(UInt32(difference)) |
| 154 | + ith += 1 |
| 155 | + |
| 156 | + # Remove trailing zeros |
| 157 | + while len(result.words) > 1 and result.words[len(result.words) - 1] == 0: |
| 158 | + result.words.resize(len(result.words) - 1) |
| 159 | + |
| 160 | + return result |
| 161 | + |
| 162 | + |
| 163 | +fn negative(x: BigInt) -> BigInt: |
| 164 | + """Returns the negative of a BigInt number. |
| 165 | +
|
| 166 | + Args: |
| 167 | + x: The BigInt value to compute the negative of. |
| 168 | +
|
| 169 | + Returns: |
| 170 | + A new BigInt containing the negative of x. |
| 171 | + """ |
| 172 | + var result = x |
| 173 | + result.sign = not result.sign |
| 174 | + return result |
| 175 | + |
| 176 | + |
| 177 | +fn absolute(x: BigInt) -> BigInt: |
| 178 | + """Returns the absolute value of a BigInt number. |
| 179 | +
|
| 180 | + Args: |
| 181 | + x: The BigInt value to compute the absolute value of. |
| 182 | +
|
| 183 | + Returns: |
| 184 | + A new BigInt containing the absolute value of x. |
| 185 | + """ |
| 186 | + if x.sign: |
| 187 | + var result = x |
| 188 | + result.sign = False |
| 189 | + return result |
| 190 | + else: |
| 191 | + return x |
0 commit comments