Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/internal_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
- #94. Implementing pi() with Machin's formula. Time taken for precision 2048: 33.580649 seconds.
- #95. Implementing pi() with Chudnovsky algorithm (binary splitting). Time taken for precision 2048: 1.771954 seconds.
- #97. Implementing Karatsuba multiplication for BigUInt. Time taken for precision 2048: 0.60656999994535 seconds.
- #105. Implementing Burnikel-Ziegler division for BigUInt. Time taken for precision 2048: 0.5454419999732636 seconds.
6 changes: 4 additions & 2 deletions docs/todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ This is a to-do list for Yuhao's personal use.

- [x] (#31) The `exp()` function performs slower than Python's counterpart in specific cases. Detailed investigation reveals the bottleneck stems from multiplication operations between decimals with significant fractional components. These operations currently rely on UInt256 arithmetic, which introduces performance overhead. Optimization of the `multiply()` function is required to address these performance bottlenecks, particularly for high-precision decimal multiplication with many digits after the decimal point.
- [ ] When Mojo supports global variables, implement a global variable for the `BigDecimal` class to store the precision of the decimal number. This will allow users to set the precision globally, rather than having to set it for each function of the `BigDecimal` class.
- [ ] Implement different methods for augmented arithmetic assignments to improve memeory-efficiency and performance.
- [x] Implement different methods for augmented arithmetic assignments to improve memeory-efficiency and performance.
- [ ] Implement different methods for adding decimojo types with `Int` types so that an implicit conversion is not required.
- [ ] Implement a method `remove_trailing_zeros` for `BigUInt`.
- [x] Implement a method `remove_leading_zeros` for `BigUInt`, which removes the zero words from the most significant end of the number.
- [ ] Use debug mode to check for unnecessary zero words before all arithmetic operations. This will help ensure that there are no zero words, which can simplify the speed of checking for zero because we only need to check the first word.
- [ ] Use debug mode to check for uninitialized `BigUInt` before all arithmetic operations. This will help ensure that there are no uninitialized `BigUInt`.
16 changes: 8 additions & 8 deletions pixi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ t = "clear && pixi run package && pixi run mojo test tests -D ASSERT=all --filte
b = "pixi run t big"

# benches
bench_decimal = "clear && pixi run package && cd benches/decimal && pixi run mojo run -I ../ -D ASSERT=all bench.mojo && cd ../.. && pixi run clean"
bench_bigint = "clear && pixi run package && cd benches/bigint && pixi run mojo run -I ../ -D ASSERT=all bench.mojo && cd ../.. && pixi run clean"
bench_biguint = "clear && pixi run package && cd benches/biguint && pixi run mojo run -I ../ -D ASSERT=all bench.mojo && cd ../.. && pixi run clean"
bench_bigdecimal = "clear && pixi run package && cd benches/bigdecimal && pixi run mojo run -I ../ -D ASSERT=all bench.mojo && cd ../.. && pixi run clean"
bench_dec = "pixi run bench_decimal"
bench_bint = "pixi run bench_bigint"
bench_buint = "pixi run bench_biguint"
bench_bdec = "pixi run bench_bigdecimal"
dec = "clear && pixi run package && cd benches/decimal && pixi run mojo run -I ../ bench.mojo && cd ../.. && pixi run clean"
bint = "clear && pixi run package && cd benches/bigint && pixi run mojo run -I ../ bench.mojo && cd ../.. && pixi run clean"
buint = "clear && pixi run package && cd benches/biguint && pixi run mojo run -I ../ bench.mojo && cd ../.. && pixi run clean"
bdec = "clear && pixi run package && cd benches/bigdecimal && pixi run mojo run -I ../ bench.mojo && cd ../.. && pixi run clean"
dec_debug = "clear && pixi run package && cd benches/decimal && pixi run mojo run -I ../ -D ASSERT=all bench.mojo && cd ../.. && pixi run clean"
bint_debug = "clear && pixi run package && cd benches/bigint && pixi run mojo run -I ../ -D ASSERT=all bench.mojo && cd ../.. && pixi run clean"
buint_debug = "clear && pixi run package && cd benches/biguint && pixi run mojo run -I ../ -D ASSERT=all bench.mojo && cd ../.. && pixi run clean"
bdec_debug = "clear && pixi run package && cd benches/bigdecimal && pixi run mojo run -I ../ -D ASSERT=all bench.mojo && cd ../.. && pixi run clean"

# doc
doc = "clear && pixi run mojo doc -o docs/doc.json --diagnose-missing-doc-strings --validate-doc-strings src/decimojo"
2 changes: 1 addition & 1 deletion src/decimojo/biguint/arithmetics.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,7 @@ fn multiply_inplace_by_uint32(mut x: BigUInt, y: UInt32):
"""
# Short circuit cases when y is between 0 and 4
# See `multiply_inplace_by_uint32_le_4()` for details
# TODO: Check the performance of `y <= 4`
# The performance is the best when `y <= 2`
if y <= 2:
multiply_inplace_by_uint32_le_4(x, y)
return
Expand Down
49 changes: 37 additions & 12 deletions src/decimojo/tests.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,47 @@ fn parse_file(file_path: String) raises -> tomlmojo.parser.TOMLDocument:
)


fn load_test_cases(
toml: tomlmojo.parser.TOMLDocument, table_name: String
) raises -> List[TestCase]:
"""Load test cases from a TOMLDocument."""
fn load_test_cases[
unary: Bool = False
](toml: tomlmojo.parser.TOMLDocument, table_name: String) raises -> List[
TestCase
]:
"""Load test cases from a TOMLDocument.

Parameters:
unary: Whether the test cases are unary (single operand) or binary (two operands).

Args:
toml: The TOMLDocument containing the test cases.
table_name: The name of the table in the TOMLDocument to load test cases from.

Returns:
A list of TestCase objects containing the test cases.
"""
# Get array of test cases
var cases_array = toml.get_array_of_tables(table_name)

var test_cases = List[TestCase]()
for case_table in cases_array:
test_cases.append(
TestCase(
case_table["a"].as_string(),
case_table["b"].as_string(),
case_table["expected"].as_string(),
case_table["description"].as_string(),

if unary:
for case_table in cases_array:
test_cases.append(
TestCase(
case_table["a"].as_string(),
"",
case_table["expected"].as_string(),
case_table["description"].as_string(),
)
)
else:
for case_table in cases_array:
test_cases.append(
TestCase(
case_table["a"].as_string(),
case_table["b"].as_string(),
case_table["expected"].as_string(),
case_table["description"].as_string(),
)
)
)

return test_cases
Loading