Skip to content

Conversation

@forfudan
Copy link
Owner

@forfudan forfudan commented Jul 6, 2025

This pull request focuses on optimizing arithmetic operations for BigUInt in the decimojo library. The changes primarily involve replacing hardcoded constants (e.g., 1_000_000_000) with the BigUInt.BASE and BigUInt.BASE_MAX properties, introducing in-place scaling methods, and improving normalization logic for division operations. These updates enhance code maintainability, readability, and performance.

Division improvements:

  • Enhanced normalization logic in floor_divide by introducing a table-based lookup for determining the number of digits to shift, improving quotient estimation accuracy. ([src/decimojo/biguint/arithmetics.mojoL931-R1037](https://github.com/forfudan/decimojo/pull/100/files#diff-95a5c66e5957fff2a2a8a2710ffb11a4ad7d0bddaf3bc11d075d62ddaa8b915cL931-R1037))
  • Although no correction is expected to occur, still kept the correction attempts counter in floor_divide_general to prevent infinite loops during quotient estimation. ([src/decimojo/biguint/arithmetics.mojoL991-R1098](https://github.com/forfudan/decimojo/pull/100/files#diff-95a5c66e5957fff2a2a8a2710ffb11a4ad7d0bddaf3bc11d075d62ddaa8b915cL991-R1098))

Updates to scaling methods:

  • Replaced scale_up_by_power_of_10 and scale_up_by_power_of_billion with their in-place equivalents (scale_up_inplace_by_power_of_10 and scale_up_inplace_by_power_of_billion) across multiple functions, reducing redundant object creation. ([[1]](https://github.com/forfudan/decimojo/pull/100/files#diff-f79534f4e7fdd891932ce9d015c50bd3c8a72c4a1689f0cb55524490ffc0458dL281-R281), [[2]](https://github.com/forfudan/decimojo/pull/100/files#diff-f79534f4e7fdd891932ce9d015c50bd3c8a72c4a1689f0cb55524490ffc0458dL385-R385), [[3]](https://github.com/forfudan/decimojo/pull/100/files#diff-95a5c66e5957fff2a2a8a2710ffb11a4ad7d0bddaf3bc11d075d62ddaa8b915cL621-R621), [[4]](https://github.com/forfudan/decimojo/pull/100/files#diff-95a5c66e5957fff2a2a8a2710ffb11a4ad7d0bddaf3bc11d075d62ddaa8b915cL640-R640), [[5]](https://github.com/forfudan/decimojo/pull/100/files#diff-95a5c66e5957fff2a2a8a2710ffb11a4ad7d0bddaf3bc11d075d62ddaa8b915cL700-R701), [[6]](https://github.com/forfudan/decimojo/pull/100/files#diff-95a5c66e5957fff2a2a8a2710ffb11a4ad7d0bddaf3bc11d075d62ddaa8b915cL807-R880), [[7]](https://github.com/forfudan/decimojo/pull/100/files#diff-95a5c66e5957fff2a2a8a2710ffb11a4ad7d0bddaf3bc11d075d62ddaa8b915cL991-R1098))

Transition to BigUInt.BASE and BigUInt.BASE_MAX:

  • Replaced hardcoded constants 1_000_000_000 and 999_999_999 with BigUInt.BASE and BigUInt.BASE_MAX in arithmetic operations (e.g., addition, subtraction, multiplication, division) to improve flexibility and reduce magic numbers. ([[1]](https://github.com/forfudan/decimojo/pull/100/files#diff-95a5c66e5957fff2a2a8a2710ffb11a4ad7d0bddaf3bc11d075d62ddaa8b915cL204-R205), [[2]](https://github.com/forfudan/decimojo/pull/100/files#diff-95a5c66e5957fff2a2a8a2710ffb11a4ad7d0bddaf3bc11d075d62ddaa8b915cL231-R236), [[3]](https://github.com/forfudan/decimojo/pull/100/files#diff-95a5c66e5957fff2a2a8a2710ffb11a4ad7d0bddaf3bc11d075d62ddaa8b915cL260-R261), [[4]](https://github.com/forfudan/decimojo/pull/100/files#diff-95a5c66e5957fff2a2a8a2710ffb11a4ad7d0bddaf3bc11d075d62ddaa8b915cL274-R274), [[5]](https://github.com/forfudan/decimojo/pull/100/files#diff-95a5c66e5957fff2a2a8a2710ffb11a4ad7d0bddaf3bc11d075d62ddaa8b915cL330-R330), [[6]](https://github.com/forfudan/decimojo/pull/100/files#diff-95a5c66e5957fff2a2a8a2710ffb11a4ad7d0bddaf3bc11d075d62ddaa8b915cL372-R372), [[7]](https://github.com/forfudan/decimojo/pull/100/files#diff-95a5c66e5957fff2a2a8a2710ffb11a4ad7d0bddaf3bc11d075d62ddaa8b915cL531-R532), [[8]](https://github.com/forfudan/decimojo/pull/100/files#diff-95a5c66e5957fff2a2a8a2710ffb11a4ad7d0bddaf3bc11d075d62ddaa8b915cL728-L735), [[9]](https://github.com/forfudan/decimojo/pull/100/files#diff-95a5c66e5957fff2a2a8a2710ffb11a4ad7d0bddaf3bc11d075d62ddaa8b915cL782-R863), [[10]](https://github.com/forfudan/decimojo/pull/100/files#diff-95a5c66e5957fff2a2a8a2710ffb11a4ad7d0bddaf3bc11d075d62ddaa8b915cL916-R986), [[11]](https://github.com/forfudan/decimojo/pull/100/files#diff-95a5c66e5957fff2a2a8a2710ffb11a4ad7d0bddaf3bc11d075d62ddaa8b915cL1063-R1176), [[12]](https://github.com/forfudan/decimojo/pull/100/files#diff-95a5c66e5957fff2a2a8a2710ffb11a4ad7d0bddaf3bc11d075d62ddaa8b915cL1090-R1185), [[13]](https://github.com/forfudan/decimojo/pull/100/files#diff-95a5c66e5957fff2a2a8a2710ffb11a4ad7d0bddaf3bc11d075d62ddaa8b915cL1111-R1206), [[14]](https://github.com/forfudan/decimojo/pull/100/files#diff-95a5c66e5957fff2a2a8a2710ffb11a4ad7d0bddaf3bc11d075d62ddaa8b915cL1136-R1231))

@forfudan forfudan merged commit 7d387ac into main Jul 6, 2025
1 check passed
@forfudan forfudan deleted the work branch July 6, 2025 21:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants