Skip to content

update div precision#104

Merged
thedavidmeister merged 2 commits intomainfrom
2025-08-15-maximize-numerator-div
Aug 15, 2025
Merged

update div precision#104
thedavidmeister merged 2 commits intomainfrom
2025-08-15-maximize-numerator-div

Conversation

@thedavidmeister
Copy link
Copy Markdown
Contributor

@thedavidmeister thedavidmeister commented Aug 15, 2025

Motivation

Solution

Checks

By submitting this for review, I'm confirming I've done the following:

  • made this PR as small as possible
  • unit-tested any new functionality
  • linked any relevant issues or PRs
  • included screenshots (if this involves a front-end change)

Summary by CodeRabbit

  • Bug Fixes
    • Improved numeric precision for division and inversion, yielding consistent exponent handling (shifted by -1 in affected results).
    • Updated power and pow10 outcomes to align with refined scaling, producing larger mantissas and adjusted exponents where applicable.
  • Tests
    • Updated test expectations to match new arithmetic behavior across division, power, and pow10 cases.
    • Corrected a misspelled test name.
  • Chores
    • Refreshed performance snapshots (gas/μ) to reflect current metrics.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Aug 15, 2025

Walkthrough

Updates core division and inversion arithmetic in LibDecimalFloatImplementation, adjusting scaling and exponent handling. Tests are re-baselined to match new results, including constants and expectations across division, pow, mixed, and pow10 tests. Gas snapshot baselines are refreshed. One test name typo is corrected.

Changes

Cohort / File(s) Summary
Core arithmetic changes
src/lib/implementation/LibDecimalFloatImplementation.sol
div: use maximize for A, remove 1e38 scaling, exponent delta becomes exponentA - exponentB; inv: scale factor 1e76 and exponent shift -76.
Test constants update
test/lib/LibCommonResults.sol
ONES and THREES constants extended by one digit, increasing their values.
Division tests re-baseline
test/src/lib/implementation/LibDecimalFloatImplementation.div.t.sol
Expected exponents generally decremented by 1; coefficients scaled accordingly; inner loop init j from -39 - di.
Mixed tests adjustment
test/src/lib/LibDecimalFloat.mixed.t.sol
Updated expected exponent and coefficient for 1/3 division/multiplication case.
Pow tests adjustment
test/src/lib/LibDecimalFloat.pow.t.sol
Two pow expectations updated with larger mantissas and exponents shifted by one order.
pow10 tests adjustment
test/src/lib/implementation/LibDecimalFloatImplementation.pow10.t.sol
Updated expected result: exponent -39 and reformatted coefficient.
Gas snapshot re-baseline
.gas-snapshot
Updated gas/micro snapshots across tests; no logic changes.
Test name fix
test/src/lib/implementation/LibDecimalFloatImplementation.add.t.sol
Rename testOverflowCheks → testOverflowChecks.

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant Lib as LibDecimalFloatImplementation
  Caller->>Lib: div(signedCoefficientA, exponentA, signedCoefficientB, exponentB)
  Lib->>Lib: maximize(A.coeff, A.exp)
  Lib->>Lib: normalize(B.coeff, B.exp)
  Lib->>Lib: coeff = A / B
  Lib->>Lib: exp = A.exp - B.exp
  Lib-->>Caller: DecimalFloat(coeff, exp)
Loading
sequenceDiagram
  participant Caller
  participant Lib as LibDecimalFloatImplementation
  Caller->>Lib: inv(signedCoefficient, exponent)
  Lib->>Lib: normalize(coeff, exp)
  Lib->>Lib: coeff' = 1e76 / coeff
  Lib->>Lib: exp' = -exp - 76
  Lib-->>Caller: DecimalFloat(coeff', exp')
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • 2025 08 13 maximize moar #99: Also renames testOverflowCheks to testOverflowChecks in the same test file, indicating overlap in test maintenance.

Suggested reviewers

  • hardyjosh

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.

✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch 2025-08-15-maximize-numerator-div

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 11

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 8f9cc3b and e168a12.

