Skip to content

Commit 8389aa3

Browse files
authored
[decimal] Implement true_divide for BigDecimal type + fix a bug in BigUInt.multiply (#70)
This pull request includes significant changes to the BigInt and BigDecimal arithmetic operations, introducing new functionality and improving existing methods. The most important changes include adding true division functionality for BigDecimal, modifying scaling methods, and updating the handling of leading zeros in BigUInt operations. Notably, this PR fixes a bug in `BigUInt.multiply` where the carry is mistakenly skipped if a word of x2 is zero. ### BigDecimal Enhancements: * Added `true_divide` function to perform division with specified precision and rounding rules in `src/decimojo/bigdecimal/arithmetics.mojo`. * Updated the benchmarks to include the new division operation in `benches/bigdecimal/bench.mojo`. [[1]](diffhunk://#diff-96175f1000f81fe074f70a9adc4b39cd3f2dac8e85d30aa7efed9fc02b46e5ecR4) [[2]](diffhunk://#diff-96175f1000f81fe074f70a9adc4b39cd3f2dac8e85d30aa7efed9fc02b46e5ecR16) [[3]](diffhunk://#diff-96175f1000f81fe074f70a9adc4b39cd3f2dac8e85d30aa7efed9fc02b46e5ecR29-R35) ### BigUInt Improvements: * Replaced `remove_trailing_zeros` with `remove_leading_empty_words` to handle leading zeros more effectively in multiple functions across `src/decimojo/biguint/arithmetics.mojo`. [[1]](diffhunk://#diff-95a5c66e5957fff2a2a8a2710ffb11a4ad7d0bddaf3bc11d075d62ddaa8b915cL180-R180) [[2]](diffhunk://#diff-95a5c66e5957fff2a2a8a2710ffb11a4ad7d0bddaf3bc11d075d62ddaa8b915cL275-R271) [[3]](diffhunk://#diff-95a5c66e5957fff2a2a8a2710ffb11a4ad7d0bddaf3bc11d075d62ddaa8b915cL383-R373) [[4]](diffhunk://#diff-95a5c66e5957fff2a2a8a2710ffb11a4ad7d0bddaf3bc11d075d62ddaa8b915cL964-R827) [[5]](diffhunk://#diff-95a5c66e5957fff2a2a8a2710ffb11a4ad7d0bddaf3bc11d075d62ddaa8b915cL1032-R895) * Simplified the `divmod` function to use `floor_divide` and `subtract` for quotient and remainder calculation in `src/decimojo/biguint/arithmetics.mojo`. ### Codebase Simplification: * Renamed `multiply_by_power_of_10` to `scale_up_by_power_of_10` for clarity and consistency in `src/decimojo/biguint/arithmetics.mojo`. * Removed unnecessary cases and optimized the `floor_divide` function in `src/decimojo/biguint/arithmetics.mojo`. [[1]](diffhunk://#diff-95a5c66e5957fff2a2a8a2710ffb11a4ad7d0bddaf3bc11d075d62ddaa8b915cL338-L343) [[2]](diffhunk://#diff-95a5c66e5957fff2a2a8a2710ffb11a4ad7d0bddaf3bc11d075d62ddaa8b915cL358-R348) These changes enhance the arithmetic capabilities of BigDecimal and BigUInt, improve code clarity, and ensure more efficient handling of numerical operations.
1 parent e5cc073 commit 8389aa3

File tree

8 files changed

+1495
-346
lines changed

8 files changed

+1495
-346
lines changed

benches/bigdecimal/bench.mojo

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from bench_bigdecimal_add import main as bench_add
22
from bench_bigdecimal_subtract import main as bench_sub
33
from bench_bigdecimal_multiply import main as bench_multiply
4+
from bench_bigdecimal_divide import main as bench_divide
45

56

67
fn main() raises:
@@ -12,6 +13,7 @@ This is the BigInt Benchmarks
1213
add: Add
1314
sub: Subtract
1415
mul: Multiply
16+
div: Divide (true divide)
1517
all: Run all benchmarks
1618
q: Exit
1719
=========================================
@@ -24,10 +26,13 @@ q: Exit
2426
bench_sub()
2527
elif command == "mul":
2628
bench_multiply()
29+
elif command == "div":
30+
bench_divide()
2731
elif command == "all":
2832
bench_add()
2933
bench_sub()
3034
bench_multiply()
35+
bench_divide()
3136
elif command == "q":
3237
return
3338
else:

0 commit comments

Comments
 (0)