Skip to content

Add min, max, is_zero#66

Merged
hardyjosh merged 3 commits intomainfrom
feat/min-max-iszero
Jun 24, 2025
Merged

Add min, max, is_zero#66
hardyjosh merged 3 commits intomainfrom
feat/min-max-iszero

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 min, max, and is_zero

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 compare two floats and return their minimum or maximum value.
    • Added a method to check if a float value is zero.
  • Tests

    • Introduced comprehensive tests for the new comparison and zero-checking methods, including both manual and property-based tests.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 16, 2025

Walkthrough

Three new methods—min, max, and is_zero—were added to the Float struct. These methods interact with the DecimalFloat contract by constructing ABI-encoded calls, executing them in the EVM, and decoding the results. Comprehensive unit and property-based tests were introduced for these methods.

Changes

File(s) Change Summary
crates/float/src/lib.rs Added min, max, and is_zero methods to Float; implemented ABI/EVM contract calls and result decoding.
crates/float/src/lib.rs Added unit and property-based tests for the new methods, covering correctness and edge cases.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant Float
    participant EVM
    participant DecimalFloatContract

    Caller->>Float: min(b)
    Float->>EVM: ABI-encoded minCall(a, b)
    EVM->>DecimalFloatContract: Execute minCall
    DecimalFloatContract-->>EVM: Return min result
    EVM-->>Float: Return encoded result
    Float-->>Caller: Decode and return Float

    Caller->>Float: is_zero()
    Float->>EVM: ABI-encoded isZeroCall(a)
    EVM->>DecimalFloatContract: Execute isZeroCall
    DecimalFloatContract-->>EVM: Return boolean result
    EVM-->>Float: Return encoded result
    Float-->>Caller: Decode and return bool
Loading

Possibly related PRs

  • 2025 04 04 min #25: Adds min and max functions and tests to the Solidity LibDecimalFloat library, directly relating to the Rust-side implementation of the same functionality in this PR.

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 c86ad7e and 6c02397.

📒 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.
⏰ Context from checks skipped due to timeout of 90000ms (7)
  • GitHub Check: rainix (ubuntu-latest, rainix-sol-static)
  • GitHub Check: rainix (ubuntu-latest, test-wasm-build)
  • 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, rainix-sol-test)
  • GitHub Check: git-clean
🔇 Additional comments (6)
crates/float/src/lib.rs (6)

337-346: LGTM! Clean implementation following established patterns.

The min method correctly follows the same pattern as other binary operations in the codebase, with proper ABI encoding, error handling, and result decoding.


348-357: LGTM! Consistent with the min method implementation.

The max method correctly mirrors the min method structure and follows established patterns for binary operations.


359-367: LGTM! Appropriate implementation for a predicate method.

The is_zero method correctly implements a unary predicate operation, returning bool instead of Float which is appropriate for a zero-check function.


856-876: LGTM! Comprehensive manual testing with good edge case coverage.

The test covers important scenarios including different sign combinations, magnitude differences, and the edge case where both operands are identical.


878-891: LGTM! Good coverage of zero representations.

The test properly covers various zero representations (-0, 0.0) which is important for floating-point zero detection, plus the negative case.


893-952: LGTM! Excellent property-based testing with comprehensive mathematical properties.

The test covers all fundamental mathematical properties of min/max operations with detailed error messages for debugging. The property assertions are mathematically sound and provide thorough validation.

✨ 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 marked this pull request as ready for review June 20, 2025 14:03
@0xgleb 0xgleb requested review from findolor and hardyjosh June 23, 2025 12:18
@hardyjosh hardyjosh merged commit f8e03c1 into main Jun 24, 2025
8 checks passed
@0xgleb 0xgleb deleted the feat/min-max-iszero branch June 24, 2025 11:05
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 min, max, and isZero methods

3 participants