Skip to content

Expose frac and floor#65

Merged
hardyjosh merged 4 commits intomainfrom
feat/frac-and-floor
Jun 20, 2025
Merged

Expose frac and floor#65
hardyjosh merged 4 commits intomainfrom
feat/frac-and-floor

Conversation

@0xgleb
Copy link
Copy Markdown
Contributor

@0xgleb 0xgleb commented Jun 16, 2025

Motivation

We need to use Solidity Float code in Rust

Solution

Expose frac and floor methods

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

  • New Features

    • Added methods to obtain the fractional part and floor value of float numbers.
  • Tests

    • Introduced comprehensive tests to ensure the correctness of the new float operations.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 16, 2025

Walkthrough

The Float struct has been extended with two new methods: frac and floor. These methods interact with corresponding Solidity contract functions, process the results, and return new Float instances or errors. Comprehensive unit and property-based tests for these methods have also been added.

Changes

File(s) Change Summary
crates/float/src/lib.rs Added frac and floor methods to Float; implemented ABI calls and result decoding.
crates/float/src/lib.rs (tests section) Added unit and property-based tests for frac and floor.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Float
    participant SolidityContract
    User->>Float: call frac()/floor()
    Float->>SolidityContract: ABI-encoded call (fracCall/floorCall)
    SolidityContract-->>Float: Encoded result
    Float-->>User: Decoded Float instance or error
Loading

Possibly related PRs

Suggested labels

enhancement

Suggested reviewers

  • findolor
  • hardyjosh

📜 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 7c0f850 and 24ee32d.

📒 Files selected for processing (1)
  • crates/float/src/lib.rs (2 hunks)
🧰 Additional context used
🧠 Learnings (1)
crates/float/src/lib.rs (1)
Learnt from: 0xgleb
PR: rainlanguage/rain.math.float#57
File: crates/float/src/lib.rs:316-328
Timestamp: 2025-06-18T09:10:41.740Z
Learning: In the rain.math.float codebase, the user prefers implementing standard Rust traits (like Neg) rather than creating redundant public methods when the trait already provides the needed functionality. Float implements Copy, so reference usage with operators is not a concern.
🔇 Additional comments (5)
crates/float/src/lib.rs (5)

317-325: LGTM: Correct implementation following established patterns.

The frac method correctly:

  • Extracts the inner value using destructuring
  • Creates appropriate calldata for the Solidity fracCall function
  • Uses the shared execute_call function for consistent error handling
  • Returns a properly wrapped Float result

327-335: LGTM: Correct implementation following established patterns.

The floor method correctly:

  • Extracts the inner value using destructuring
  • Creates appropriate calldata for the Solidity floorCall function
  • Uses the shared execute_call function for consistent error handling
  • Returns a properly wrapped Float result

744-764: LGTM: Comprehensive integer test coverage.

The test correctly verifies the mathematical properties for integer inputs:

  • Floor of an integer equals the original integer
  • Fractional part of an integer equals zero
  • Decomposition property (original = floor + frac) holds
  • Tests both positive and negative integers

766-777: LGTM: Specific value verification test.

The test correctly verifies expected behavior for a specific floating-point input, ensuring the floor and fractional parts match expected values.


779-822: LGTM: Excellent property-based testing.

The property-based test comprehensively verifies the mathematical properties of frac and floor operations:

  • floor.frac() == 0: Floor has no fractional part
  • frac.floor() == 0: Fractional part has floor of zero
  • original == floor + frac: Decomposition property holds
  • -1 < frac < 1: Fractional part bounds are correct

This thorough testing approach ensures correctness across arbitrary inputs.

✨ Finishing Touches
  • 📝 Generate Docstrings

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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

@0xgleb 0xgleb self-assigned this Jun 16, 2025
@0xgleb 0xgleb linked an issue Jun 16, 2025 that may be closed by this pull request
@0xgleb 0xgleb mentioned this pull request Jun 16, 2025
4 tasks
@0xgleb 0xgleb marked this pull request as ready for review June 19, 2025 10:33
@0xgleb 0xgleb requested review from findolor and hardyjosh June 19, 2025 10:34
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: 2

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between d3cfae3 and 7c0f850.

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

754-832: Excellent comprehensive test coverage with robust property-based testing.

The test suite is well-structured and covers:

  • Unit tests for integer and floating-point inputs
  • Property-based tests verifying mathematical invariants:
    • floor(x).frac() == 0 (floor has no fractional part)
    • frac(x).floor() == 0 (fractional part's floor is zero)
    • x == floor(x) + frac(x) (decomposition property)
    • -1 < frac(x) < 1 (fractional part bounds)

This thorough testing approach ensures the mathematical correctness of the implementations and provides confidence in the functionality.

Comment thread crates/float/src/lib.rs Outdated
Comment thread crates/float/src/lib.rs
Comment on lines +326 to +346
impl Float {
pub fn frac(self) -> Result<Float, FloatError> {
let Float(a) = self;
let calldata = DecimalFloat::fracCall { a }.abi_encode();

execute_call(Bytes::from(calldata), |output| {
let decoded = DecimalFloat::fracCall::abi_decode_returns(output.as_ref())?;
Ok(Float(decoded))
})
}

pub fn floor(self) -> Result<Float, FloatError> {
let Float(a) = self;
let calldata = DecimalFloat::floorCall { a }.abi_encode();

execute_call(Bytes::from(calldata), |output| {
let decoded = DecimalFloat::floorCall::abi_decode_returns(output.as_ref())?;
Ok(Float(decoded))
})
}
}
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot Jun 19, 2025

Choose a reason for hiding this comment

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

🧹 Nitpick (assertive)

Consider consolidating impl blocks for better code organization.

The frac and floor methods are correctly implemented and follow the established patterns. However, having a separate impl block for these methods creates unnecessary fragmentation.

Consider moving these methods to the main impl block (starting at line 137) to keep all Float methods together:

-impl Float {
-    pub fn frac(self) -> Result<Float, FloatError> {
-        let Float(a) = self;
-        let calldata = DecimalFloat::fracCall { a }.abi_encode();
-
-        execute_call(Bytes::from(calldata), |output| {
-            let decoded = DecimalFloat::fracCall::abi_decode_returns(output.as_ref())?;
-            Ok(Float(decoded))
-        })
-    }
-
-    pub fn floor(self) -> Result<Float, FloatError> {
-        let Float(a) = self;
-        let calldata = DecimalFloat::floorCall { a }.abi_encode();
-
-        execute_call(Bytes::from(calldata), |output| {
-            let decoded = DecimalFloat::floorCall::abi_decode_returns(output.as_ref())?;
-            Ok(Float(decoded))
-        })
-    }
-}

And add these methods to the main impl block instead.

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In crates/float/src/lib.rs between lines 326 and 346, the frac and floor methods
are implemented in a separate impl block, causing fragmentation. To fix this,
move these methods into the main Float impl block that starts around line 137,
consolidating all Float methods into a single impl block for better organization
and maintainability.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

this isn't needed and organization will be updated later anyway

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@0xgleb Understood! You're right that the core functionality is what matters for this PR. The frac and floor methods are implemented correctly and follow the established patterns. Code organization can indeed be addressed in a future refactoring when you're ready to tackle the broader structural improvements.

🐰💼
Rabbit filing away organizational notes for later

@hardyjosh hardyjosh merged commit c86ad7e into main Jun 20, 2025
8 checks passed
@coderabbitai coderabbitai Bot mentioned this pull request Feb 5, 2026
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.

Expose frac and floor methods

3 participants