Skip to content

wip on frac#384

Merged
thedavidmeister merged 3 commits into
mainfrom
2025-07-28-frac
Jul 28, 2025
Merged

wip on frac#384
thedavidmeister merged 3 commits into
mainfrom
2025-07-28-frac

Conversation

@thedavidmeister
Copy link
Copy Markdown
Contributor

@thedavidmeister thedavidmeister commented Jul 28, 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

  • New Features

    • Enabled the "frac" operation, allowing users to compute the fractional part of a number within the interpreter.
  • Improvements

    • Updated the "frac" operation to use decimal floating point arithmetic for improved accuracy and consistency.
    • Enhanced testing for the "frac" operation with updated data types and decimal float abstractions.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jul 28, 2025

Walkthrough

The changes fully enable the "frac" opcode in the standard operations library by importing and registering it within LibAllStandardOps, updating opcode counts, and activating its metadata and handler functions. The implementation of LibOpFrac is refactored from fixed-point to decimal floating point arithmetic, updating all relevant function signatures and logic. Corresponding tests are updated to use the new floating point types and operand structures.

Changes

Cohort / File(s) Change Summary
Standard Ops Registration & Metadata
src/lib/op/LibAllStandardOps.sol
Imports LibOpFrac, increases opcode count to 59, and fully registers the "frac" opcode by uncommenting its metadata, operand handler, integrity, and execution function pointers, enabling its use in the interpreter.
Frac Opcode Implementation
src/lib/op/math/LibOpFrac.sol
Refactors LibOpFrac from 18-decimal fixed point arithmetic to decimal floating point arithmetic, updates imports, function signatures (OperandOperandV2), and logic for integrity and run. Adds referenceFn for testing, using the new floating point type.
Frac Opcode Tests
test/src/lib/op/math/LibOpFrac.t.sol
Updates tests for LibOpFrac to use OperandV2, Float, and StackItem types reflecting the decimal floating point refactor. Adjusts test inputs and evaluation logic accordingly while maintaining existing test coverage and assertions.

Sequence Diagram(s)

sequenceDiagram
  participant Interpreter
  participant LibAllStandardOps
  participant LibOpFrac
  participant LibDecimalFloat

  Interpreter->>LibAllStandardOps: Lookup "frac" opcode
  LibAllStandardOps->>LibOpFrac: Call integrity() and run()
  LibOpFrac->>LibDecimalFloat: Compute fractional part (frac())
  LibDecimalFloat-->>LibOpFrac: Return fractional part
  LibOpFrac-->>LibAllStandardOps: Return result
  LibAllStandardOps-->>Interpreter: Return result to stack
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~15 minutes

Possibly related PRs

  • e constant #375: Integrates the "e" opcode and its implementation in LibOpE, similar structural changes to opcode registration and handling as this PR for "frac".
  • inv op #374: Integrates the "inv" opcode with similar registration and implementation pattern, sharing the same files and approach as this PR.
  • 2025 07 09 div #351: Integrates the "div" opcode using decimal floating point arithmetic, closely mirroring the structural and implementation changes made here for "frac".

📜 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 9d8cd80 and 8bbf91d.

📒 Files selected for processing (1)
  • test/src/lib/op/math/LibOpFrac.t.sol (1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: thedavidmeister
PR: rainlanguage/rain.interpreter#360
File: src/lib/op/erc20/LibOpERC20Allowance.sol:0-0
Timestamp: 2025-07-15T11:31:28.010Z
Learning: In the rainlanguage/rain.interpreter project, forge (Foundry's formatting tool) handles code formatting automatically, so formatting-related suggestions are not actionable.
Learnt from: thedavidmeister
PR: rainlanguage/rain.interpreter#360
File: src/lib/op/erc20/LibOpERC20Allowance.sol:0-0
Timestamp: 2025-07-15T11:31:35.645Z
Learning: In the rain.interpreter codebase, the team uses forge for automatic code formatting, so manual formatting suggestions are not needed as the tool will handle formatting automatically.
test/src/lib/op/math/LibOpFrac.t.sol (1)

Learnt from: thedavidmeister
PR: #368
File: test/src/lib/op/math/uint256/LibOpUint256Mul.t.sol:56-69
Timestamp: 2025-07-17T14:15:14.886Z
Learning: In multiplication overflow detection tests like LibOpUint256MulTest, when performing sequential multiplication (a * b * c * d...), encountering a zero value means the final result will always be zero regardless of subsequent values. Since zero multiplied by any value (including MAX_UINT256) cannot overflow, it's safe and correct to break out of the overflow detection loop early when zero is encountered.

🔇 Additional comments (5)
test/src/lib/op/math/LibOpFrac.t.sol (5)

4-8: LGTM! Imports align with decimal floating point modernization.

The imports correctly reflect the transition from fixed-point to decimal floating point arithmetic, including the necessary LibDecimalFloat, Float, and StackItem types.


10-17: LGTM! Integrity test correctly validates operation signature.

The test properly verifies that the frac operation takes exactly 1 input and produces 1 output, using the updated OperandV2 type.


19-28: LGTM! Runtime test correctly adapted for decimal floating point.

The test properly handles the new Float input type and StackItem array structure, correctly unwrapping the float value for stack processing.


30-40: LGTM! Comprehensive eval tests with correct expected values.

The test cases properly cover various scenarios including:

  • Whole numbers (0, 1, 2, 3) → fractional part 0
  • Decimal numbers (0.5, 3.8) → correct fractional parts
  • Negative numbers (-0.5) → correct handling
  • Large numbers (1.5e10) → correctly identifies as whole number

The use of LibDecimalFloat.packLossless correctly encodes the expected floating point results.


42-62: LGTM! Comprehensive error condition testing.

The error tests properly validate:

  • Input count constraints (exactly 1 input required)
  • Output count constraints (exactly 1 output produced)
  • Operand restrictions (no operands allowed)

All test cases use appropriate error checking methods and expected error conditions.

✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch 2025-07-28-frac

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 generate unit tests to generate unit tests for 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.

@thedavidmeister thedavidmeister merged commit f6fd07e into main Jul 28, 2025
11 checks passed
@coderabbitai coderabbitai Bot mentioned this pull request Aug 6, 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