Skip to content

[decimal] Implement scaleb, bit_count, __float__, fma, etc#183

Merged
forfudan merged 1 commit intoclifrom
update
Mar 3, 2026
Merged

[decimal] Implement scaleb, bit_count, __float__, fma, etc#183
forfudan merged 1 commit intoclifrom
update

Conversation

@forfudan
Copy link
Copy Markdown
Owner

@forfudan forfudan commented Mar 3, 2026

Implements several Python decimal.Decimal / int-compatibility APIs across Decimo’s numeric types, with accompanying CLI/test coverage and roadmap updates.

Changes:

  • Add BigInt.__float__() and BigInt.bit_count() APIs, plus unit tests.
  • Add BigDecimal.scaleb(), BigDecimal.fma(), and BigDecimal.to_string_with_separators(), plus unit tests.
  • Extend CLI evaluator tests to cover rounding modes; clarify CLI/evaluator precision documentation and update the API roadmap.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Implements several Python decimal.Decimal / int-compatibility APIs across Decimo’s numeric types, with accompanying CLI/test coverage and roadmap updates.

Changes:

  • Add BigInt.__float__() and BigInt.bit_count() APIs, plus unit tests.
  • Add BigDecimal.scaleb(), BigDecimal.fma(), and BigDecimal.to_string_with_separators(), plus unit tests.
  • Extend CLI evaluator tests to cover rounding modes; clarify CLI/evaluator precision documentation and update the API roadmap.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/cli/test_evaluator.mojo Adds evaluator tests verifying rounding-mode behavior at given significant-digit precision.
tests/bigint/test_bigint_conversion.mojo Adds __float__ conversion tests for BigInt.
tests/bigint/test_bigint_bitwise.mojo Adds bit_count() test coverage, including negative and large values.
tests/bigdecimal/test_bigdecimal_methods.mojo Adds tests for scaleb(), fma(), and to_string_with_separators().
src/decimo/bigint/bigint.mojo Implements __float__() and bit_count() on BigInt (and adds FloatableRaising trait conformance).
src/decimo/bigdecimal/bigdecimal.mojo Implements to_string_with_separators(), fma(), and scaleb() on BigDecimal.
src/cli/main.mojo Clarifies precision option comment as “significant digits”.
src/cli/calculator/evaluator.mojo Improves guard-digits documentation for working precision.
docs/plans/api_roadmap.md Marks newly implemented APIs as completed with brief implementation notes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +480 to +481
return Float64(String(self))

Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__float__ converts via Float64(String(self)), which forces a full base-2^32 → decimal string conversion and then reparses it. For very large BigInt values this is substantially more expensive (time + memory) than converting from the binary words directly; consider a word-based Float64 conversion to avoid the intermediate string if this becomes a hotspot.

Suggested change
return Float64(String(self))
var result = 0.0
# Process words from most significant to least significant.
# Each word contributes 32 bits in base-2^32 representation.
for i in range(len(self.words) - 1, -1, -1):
result = result * 4294967296.0 + Float64(self.words[i])
# Apply sign (assuming negative values have self.sign < 0).
if self.sign < 0:
result = -result
return result

Copilot uses AI. Check for mistakes.
Comment on lines +1271 to +1278
var count = 0
for i in range(len(self.words)):
var w = self.words[i]
# Kernighan's bit-counting trick
while w != 0:
w &= w - 1
count += 1
return count
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bit_count() iterates over all self.words. Other methods (e.g., to_string) compute an effective word count that trims most-significant zero words; if BigInt instances can contain such trailing zeros, bit_count() will do unnecessary work. Consider trimming/using an eff_words count before counting bits.

Copilot uses AI. Check for mistakes.
@forfudan forfudan merged commit f3d6001 into cli Mar 3, 2026
13 checks passed
@forfudan forfudan deleted the update branch March 3, 2026 21:36
forfudan added a commit that referenced this pull request Mar 23, 2026
)

Implements several Python `decimal.Decimal` / `int`-compatibility APIs
across Decimo’s numeric types, with accompanying CLI/test coverage and
roadmap updates.

**Changes:**
- Add `BigInt.__float__()` and `BigInt.bit_count()` APIs, plus unit
tests.
- Add `BigDecimal.scaleb()`, `BigDecimal.fma()`, and
`BigDecimal.to_string_with_separators()`, plus unit tests.
- Extend CLI evaluator tests to cover rounding modes; clarify
CLI/evaluator precision documentation and update the API roadmap.
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