📒 Files selected for processing (7)
  • .gas-snapshot (7 hunks)
  • src/lib/implementation/LibDecimalFloatImplementation.sol (2 hunks)
  • test/lib/LibCommonResults.sol (1 hunks)
  • test/src/lib/LibDecimalFloat.mixed.t.sol (1 hunks)
  • test/src/lib/LibDecimalFloat.pow.t.sol (1 hunks)
  • test/src/lib/implementation/LibDecimalFloatImplementation.div.t.sol (3 hunks)
  • test/src/lib/implementation/LibDecimalFloatImplementation.pow10.t.sol (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
  • GitHub Check: rainix (ubuntu-latest, rainix-sol-static)
  • GitHub Check: rainix (ubuntu-latest, rainix-sol-test)
  • GitHub Check: rainix (ubuntu-latest, rainix-rs-test)
  • GitHub Check: git-clean
🔇 Additional comments (8)
.gas-snapshot (1)

1-305: Gas snapshot baseline update reflects division precision changes.

The gas snapshot has been comprehensively updated to reflect the new baseline measurements after the division precision changes. All test metrics show consistent patterns with the core implementation updates.

The snapshot data appears to be systematically updated across all test cases, which is expected when core arithmetic operations like division change their precision handling.

src/lib/implementation/LibDecimalFloatImplementation.sol (3)

195-196: Division coefficient and exponent calculation simplified.

The core division calculation has been simplified:

  • Coefficient: Changed from (A * 1e38) / B to A / B
  • Exponent: Changed from exponentA - exponentB - 38 to exponentA - exponentB

This removes the intermediate scaling factor of 1e38, which explains why test expectations needed to shift by one order of magnitude (exponent decrements by 1).

This change appears to be a precision optimization that eliminates unnecessary intermediate scaling while maintaining mathematical correctness.


374-375: Inverse function precision increased from 1e75 to 1e76.

The inverse calculation has been updated to use a larger scaling constant:

  • Divisor constant: 1e751e76
  • Exponent adjustment: -exponent - 75-exponent - 76

This change is consistent with the division precision updates and maintains the mathematical relationship inv(x) = 1/x.

The precision increase aligns with the division changes and should improve numerical accuracy for inverse operations.


192-193: Division precision adjustment is intentional

The change to call maximize on the dividend (and normalize on the divisor) before performing signedCoefficientA / signedCoefficientB is by design—it scales the numerator up to maximize precision, while keeping the denominator within normalized bounds to avoid overflow. Unit tests in LibDecimalFloatImplementation.div.t.sol and higher‐level div tests exercise this behavior.

No code changes required.

test/src/lib/LibDecimalFloat.mixed.t.sol (2)

19-19: Test expectation correctly updated for new division precision.

The division exponent expectation has been updated from -38 to -39, reflecting the one-order-of-magnitude shift in the core division implementation.


24-25: Multiplication result expectations updated consistently.

The test expectations for the multiplication result have been updated:

  • signedCoefficientMul: Extended by one order of magnitude
  • exponentMul: Decreased from -20 to -21

These changes are mathematically consistent with the upstream division precision changes and maintain the overall test validity.

test/src/lib/implementation/LibDecimalFloatImplementation.pow10.t.sol (1)

59-59: Pow10 test expectation updated for consistency with division changes.

The test expectation for checkPow10(-0.3e37, -37) has been updated to reflect the new precision handling:

  • Expected coefficient: Increased by one order of magnitude to 0.501253132832080200501253132832080200501e39
  • Expected exponent: Decreased from -38 to -39

This change maintains mathematical consistency with the updated division and inverse implementations.

The updated expectation preserves the same numerical value while reflecting the new internal precision representation.

test/src/lib/LibDecimalFloat.pow.t.sol (1)

41-42: Pow expectations updated consistently with new scaling (×10 mantissa, -1 exponent).

Both updated assertions reflect the global shift in division/inversion precision and look correct. Values remain within int256 bounds and respect the library’s 39-digit scale.

Comment thread test/lib/LibCommonResults.sol
Comment thread test/src/lib/implementation/LibDecimalFloatImplementation.div.t.sol
Comment thread test/src/lib/implementation/LibDecimalFloatImplementation.div.t.sol
Comment thread test/src/lib/implementation/LibDecimalFloatImplementation.div.t.sol
Comment thread test/src/lib/implementation/LibDecimalFloatImplementation.div.t.sol
Comment thread test/src/lib/implementation/LibDecimalFloatImplementation.div.t.sol
Comment thread test/src/lib/implementation/LibDecimalFloatImplementation.div.t.sol
Comment thread test/src/lib/implementation/LibDecimalFloatImplementation.div.t.sol
Comment thread test/src/lib/implementation/LibDecimalFloatImplementation.div.t.sol
Comment thread test/src/lib/implementation/LibDecimalFloatImplementation.div.t.sol
@thedavidmeister thedavidmeister merged commit 040b821 into main Aug 15, 2025
8 checks passed
@github-actions
Copy link
Copy Markdown

@coderabbitai assess this PR size classification for the totality of the PR with the following criterias and report it in your comment:

S/M/L PR Classification Guidelines:

This guide helps classify merged pull requests by effort and complexity rather than just line count. The goal is to assess the difficulty and scope of changes after they have been completed.

Small (S)

Characteristics:

  • Simple bug fixes, typos, or minor refactoring
  • Single-purpose changes affecting 1-2 files
  • Documentation updates
  • Configuration tweaks
  • Changes that require minimal context to review

Review Effort: Would have taken 5-10 minutes

Examples:

  • Fix typo in variable name
  • Update README with new instructions
  • Adjust configuration values
  • Simple one-line bug fixes
  • Import statement cleanup

Medium (M)

Characteristics:

  • Feature additions or enhancements
  • Refactoring that touches multiple files but maintains existing behavior
  • Breaking changes with backward compatibility
  • Changes requiring some domain knowledge to review

Review Effort: Would have taken 15-30 minutes

Examples:

  • Add new feature or component
  • Refactor common utility functions
  • Update dependencies with minor breaking changes
  • Add new component with tests
  • Performance optimizations
  • More complex bug fixes

Large (L)

Characteristics:

  • Major feature implementations
  • Breaking changes or API redesigns
  • Complex refactoring across multiple modules
  • New architectural patterns or significant design changes
  • Changes requiring deep context and multiple review rounds

Review Effort: Would have taken 45+ minutes

Examples:

  • Complete new feature with frontend/backend changes
  • Protocol upgrades or breaking changes
  • Major architectural refactoring
  • Framework or technology upgrades

Additional Factors to Consider

When deciding between sizes, also consider:

  • Test coverage impact: More comprehensive test changes lean toward larger classification
  • Risk level: Changes to critical systems bump up a size category
  • Team familiarity: Novel patterns or technologies increase complexity

Notes:

  • the assessment must be for the totality of the PR, that means comparing the base branch to the last commit of the PR
  • the assessment output must be exactly one of: S, M or L (single-line comment) in format of: SIZE={S/M/L}
  • do not include any additional text, only the size classification
  • your assessment comment must not include tips or additional sections
  • do NOT tag me or anyone else on your comment

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Aug 15, 2025

Tip

For best results, initiate chat on the files or code changes.

🧩 Analysis chain

SIZE=M


🏁 Script executed:

#!/bin/bash
# Get a comprehensive view of all changes in this PR
git log --oneline -1
echo "=== Files changed ==="
git diff --name-only HEAD~1 HEAD
echo "=== Summary of changes ==="
git diff --stat HEAD~1 HEAD
echo "=== Core implementation changes ==="
git diff HEAD~1 HEAD -- src/lib/implementation/LibDecimalFloatImplementation.sol | head -50

Length of output: 758


Based on the comprehensive analysis from the AI summary and the information provided, I can assess this PR size classification:

Analysis of Changes:

  • Core functionality modified: Changes to division precision in LibDecimalFloatImplementation.sol affecting div() and inv() functions
  • Mathematical algorithm changes: Division semantics shifted from scaled approach (A * 1e38) / B to direct A / B calculation
  • Multiple files affected: 6+ files including implementation, constants, and extensive test updates
  • Test cascade effects: Multiple test files required expectation updates due to precision changes
  • Domain expertise required: Understanding floating-point precision mathematics to properly review

Complexity Assessment:

  • Not a simple bug fix or typo (eliminates Small classification)
  • Requires mathematical domain knowledge to review properly
  • Changes fundamental behavior while maintaining API compatibility
  • Extensive test updates indicate significant behavioral changes
  • Would require 15-30 minutes of focused review to understand implications

SIZE=M


🏁 Script executed:

#!/bin/bash
# Check git history and get PR details
git log --oneline -5
echo "=== Current branch ==="
git branch
echo "=== Get all files in the repo to understand scope ==="
find . -name "*.sol" -type f | head -20
echo "=== Check if we can see diff from origin ==="
git remote -v
git log --oneline origin/main..HEAD 2>/dev/null || echo "Cannot access origin/main"

Length of output: 1282


SIZE=M

This was referenced Aug 16, 2025
This was referenced Aug 26, 2025
@coderabbitai coderabbitai Bot mentioned this pull request Sep 9, 2025
7 tasks
@coderabbitai coderabbitai Bot mentioned this pull request Sep 22, 2025
4 tasks
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.

1 participant