diff --git a/mojoproject.toml b/mojoproject.toml index a174962..01f99d1 100644 --- a/mojoproject.toml +++ b/mojoproject.toml @@ -10,7 +10,7 @@ readme = "README.md" version = "0.3.0" [dependencies] -max = "==25.2" +max = "==25.3" [tasks] # format the code diff --git a/src/decimojo/bigdecimal/bigdecimal.mojo b/src/decimojo/bigdecimal/bigdecimal.mojo index 86008b0..cf79f40 100644 --- a/src/decimojo/bigdecimal/bigdecimal.mojo +++ b/src/decimojo/bigdecimal/bigdecimal.mojo @@ -620,15 +620,20 @@ struct BigDecimal(Absable, Comparable, IntableRaising, Roundable, Writable): return decimojo.bigdecimal.comparison.min(self, other) @always_inline - fn root(self, root: Self, precision: Int) raises -> Self: + fn root(self, root: Self, precision: Int = 28) raises -> Self: """Returns the root of the BigDecimal number.""" return decimojo.bigdecimal.exponential.root(self, root, precision) @always_inline - fn sqrt(self, precision: Int) raises -> Self: + fn sqrt(self, precision: Int = 28) raises -> Self: """Returns the square root of the BigDecimal number.""" return decimojo.bigdecimal.exponential.sqrt(self, precision) + @always_inline + fn cbrt(self, precision: Int = 28) raises -> Self: + """Returns the cube root of the BigDecimal number.""" + return decimojo.bigdecimal.exponential.cbrt(self, precision) + @always_inline fn true_divide(self, other: Self, precision: Int) raises -> Self: """Returns the result of true division of two BigDecimal numbers. @@ -798,9 +803,11 @@ struct BigDecimal(Absable, Comparable, IntableRaising, Roundable, Writable): else: ndigits = 3 print( - "word {}:{}{}".format( + String("word {}:{}{}") + .format( i, " " * (10 - ndigits), String(self.coefficient.words[i]) - ).rjust(9, fillchar="0") + ) + .rjust(9, fillchar="0") ) print("----------------------------------------------") @@ -866,7 +873,6 @@ struct BigDecimal(Absable, Comparable, IntableRaising, Roundable, Writable): var number_of_words_to_remove = number_of_digits_to_remove // 9 var number_of_remaining_digits_to_remove = number_of_digits_to_remove % 9 - var words: List[UInt32] = List[UInt32]() words = self.coefficient.words[number_of_words_to_remove:] var coefficient = BigUInt(words^) diff --git a/src/decimojo/bigdecimal/exponential.mojo b/src/decimojo/bigdecimal/exponential.mojo index b574cf5..80434d2 100644 --- a/src/decimojo/bigdecimal/exponential.mojo +++ b/src/decimojo/bigdecimal/exponential.mojo @@ -26,6 +26,24 @@ from decimojo.bigdecimal.bigdecimal import BigDecimal from decimojo.rounding_mode import RoundingMode import decimojo.utility +# ===----------------------------------------------------------------------=== # +# List of functions in this module: +# - power(base: BigDecimal, exponent: BigDecimal, precision: Int) -> BigDecimal +# - integer_power(base: BigDecimal, exponent: BigDecimal, precision: Int) -> BigDecimal +# - root(x: BigDecimal, n: BigDecimal, precision: Int) -> BigDecimal +# - integer_root(x: BigDecimal, n: BigDecimal, precision: Int) -> BigDecimal +# - is_integer_reciprocal_and_return(n: BigDecimal) -> Tuple[Bool, BigDecimal] +# - is_odd_reciprocal(n: BigDecimal) -> Bool +# - sqrt(x: BigDecimal, precision: Int) -> BigDecimal +# - exp(x: BigDecimal, precision: Int) -> BigDecimal +# - exp_taylor_series(x: BigDecimal, minimum_precision: Int) -> BigDecimal +# - ln(x: BigDecimal, precision: Int) -> BigDecimal +# - log(x: BigDecimal, precision: Int) -> BigDecimal +# - log10(x: BigDecimal, precision: Int) -> BigDecimal +# - ln_series_expansion(x: BigDecimal, precision: Int) -> BigDecimal +# - compute_ln2(precision: Int) -> BigDecimal +# - compute_ln1d25(precision: Int) -> BigDecimal +# ===----------------------------------------------------------------------=== # # ===----------------------------------------------------------------------=== # # Power and root functions @@ -561,6 +579,28 @@ fn sqrt(x: BigDecimal, precision: Int) raises -> BigDecimal: return guess^ +fn cbrt(x: BigDecimal, precision: Int) raises -> BigDecimal: + """Calculate the cube root of a BigDecimal number. + + Args: + x: The number to calculate the cube root of. + precision: The desired precision (number of significant digits) of the result. + + Returns: + The cube root of x with the specified precision. + + Raises: + Error: If x is negative. + """ + + result = integer_root( + x, + BigDecimal(coefficient=BigUInt(List[UInt32](3)), scale=0, sign=False), + precision, + ) + return result^ + + # ===----------------------------------------------------------------------=== # # Exponential functions # ===----------------------------------------------------------------------=== # diff --git a/src/decimojo/bigint/bigint.mojo b/src/decimojo/bigint/bigint.mojo index 69df52e..15b28af 100644 --- a/src/decimojo/bigint/bigint.mojo +++ b/src/decimojo/bigint/bigint.mojo @@ -261,9 +261,8 @@ struct BigInt(Absable, IntableRaising, Writable): The BigInt representation of the string. """ var coef: List[UInt8] - var scale: Int var sign: Bool - coef, scale, sign = decimojo.str.parse_numeric_string(value) + coef, _scale, sign = decimojo.str.parse_numeric_string(value) # Check if the number is zero if len(coef) == 1 and coef[0] == UInt8(0): @@ -620,8 +619,10 @@ struct BigInt(Absable, IntableRaising, Writable): else: ndigits = 3 print( - "word {}:{}{}".format( + String("word {}:{}{}") + .format( i, " " * (10 - ndigits), String(self.magnitude.words[i]) - ).rjust(9, fillchar="0") + ) + .rjust(9, fillchar="0") ) print("----------------------------------------------") diff --git a/src/decimojo/biguint/arithmetics.mojo b/src/decimojo/biguint/arithmetics.mojo index 8b46e4d..e75925e 100644 --- a/src/decimojo/biguint/arithmetics.mojo +++ b/src/decimojo/biguint/arithmetics.mojo @@ -25,6 +25,34 @@ from decimojo.biguint.biguint import BigUInt import decimojo.biguint.comparison from decimojo.rounding_mode import RoundingMode +# ===----------------------------------------------------------------------=== # +# List of functions in this module: +# add(x1: BigUInt, x2: BigUInt) -> BigUInt +# add_inplace(x1: BigUInt, x2: BigUInt) +# add_inplace_by_1(x: BigUInt) -> None +# subtract(x1: BigUInt, x2: BigUInt) -> BigUInt +# negative(x: BigUInt) -> BigUInt +# absolute(x: BigUInt) -> BigUInt +# multiply(x1: BigUInt, x2: BigUInt) -> BigUInt +# floor_divide(x1: BigUInt, x2: BigUInt) -> BigUInt +# truncate_divide(x1: BigUInt, x2: BigUInt) -> BigUInt +# ceil_divide(x1: BigUInt, x2: BigUInt) -> BigUInt +# floor_modulo(x1: BigUInt, x2: BigUInt) -> BigUInt +# truncate_modulo(x1: BigUInt, x2: BigUInt) -> BigUInt +# ceil_modulo(x1: BigUInt, x2: BigUInt) -> BigUInt +# divmod(x1: BigUInt, x2: BigUInt) -> Tuple[BigUInt, BigUInt] +# scale_up_by_power_of_10(x: BigUInt, n: Int) -> BigUInt +# floor_divide_general(x1: BigUInt, x2: BigUInt) -> BigUInt +# floor_divide_partition(x1: BigUInt, x2: BigUInt) -> BigUInt +# floor_divide_inplace_by_single_word(x1: BigUInt, x2: BigUInt) -> None +# floor_divide_inplace_by_double_words(x1: BigUInt, x2: BigUInt) -> None +# floor_divide_inplace_by_2(x: BigUInt) -> None +# scale_down_by_power_of_10(x: BigUInt, n: Int) -> BigUInt +# estimate_quotient(x1: BigUInt, x2: BigUInt, j: Int, m: Int) -> UInt64 +# shift_words_left(x: BigUInt, j: Int) -> BigUInt +# power_of_10(n: Int) -> BigUInt +# ===----------------------------------------------------------------------=== # + # ===----------------------------------------------------------------------=== # # Arithmetic Operations # add, subtract, negative, absolute, multiply, floor_divide, modulo @@ -75,7 +103,7 @@ fn add(x1: BigUInt, x2: BigUInt) raises -> BigUInt: var carry: UInt32 = 0 var ith: Int = 0 - var sum_of_words: UInt32 = 0 + var sum_of_words: UInt32 # Add corresponding words from both numbers while ith < len(x1.words) or ith < len(x2.words): @@ -102,7 +130,7 @@ fn add(x1: BigUInt, x2: BigUInt) raises -> BigUInt: return BigUInt(words=words^) -fn add_inplace(mut x1: BigUInt, x2: BigUInt) raises: +fn add_inplace(mut x1: BigUInt, x2: BigUInt) raises -> None: """Increments a BigUInt number by another BigUInt number in place. Args: @@ -134,7 +162,7 @@ fn add_inplace(mut x1: BigUInt, x2: BigUInt) raises: var carry: UInt32 = 0 var ith: Int = 0 - var sum_of_words: UInt32 = 0 + var sum_of_words: UInt32 var x1_len = len(x1.words) while ith < x1_len or ith < len(x2.words): @@ -164,7 +192,7 @@ fn add_inplace(mut x1: BigUInt, x2: BigUInt) raises: return -fn add_inplace_by_1(mut x: BigUInt) raises: +fn add_inplace_by_1(mut x: BigUInt) raises -> None: """Increments a BigUInt number by 1.""" var i = 0 while i < len(x.words): @@ -211,7 +239,7 @@ fn subtract(x1: BigUInt, x2: BigUInt) raises -> BigUInt: var words = List[UInt32](capacity=max(len(x1.words), len(x2.words))) var borrow: Int32 = 0 var ith: Int = 0 - var difference: Int32 = 0 # Int32 is sufficient for the difference + var difference: Int32 # Int32 is sufficient for the difference while ith < len(x1.words): # Subtract the borrow @@ -294,7 +322,7 @@ fn multiply(x1: BigUInt, x2: BigUInt) raises -> BigUInt: # x1 = x1[0] + x1[1] * 10^9 # x2 = x2[0] + x2[1] * 10^9 # x1 * x2 = x1[0] * x2[0] + (x1[0] * x2[1] + x1[1] * x2[0]) * 10^9 + x1[1] * x2[1] * 10^18 - var carry: UInt64 = 0 + var carry: UInt64 for i in range(len(x1.words)): # Skip if the word is zero if x1.words[i] == 0: @@ -769,7 +797,7 @@ fn floor_divide_partition(x1: BigUInt, x2: BigUInt) raises -> BigUInt: var number_of_words_remainder = len(x1.words) % len(x2.words) var number_of_words_dividend: Int var result = x1 - result.words.resize(len(x1.words) - number_of_words_remainder) + result.words.resize(len(x1.words) - number_of_words_remainder, UInt32(0)) var remainder = BigUInt(List[UInt32](capacity=len(x2.words))) for i in range(len(x1.words) - number_of_words_remainder, len(x1.words)): remainder.words.append(x1.words[i]) @@ -804,7 +832,9 @@ fn floor_divide_partition(x1: BigUInt, x2: BigUInt) raises -> BigUInt: return result^ -fn floor_divide_inplace_by_single_word(mut x1: BigUInt, x2: BigUInt) raises: +fn floor_divide_inplace_by_single_word( + mut x1: BigUInt, x2: BigUInt +) raises -> None: """Divides a BigUInt by a single word divisor in-place. Args: @@ -826,7 +856,9 @@ fn floor_divide_inplace_by_single_word(mut x1: BigUInt, x2: BigUInt) raises: x1.remove_leading_empty_words() -fn floor_divide_inplace_by_double_words(mut x1: BigUInt, x2: BigUInt) raises: +fn floor_divide_inplace_by_double_words( + mut x1: BigUInt, x2: BigUInt +) raises -> None: """Divides a BigUInt by double-word divisor in-place. Args: @@ -846,7 +878,7 @@ fn floor_divide_inplace_by_double_words(mut x1: BigUInt, x2: BigUInt) raises: var carry = UInt128(0) if len(x1.words) % 2 == 1: carry = UInt128(x1.words[-1]) - x1.words.resize(len(x1.words) - 1) + x1.words.resize(len(x1.words) - 1, UInt32(0)) for i in range(len(x1.words) - 1, -1, -2): var dividend = carry * UInt128(1_000_000_000_000_000_000) + UInt128( @@ -861,7 +893,7 @@ fn floor_divide_inplace_by_double_words(mut x1: BigUInt, x2: BigUInt) raises: return -fn floor_divide_inplace_by_2(mut x: BigUInt): +fn floor_divide_inplace_by_2(mut x: BigUInt) -> None: """Divides a BigUInt by 2 in-place. Args: @@ -880,7 +912,7 @@ fn floor_divide_inplace_by_2(mut x: BigUInt): # Remove leading zeros while len(x.words) > 1 and x.words[len(x.words) - 1] == 0: - x.words.resize(len(x.words) - 1) + x.words.resize(len(x.words) - 1, UInt32(0)) fn scale_down_by_power_of_10(x: BigUInt, n: Int) raises -> BigUInt: diff --git a/src/decimojo/biguint/biguint.mojo b/src/decimojo/biguint/biguint.mojo index cc050a1..747365f 100644 --- a/src/decimojo/biguint/biguint.mojo +++ b/src/decimojo/biguint/biguint.mojo @@ -327,7 +327,7 @@ struct BigUInt(Absable, IntableRaising, Writable): raise Error( "Error in `from_string`: The number is not an integer." ) - coef.resize(len(coef) - scale) + coef.resize(len(coef) - scale, UInt8(0)) scale = 0 var number_of_digits = len(coef) - scale @@ -822,9 +822,9 @@ struct BigUInt(Absable, IntableRaising, Writable): else: ndigits = 3 print( - "word {}:{}{}".format( - i, " " * (10 - ndigits), String(self.words[i]) - ).rjust(9, fillchar="0") + String("word {}:{}{}") + .format(i, " " * (10 - ndigits), String(self.words[i])) + .rjust(9, fillchar="0") ) print("----------------------------------------------") @@ -893,10 +893,9 @@ struct BigUInt(Absable, IntableRaising, Writable): if word_index >= len(self.words): return 0 var word = self.words[word_index] - var digit: UInt32 = 0 for _ in range(digit_index): word = word // 10 - digit = word % 10 + var digit = word % 10 return UInt8(digit) @always_inline @@ -941,7 +940,7 @@ struct BigUInt(Absable, IntableRaising, Writable): fn remove_leading_empty_words(mut self): """Removes leading words of 0 from BigUInt's internal representation.""" while len(self.words) > 1 and self.words[-1] == 0: - self.words.resize(len(self.words) - 1) + self.words.resize(len(self.words) - 1, UInt32(0)) @always_inline fn remove_trailing_digits_with_rounding( diff --git a/src/decimojo/decimal/arithmetics.mojo b/src/decimojo/decimal/arithmetics.mojo index b025932..c9f44e9 100644 --- a/src/decimojo/decimal/arithmetics.mojo +++ b/src/decimojo/decimal/arithmetics.mojo @@ -404,7 +404,6 @@ fn multiply(x1: Decimal, x2: Decimal) raises -> Decimal: var x1_scale = x1.scale() var x2_scale = x2.scale() var combined_scale = x1_scale + x2_scale - """Combined scale of the two operands.""" var is_negative = x1.is_negative() != x2.is_negative() # SPECIAL CASE: true one @@ -510,8 +509,10 @@ fn multiply(x1: Decimal, x2: Decimal) raises -> Decimal: var prod: UInt128 = UInt128(x1_coef) * UInt128(x2_coef) if prod > Decimal.MAX_AS_UINT128: raise Error( - "Error in `multiply()`: The product is {}, which exceeds" - " the capacity of Decimal (2^96-1)".format(prod) + String( + "Error in `multiply()`: The product is {}, which" + " exceeds the capacity of Decimal (2^96-1)" + ).format(prod) ) else: return Decimal.from_uint128(prod, 0, is_negative) @@ -520,8 +521,10 @@ fn multiply(x1: Decimal, x2: Decimal) raises -> Decimal: else: var prod: UInt256 = UInt256(x1_coef) * UInt256(x2_coef) raise Error( - "Error in `multiply()`: The product is {}, which exceeds the" - " capacity of Decimal (2^96-1)".format(prod) + String( + "Error in `multiply()`: The product is {}, which exceeds" + " the capacity of Decimal (2^96-1)" + ).format(prod) ) # SPECIAL CASE: Both operands are integers but with scales @@ -963,13 +966,9 @@ fn divide(x1: Decimal, x2: Decimal) raises -> Decimal: # 但我們只能算到 1.0000000000000000000000000000_5, # 在銀行家捨去法中,我們將捨去項爲5時,向上捨去, 保留28位後爲1.0000000000000000000000000000 # 這樣的捨去法是不準確的,所以我們一律在到達餘數非零且捨去項爲5時,向上捨去 - var is_exact_division: Bool = False - if rem == 0: - is_exact_division = True - else: - if digit == 5: - # Not exact division, round up the last digit - quot += 1 + if (digit == 5) and (rem != 0): + # Not exact division, round up the last digit + quot += 1 var scale_of_quot = step_counter + diff_scale + adjusted_scale @@ -980,6 +979,12 @@ fn divide(x1: Decimal, x2: Decimal) raises -> Decimal: var ndigits_quot = decimojo.utility.number_of_digits(quot) var ndigits_quot_int_part = ndigits_quot - scale_of_quot + print( + String( + "quot: {}, rem: {}, step_counter: {}, scale_of_quot: {}" + ).format(quot, rem, step_counter, scale_of_quot) + ) + # TODO: 可以考慮先降 scale 再判斷是否超出最大值. # TODO: 爲降 scale 引入 round_to_remove_last_n_digits 函數 # If quot is within MAX, return the result @@ -1062,9 +1067,6 @@ fn divide(x1: Decimal, x2: Decimal) raises -> Decimal: step_counter += 1 # Check if division is exact - var is_exact_division: Bool = False - if rem256 == 0: - is_exact_division = True else: if digit == 5: # Not exact division, round up the last digit diff --git a/src/decimojo/decimal/decimal.mojo b/src/decimojo/decimal/decimal.mojo index d33f26b..7178e42 100644 --- a/src/decimojo/decimal/decimal.mojo +++ b/src/decimojo/decimal/decimal.mojo @@ -485,9 +485,7 @@ struct Decimal( ).format(scale) ) - var result = UnsafePointer[UInt128].address_of(value).bitcast[ - Decimal - ]()[] + var result = UnsafePointer(to=value).bitcast[Decimal]()[] result.flags |= (scale << Self.SCALE_SHIFT) & Self.SCALE_MASK result.flags |= sign << 31 @@ -530,9 +528,9 @@ struct Decimal( if value_bytes_len != value_string_slice.char_length(): raise Error( - "There are invalid characters in decimal string: {}".format( - value - ) + String( + "There are invalid characters in decimal string: {}" + ).format(value) ) # Yuhao's notes: @@ -543,7 +541,7 @@ struct Decimal( var decimal_point_read = False var exponent_notation_read = False var exponent_sign_read = False - var exponent_start = False + # var exponent_start = False var unexpected_end_char = False var mantissa_sign: Bool = False # True if negative @@ -612,7 +610,7 @@ struct Decimal( # Exponent part if exponent_notation_read: exponent_sign_read = True - exponent_start = True + # exponent_start = True raw_exponent = raw_exponent * 10 # Mantissa part @@ -642,7 +640,7 @@ struct Decimal( raw_exponent > Decimal.MAX_NUM_DIGITS * 2 ): raise Error( - "Exponent part is too large: {}".format( + String("Exponent part is too large: {}").format( raw_exponent ) ) @@ -654,7 +652,7 @@ struct Decimal( continue else: - exponent_start = True + # exponent_start = True raw_exponent = raw_exponent * 10 + UInt32(code[] - 48) # Mantissa part @@ -674,7 +672,7 @@ struct Decimal( else: raise Error( - "Invalid character in decimal string: {}".format( + String("Invalid character in decimal string: {}").format( chr(Int(code[])) ) ) @@ -785,7 +783,7 @@ struct Decimal( return Decimal.ZERO() # Get the positive value of the input - var abs_value: Float64 = value + var abs_value: Float64 var is_negative: Bool = value < 0 if is_negative: abs_value = -value @@ -802,9 +800,7 @@ struct Decimal( ) # Extract binary exponent using IEEE 754 bit manipulation - var bits: UInt64 = UnsafePointer[Float64].address_of(abs_value).bitcast[ - UInt64 - ]().load() + var bits: UInt64 = UnsafePointer(to=abs_value).bitcast[UInt64]().load() var biased_exponent: Int = Int((bits >> 52) & 0x7FF) # print("DEBUG: biased_exponent = ", biased_exponent) diff --git a/src/decimojo/decimal/special.mojo b/src/decimojo/decimal/special.mojo index e9386d3..61a0bdf 100644 --- a/src/decimojo/decimal/special.mojo +++ b/src/decimojo/decimal/special.mojo @@ -40,7 +40,9 @@ fn factorial(n: Int) raises -> Decimal: raise Error("Factorial is not defined for negative numbers") if n > 27: - raise Error("{}! is too large to be represented by Decimal".format(n)) + raise Error( + String("{}! is too large to be represented by Decimal").format(n) + ) # Directly return the factorial for n = 0 to 27 if n == 0 or n == 1: diff --git a/src/decimojo/prelude.mojo b/src/decimojo/prelude.mojo index b093b4d..fda9518 100644 --- a/src/decimojo/prelude.mojo +++ b/src/decimojo/prelude.mojo @@ -20,12 +20,6 @@ The list contains the functions or types that are the most essential for a user. You can use the following code to import them: -```mojo -from decimojo.prelude import dm, Decimal, RoundingMode -``` - -Or - ```mojo from decimojo.prelude import * ``` @@ -33,4 +27,7 @@ from decimojo.prelude import * import decimojo as dm from decimojo.decimal.decimal import Decimal +from decimojo.bigdecimal.bigdecimal import BigDecimal +from decimojo.biguint.biguint import BigUInt +from decimojo.bigint.bigint import BigInt from decimojo.rounding_mode import RoundingMode diff --git a/src/decimojo/str.mojo b/src/decimojo/str.mojo index c42c139..8064f26 100644 --- a/src/decimojo/str.mojo +++ b/src/decimojo/str.mojo @@ -69,8 +69,9 @@ fn parse_numeric_string( if value_bytes_len != value_string_slice.char_length(): raise Error( - "There are invalid characters in the string of the number: {}" - .format(value) + String( + "There are invalid characters in the string of the number: {}" + ).format(value) ) # Yuhao's notes: @@ -81,7 +82,7 @@ fn parse_numeric_string( var decimal_point_read = False var exponent_notation_read = False var exponent_sign_read = False - var exponent_start = False + # var exponent_start = False var unexpected_end_char = False var mantissa_sign: Bool = False # True if negative @@ -147,7 +148,7 @@ fn parse_numeric_string( # Exponent part if exponent_notation_read: exponent_sign_read = True - exponent_start = True + # exponent_start = True raw_exponent = raw_exponent * 10 # Mantissa part @@ -167,7 +168,7 @@ fn parse_numeric_string( # Exponent part if exponent_notation_read: - exponent_start = True + # exponent_start = True raw_exponent = raw_exponent * 10 + Int(code - 48) # Mantissa part @@ -180,9 +181,9 @@ fn parse_numeric_string( else: raise Error( - "Invalid character in the string of the number: {}".format( - chr(Int(code)) - ) + String( + "Invalid character in the string of the number: {}" + ).format(chr(Int(code))) ) if unexpected_end_char: diff --git a/src/decimojo/utility.mojo b/src/decimojo/utility.mojo index 07020c7..2946394 100644 --- a/src/decimojo/utility.mojo +++ b/src/decimojo/utility.mojo @@ -54,9 +54,7 @@ fn bitcast[dtype: DType](dec: Decimal) -> Scalar[dtype]: ]() # Bitcast the Decimal to the desired Mojo scalar type - var result = UnsafePointer[Decimal].address_of(dec).bitcast[ - Scalar[dtype] - ]().load() + var result = UnsafePointer(to=dec).bitcast[Scalar[dtype]]().load() # Mask out the bits in flags result &= Scalar[dtype](0xFFFFFFFF_FFFFFFFF_FFFFFFFF) return result diff --git a/src/tomlmojo/tokenizer.mojo b/src/tomlmojo/tokenizer.mojo index b86d454..b043cd5 100644 --- a/src/tomlmojo/tokenizer.mojo +++ b/src/tomlmojo/tokenizer.mojo @@ -219,7 +219,6 @@ struct Tokenizer: start_line = self.position.line start_column = self.position.column quote_char = self.current_char - result = String("") # Skip opening quote self._advance() diff --git a/tests/bigdecimal/test_bigdecimal_divide.mojo b/tests/bigdecimal/test_bigdecimal_divide.mojo index 5a13c12..e2775ea 100644 --- a/tests/bigdecimal/test_bigdecimal_divide.mojo +++ b/tests/bigdecimal/test_bigdecimal_divide.mojo @@ -106,7 +106,7 @@ fn test_division_by_zero() raises: var a = BigDecimal("1") var b = BigDecimal("0") - var exception_caught = False + var exception_caught: Bool try: _ = a / b exception_caught = False diff --git a/tests/bigdecimal/test_bigdecimal_exponential.mojo b/tests/bigdecimal/test_bigdecimal_exponential.mojo index 07966cf..3cea59b 100644 --- a/tests/bigdecimal/test_bigdecimal_exponential.mojo +++ b/tests/bigdecimal/test_bigdecimal_exponential.mojo @@ -119,7 +119,7 @@ fn test_negative_sqrt() raises: var negative_number = BigDecimal("-1") - var exception_caught = False + var exception_caught: Bool try: _ = negative_number.sqrt(precision=28) exception_caught = False @@ -194,7 +194,7 @@ fn test_ln_invalid_inputs() raises: # Test 1: ln of zero should raise an error var zero = BigDecimal("0") - var exception_caught = False + var exception_caught: Bool try: _ = zero.ln() exception_caught = False @@ -205,7 +205,6 @@ fn test_ln_invalid_inputs() raises: # Test 2: ln of negative number should raise an error var negative = BigDecimal("-1") - exception_caught = False try: _ = negative.ln() exception_caught = False @@ -285,7 +284,7 @@ fn test_root_invalid_inputs() raises: # Test 1: 0th root should raise an error var a1 = BigDecimal("16") var n1 = BigDecimal("0") - var exception_caught = False + var exception_caught: Bool try: _ = a1.root(n1, precision=28) exception_caught = False @@ -297,7 +296,6 @@ fn test_root_invalid_inputs() raises: # Test 2: Even root of negative number should raise an error var a2 = BigDecimal("-16") var n2 = BigDecimal("2") - exception_caught = False try: _ = a2.root(n2, precision=28) exception_caught = False @@ -311,7 +309,6 @@ fn test_root_invalid_inputs() raises: # Test 3: Fractional root with even denominator of negative number should raise an error var a3 = BigDecimal("-16") var n3 = BigDecimal("2.5") # 5/2, denominator is even - exception_caught = False try: _ = a3.root(n3, precision=28) exception_caught = False @@ -399,7 +396,7 @@ fn test_power_invalid_inputs() raises: # Test 1: 0^0 should raise an error (undefined) var base1 = BigDecimal("0") var exp1 = BigDecimal("0") - var exception_caught = False + var exception_caught: Bool try: _ = base1.power(exp1, precision=28) exception_caught = False @@ -411,7 +408,6 @@ fn test_power_invalid_inputs() raises: # Test 2: 0^-1 should raise an error (division by zero) var base2 = BigDecimal("0") var exp2 = BigDecimal("-1") - exception_caught = False try: _ = base2.power(exp2, precision=28) exception_caught = False @@ -425,7 +421,6 @@ fn test_power_invalid_inputs() raises: # Test 3: Negative number raised to a fractional power should raise an error var base3 = BigDecimal("-2") var exp3 = BigDecimal("0.5") - exception_caught = False try: _ = base3.power(exp3, precision=28) exception_caught = False diff --git a/tests/bigint/test_bigint_floor_divide.mojo b/tests/bigint/test_bigint_floor_divide.mojo index 5666375..abbcb75 100644 --- a/tests/bigint/test_bigint_floor_divide.mojo +++ b/tests/bigint/test_bigint_floor_divide.mojo @@ -335,7 +335,7 @@ fn test_large_number_division() raises: String(py_result1), "Large positive number division gave incorrect result", ) - print("passed: {} // {} = {}".format(a1, b1, result1)) + print(String("passed: {} // {} = {}").format(a1, b1, result1)) # Test case 2: Large negative number divided by small number var a2 = BigInt("-" + "1" + "0" * 50) # -10^50 @@ -347,7 +347,7 @@ fn test_large_number_division() raises: String(py_result2), "Large negative number division gave incorrect result", ) - print("passed: {} // {} = {}".format(a2, b2, result2)) + print(String("passed: {} // {} = {}").format(a2, b2, result2)) # Test case 3: Large positive number divided by small negative number var a3 = BigInt("1" + "0" * 50) # 10^50 @@ -359,7 +359,7 @@ fn test_large_number_division() raises: String(py_result3), "Large positive // small negative gave incorrect result", ) - print("passed: {} // {} = {}".format(a3, b3, result3)) + print(String("passed: {} // {} = {}").format(a3, b3, result3)) # Test case 4: Large negative number divided by small negative number var a4 = BigInt("-" + "1" + "0" * 50) # -10^50 @@ -371,7 +371,7 @@ fn test_large_number_division() raises: String(py_result4), "Large negative // small negative gave incorrect result", ) - print("passed: {} // {} = {}".format(a4, b4, result4)) + print(String("passed: {} // {} = {}").format(a4, b4, result4)) # Test case 5: Large number divided by large number (same sign) var a5 = BigInt("9" * 30) # 30 nines @@ -383,7 +383,7 @@ fn test_large_number_division() raises: String(py_result5), "Large // large (same sign) gave incorrect result", ) - print("passed: {} // {} = {}".format(a5, b5, result5)) + print(String("passed: {} // {} = {}").format(a5, b5, result5)) # Test case 6: Large number divided by large number (opposite sign) var a6 = BigInt("9" * 30) # 30 nines @@ -395,7 +395,7 @@ fn test_large_number_division() raises: String(py_result6), "Large // large (opposite sign) gave incorrect result", ) - print("passed: {} // {} = {}".format(a6, b6, result6)) + print(String("passed: {} // {} = {}").format(a6, b6, result6)) # Test case 7: Very large number divisible by power of 10 (positive) var a7 = BigInt("1" + "0" * 100) # 10^100 @@ -407,7 +407,7 @@ fn test_large_number_division() raises: String(py_result7), "Power of 10 division gave incorrect result", ) - print("passed: {} // {} = {}".format(a7, b7, result7)) + print(String("passed: {} // {} = {}").format(a7, b7, result7)) # Test case 8: Very large number divisible by power of 10 (negative dividend) var a8 = BigInt("-" + "1" + "0" * 100) # -10^100 @@ -419,7 +419,7 @@ fn test_large_number_division() raises: String(py_result8), "Negative power of 10 division gave incorrect result", ) - print("passed: {} // {} = {}".format(a8, b8, result8)) + print(String("passed: {} // {} = {}").format(a8, b8, result8)) # Test case 9: Very large complex numbers stra = "123456789" * 50 @@ -433,7 +433,7 @@ fn test_large_number_division() raises: String(py_result9), "Complex large number division incorrect", ) - print("passed: {} // {} = {}".format(a9, b9, result9)) + print(String("passed: {} // {} = {}").format(a9, b9, result9)) # Test case 10: Very large negative complex numbers var a10 = BigInt("-" + stra) @@ -445,7 +445,7 @@ fn test_large_number_division() raises: String(py_result10), "Complex large negative number division incorrect", ) - print("passed: {} // {} = {}".format(a10, b10, result10)) + print(String("passed: {} // {} = {}").format(a10, b10, result10)) print("✓ Large number division tests passed!") diff --git a/tests/bigint/test_bigint_truncate_divide.mojo b/tests/bigint/test_bigint_truncate_divide.mojo index 7a05b9f..2920c09 100644 --- a/tests/bigint/test_bigint_truncate_divide.mojo +++ b/tests/bigint/test_bigint_truncate_divide.mojo @@ -232,7 +232,7 @@ fn test_large_number_division() raises: "14285714285714285714285714285714285714285714285714", "10^50 / 7 gave incorrect result", ) - print("passed: {} / {} = {}".format(a1, b1, result1)) + print(String("passed: {} / {} = {}").format(a1, b1, result1)) # Test case 2: Large negative number divided by small number var a2 = BigInt("-" + "1" + "0" * 50) # -10^50 @@ -243,7 +243,7 @@ fn test_large_number_division() raises: "-14285714285714285714285714285714285714285714285714", "-10^50 / 7 gave incorrect result", ) - print("passed: {} / {} = {}".format(a2, b2, result2)) + print(String("passed: {} / {} = {}").format(a2, b2, result2)) # Test case 3: Large positive number divided by small negative number var a3 = BigInt("1" + "0" * 50) # 10^50 @@ -254,7 +254,7 @@ fn test_large_number_division() raises: "-14285714285714285714285714285714285714285714285714", "10^50 / -7 gave incorrect result", ) - print("passed: {} / {} = {}".format(a3, b3, result3)) + print(String("passed: {} / {} = {}").format(a3, b3, result3)) # Test case 4: Large negative number divided by small negative number var a4 = BigInt("-" + "1" + "0" * 50) # -10^50 @@ -265,7 +265,7 @@ fn test_large_number_division() raises: "14285714285714285714285714285714285714285714285714", "-10^50 / -7 gave incorrect result", ) - print("passed: {} / {} = {}".format(a4, b4, result4)) + print(String("passed: {} / {} = {}").format(a4, b4, result4)) # Test case 5: Large number divided by large number (same sign) var a5 = BigInt("9" * 30) # 30 nines @@ -276,7 +276,7 @@ fn test_large_number_division() raises: "1000000000000001", "large / large (same sign) gave incorrect result", ) - print("passed: {} / {} = {}".format(a5, b5, result5)) + print(String("passed: {} / {} = {}").format(a5, b5, result5)) # Test case 6: Large number divided by large number (opposite sign) var a6 = BigInt("9" * 30) # 30 nines @@ -287,7 +287,7 @@ fn test_large_number_division() raises: "-1000000000000001", "large / large (opposite sign) gave incorrect result", ) - print("passed: {} / {} = {}".format(a6, b6, result6)) + print(String("passed: {} / {} = {}").format(a6, b6, result6)) print("✓ Large number division tests passed!") diff --git a/tests/decimal/test_ln.mojo b/tests/decimal/test_ln.mojo index 5924715..3ef16cc 100644 --- a/tests/decimal/test_ln.mojo +++ b/tests/decimal/test_ln.mojo @@ -149,8 +149,10 @@ fn test_edge_cases() raises: var result_small = ln(very_small) testing.assert_true( String(result_small).startswith("-62.16979751083923346848576927"), - "ln of a very small number should be -62.16979751083923346848576927...," - " but got {}".format(result_small), + String( + "ln of a very small number should be" + " -62.16979751083923346848576927..., but got {}" + ).format(result_small), ) # Test case 14: ln of a very large number @@ -158,9 +160,9 @@ fn test_edge_cases() raises: var result_large = ln(very_large) testing.assert_true( String(result_large).startswith("64.4723"), - "ln of a very large number should be 64.4723..., but got {}".format( - result_large - ), + String( + "ln of a very large number should be 64.4723..., but got {}" + ).format(result_large), ) print("✓ Edge cases tests passed!") @@ -183,8 +185,10 @@ fn test_precision() raises: var result_ten = ln(ten) testing.assert_true( String(result_ten).startswith("2.30258509299404568401"), - "ln(10) with high precision should be 2.30258509299404568401..., but" - " got {}".format(result_ten), + String( + "ln(10) with high precision should be 2.30258509299404568401...," + " but got {}" + ).format(result_ten), ) print("✓ Precision tests passed!")