Skip to content

simplify float#39

Merged
thedavidmeister merged 3 commits intomainfrom
2025-05-04-format
May 5, 2025
Merged

simplify float#39
thedavidmeister merged 3 commits intomainfrom
2025-05-04-format

Conversation

@thedavidmeister
Copy link
Copy Markdown
Contributor

@thedavidmeister thedavidmeister commented May 5, 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

  • Refactor
    • Streamlined decimal formatting logic for improved clarity and maintainability.
    • Updated function signatures and removed redundant formatting functions.
  • Tests
    • Enhanced test coverage with new example and negative round-trip tests.
    • Simplified and reorganized test structure for better readability and code reuse.
    • Removed outdated memory formatting test.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 5, 2025

"""

Walkthrough

The changes refactor the toDecimalString function in the LibFormatDecimalFloat library to accept a single Float struct parameter instead of separate coefficient and exponent parameters. Associated test code is updated to use this new interface, with a helper function introduced for formatting and assertion. The tests are reorganized, with some removed and new ones added to cover negative round-trip and example cases. The gas snapshot is updated to reflect the new and modified tests, including changes in gas usage metrics.

Changes

File(s) Change Summary
src/lib/format/LibFormatDecimalFloat.sol Refactored toDecimalString to accept a Float struct; removed the overload that delegated to the two-parameter version; updated internal unpacking logic.
test/src/lib/format/LibFormatDecimalFloat.t.sol Updated tests to use a new internal checkFormat helper; removed external formatting function and memory/stack comparison test; consolidated assertions through the helper function.
.gas-snapshot Removed two old tests and added three new/updated tests for formatting; updated gas usage statistics to reflect new test structure and coverage.

Sequence Diagram(s)

sequenceDiagram
    participant Test as Test Contract
    participant Lib as LibFormatDecimalFloat

    Test->>Lib: toDecimalString(Float)
    Lib->>Lib: unpack Float into (signedCoefficient, exponent)
    Lib->>Lib: handle negative coefficient, convert to positive if needed
    Lib->>Lib: convert to fixed-point decimal (18 decimals)
    Lib->>Lib: format as string
    Lib-->>Test: formatted string
Loading

Possibly related PRs

  • format #26: Introduced the initial LibFormatDecimalFloat library with both the two-parameter and Float-parameter overloads of toDecimalString and related tests; this PR directly refactors and builds upon that code.
    """

📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between e859c0d and e4946e1.

📒 Files selected for processing (1)
  • src/lib/format/LibFormatDecimalFloat.sol (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: git-clean
  • GitHub Check: rainix (ubuntu-latest, rainix-sol-static)
  • GitHub Check: rainix (ubuntu-latest, rainix-sol-test)
🔇 Additional comments (2)
src/lib/format/LibFormatDecimalFloat.sol (2)

15-18: Well-executed API simplification.

The refactoring to accept a single Float struct instead of separate coefficient and exponent parameters improves the API design by:

  1. Making the function signature cleaner and more consistent with the Float struct pattern
  2. Eliminating the need for function overloading
  3. Providing a more intuitive interface for clients using the Float struct

This change could potentially break backward compatibility for any code directly calling the previous two-parameter version, but makes the codebase more maintainable long-term.


17-18:

Details

✅ Verification successful

Verify all calling code has been updated.

The function signature change from accepting two parameters to a single Float struct is a breaking change. Make sure all calling code in the codebase has been updated to use this new signature.


🏁 Script executed:

#!/bin/bash
# Search for all calls to toDecimalString that might need updating
# Look for call patterns that pass two parameters instead of a Float struct

echo "Searching for potential calls to the old toDecimalString signature..."
rg -A 2 "toDecimalString\s*\(\s*[^,]*,\s*[^,)]*\)" --type solidity

Length of output: 250


Let’s re-run the search across all .sol files without relying on an unrecognized file-type flag:


🏁 Script executed:

#!/bin/bash
echo "Searching for two-argument calls to toDecimalString in .sol files..."
rg -n --glob '*.sol' "toDecimalString\s*\([^,]*,[^)]*\)"

Length of output: 366


Verification complete: No legacy toDecimalString calls remain

A search across all .sol files found no two-argument invocations of toDecimalString. The only occurrence in
test/src/lib/format/LibFormatDecimalFloat.t.sol uses

LibFormatDecimalFloat.toDecimalString(
    LibDecimalFloat.packLossless(signedCoefficient, exponent)
);

which correctly passes a single Float struct. All callers have been updated—no further action required.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 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.
    • Generate unit testing code for this file.
    • 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. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • 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 src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

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

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

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

Documentation and Community

  • 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: 1

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 69fe81d and e859c0d.

📒 Files selected for processing (3)
  • .gas-snapshot (1 hunks)
  • src/lib/format/LibFormatDecimalFloat.sol (1 hunks)
  • test/src/lib/format/LibFormatDecimalFloat.t.sol (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: rainix (ubuntu-latest, rainix-sol-test)
  • GitHub Check: rainix (ubuntu-latest, rainix-sol-static)
  • GitHub Check: git-clean
🔇 Additional comments (7)
src/lib/format/LibFormatDecimalFloat.sol (2)

16-19: Great API simplification!

Refactoring toDecimalString to accept a single Float struct parameter instead of separate parameters creates a more consistent API. This approach aligns with other library functions and reduces the chance of errors by ensuring callers work with properly constructed Float structs.


18-27: Implementation looks correct.

The function correctly unpacks the Float struct and preserves the existing logic for handling negative coefficients and formatting. The implementation maintains the same functionality while providing a cleaner interface.

.gas-snapshot (1)

172-174: Gas snapshot correctly updated with new tests.

The gas snapshot has been properly updated to reflect the reorganized test structure, including the new test for negative values and example formatting. This ensures accurate gas usage tracking for the refactored code.

test/src/lib/format/LibFormatDecimalFloat.t.sol (4)

17-21: Well-designed helper function.

The new checkFormat helper function is a good refactoring that reduces code duplication and centralizes the formatting and assertion logic. It makes the tests more maintainable and readable.


38-46: Good test coverage for negative values.

Adding a dedicated test for negative number formatting improves the test suite's comprehensiveness. This test ensures that negating a number correctly applies the "-" prefix in the formatted string, which is an important edge case.


51-67: Clean test refactoring.

Replacing direct assertions with calls to the checkFormat helper function makes the tests more concise and easier to maintain. This is a good example of DRY (Don't Repeat Yourself) principle in test code.


69-75: Consistent testing approach for negative numbers.

The negative decimal test cases correctly use the same helper function, maintaining a consistent testing approach across all scenarios.

Comment thread src/lib/format/LibFormatDecimalFloat.sol Outdated
@thedavidmeister thedavidmeister merged commit 9152502 into main May 5, 2025
4 checks passed
@coderabbitai coderabbitai Bot mentioned this pull request Sep 1, 2025
4 tasks
@coderabbitai coderabbitai Bot mentioned this pull request Oct 2, 2025
4 tasks
@coderabbitai coderabbitai Bot mentioned this pull request Dec 1, 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