Skip to content

pow 0#87

Merged
thedavidmeister merged 4 commits intomainfrom
2025-07-29-pow0
Jul 29, 2025
Merged

pow 0#87
thedavidmeister merged 4 commits intomainfrom
2025-07-29-pow0

Conversation

@thedavidmeister
Copy link
Copy Markdown
Contributor

@thedavidmeister thedavidmeister commented Jul 29, 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 power calculation to correctly handle zero base with zero or negative exponents.
    • Added error handling for zero raised to a negative power.
  • Tests

    • Added tests verifying that any number to the zero power equals one.
    • Added tests confirming zero to positive powers equals zero.
    • Added tests ensuring errors are thrown when zero is raised to a negative power.
  • New Features

    • Introduced constants representing zero and one for decimal float operations.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jul 29, 2025

Walkthrough

The pow function in the LibDecimalFloat library was enhanced with special case handling to return one when the exponent is zero, return zero when the base is zero with non-negative exponent, and revert with a ZeroNegativePower error when zero is raised to a negative exponent. Corresponding tests were added to verify these cases. A new error type and constants for zero and one were also introduced.

Changes

Cohort / File(s) Change Summary
LibDecimalFloat pow special cases
src/lib/LibDecimalFloat.sol
Added constants FLOAT_ZERO and FLOAT_ONE. Enhanced pow function with early returns for zero exponent and zero base, and revert on zero base with negative exponent.
Zero negative power error
src/error/ErrDecimalFloat.sol
Added new error ZeroNegativePower(Float b) for handling zero base raised to negative exponent attempts.
Pow special cases tests
test/src/lib/LibDecimalFloat.pow.t.sol
Added tests to verify that raising any base to zero returns one, zero to positive exponent returns zero, and zero to negative exponent reverts with ZeroNegativePower.
Constants tests
test/src/lib/LibDecimalFloat.constants.t.sol
Added tests to verify the correctness of new constants FLOAT_ZERO and FLOAT_ONE.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~15 minutes

Possibly related PRs

  • float struct #24: Introduces the Float struct and refactors power functions in LibDecimalFloat, related to the pow functionality changes in this PR.

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 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 7f67914 and 2fbc49e.

📒 Files selected for processing (1)
  • test/src/lib/LibDecimalFloat.constants.t.sol (1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: 0xgleb
PR: rainlanguage/rain.math.float#59
File: crates/float/src/lib.rs:233-242
Timestamp: 2025-06-17T10:17:56.205Z
Learning: In the rainlanguage/rain.math.float repository, the maintainer 0xgleb prefers to handle documentation additions and improvements in separate issues rather than inline with feature PRs.
Learnt from: 0xgleb
PR: rainlanguage/rain.math.float#70
File: crates/float/src/evm.rs:38-43
Timestamp: 2025-07-03T11:20:50.456Z
Learning: In the rainlanguage/rain.math.float codebase, the user 0xgleb prefers not to add explanatory comments for well-established Rust idioms like the double `?` pattern, as these are self-explanatory to experienced Rust developers and don't need over-commenting.
Learnt from: rouzwelt
PR: rainlanguage/rain.math.float#76
File: test_js/float.test.ts:9-32
Timestamp: 2025-07-17T02:38:44.698Z
Learning: In the rainlanguage/rain.math.float repository, the user rouzwelt accepts non-null assertions in test files because tests should throw and fail immediately when something goes wrong, making it clear where the issue occurred.
test/src/lib/LibDecimalFloat.constants.t.sol (11)

Learnt from: 0xgleb
PR: #58
File: src/concrete/DecimalFloat.sol:175-182
Timestamp: 2025-06-16T13:17:28.513Z
Learning: In the rainlanguage/rain.math.float codebase, there's an established naming convention where functions accepting a Float type parameter consistently use float as the parameter name, even though it shadows the type name. This pattern is used throughout LibDecimalFloat.sol and should be maintained for consistency in related contracts like DecimalFloat.sol.

Learnt from: rouzwelt
PR: #83
File: src/concrete/DecimalFloat.sol:248-251
Timestamp: 2025-07-24T04:32:14.171Z
Learning: In the rainlanguage/rain.math.float project, functions in DecimalFloat.sol that return tuples from LibDecimalFloat calls must unpack the tuple into local variables before returning them (rather than returning directly) to maintain compatibility with Slither static analysis checks.

Learnt from: thedavidmeister
PR: #30
File: test/src/lib/LibDecimalFloat.gt.t.sol:33-36
Timestamp: 2025-04-25T03:58:01.307Z
Learning: In the rain.math.float library, all values of Float (which is a type alias for bytes32) are considered valid and can be safely used with methods like gt(), lt(), or eq() without causing reverts.

Learnt from: 0xgleb
PR: #58
File: crates/float/src/lib.rs:382-401
Timestamp: 2025-06-16T13:19:32.009Z
Learning: In the rainlanguage/rain.math.float codebase, variable shadowing in property tests like test_lt_eq_gt_with_add is acceptable when it represents a logical progression of the same conceptual value being tested through different states.

Learnt from: 0xgleb
PR: #46
File: crates/float/proptest-regressions/lib.txt:1-3
Timestamp: 2025-06-11T09:03:20.771Z
Learning: The file crates/float/proptest-regressions/lib.txt is automatically generated by proptest and should not be manually edited.

Learnt from: 0xgleb
PR: #64
File: src/concrete/DecimalFloat.sol:0-0
Timestamp: 2025-06-17T10:02:01.394Z
Learning: In the rain.math.float project, the Float type is designed with a specific bit layout: 224-bit signed coefficient and 32-bit signed exponent, represented as 32 bytes total. The LibDecimalFloat.unpack function returns values that are guaranteed to fit within int224 and int32 ranges due to this internal representation, making explicit range checks before casting unnecessary.

Learnt from: 0xgleb
PR: #64
File: src/concrete/DecimalFloat.sol:0-0
Timestamp: 2025-06-17T10:02:01.394Z
Learning: In the rain.math.float project, the Float type is designed with a specific bit layout: 224-bit signed coefficient and 32-bit signed exponent, represented as 32 bytes total. The LibDecimalFloat.unpack function returns values that are guaranteed to fit within int224 and int32 ranges due to this internal representation, making explicit range checks before casting unnecessary.

Learnt from: rouzwelt
PR: #76
File: test_js/float.test.ts:9-32
Timestamp: 2025-07-17T02:38:44.698Z
Learning: In the rainlanguage/rain.math.float repository, the user rouzwelt accepts non-null assertions in test files because tests should throw and fail immediately when something goes wrong, making it clear where the issue occurred.

Learnt from: 0xgleb
PR: #59
File: crates/float/src/lib.rs:447-461
Timestamp: 2025-06-17T10:11:32.740Z
Learning: In the rainlanguage/rain.math.float repository, the user 0xgleb prefers using .unwrap() over ? in tests because it provides clearer stack traces showing the exact line where panics occurred, making debugging easier.

Learnt from: 0xgleb
PR: #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.

Learnt from: 0xgleb
PR: #70
File: crates/float/src/evm.rs:38-43
Timestamp: 2025-07-03T11:20:50.456Z
Learning: In the rainlanguage/rain.math.float codebase, the user 0xgleb prefers not to add explanatory comments for well-established Rust idioms like the double ? pattern, as these are self-explanatory to experienced Rust developers and don't need over-commenting.

⏰ 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). (7)
  • GitHub Check: rainix (ubuntu-latest, test-wasm-build)
  • GitHub Check: rainix (ubuntu-latest, rainix-rs-static)
  • 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: rainix (macos-latest, rainix-rs-test)
  • GitHub Check: git-clean
🔇 Additional comments (1)
test/src/lib/LibDecimalFloat.constants.t.sol (1)

30-40: LGTM! Well-structured constant tests following established patterns.

The new test functions correctly verify the FLOAT_ZERO and FLOAT_ONE constants by:

  • Following the established naming convention and structure from existing constant tests
  • Using appropriate packed values: (0,0) for zero and (1,0) for one
  • Maintaining consistency with the existing test patterns using packLossless and Float.unwrap

These tests properly validate the constants that support the enhanced pow function special case handling.

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

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.

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 9de734b and 19d6197.

📒 Files selected for processing (2)
  • src/lib/LibDecimalFloat.sol (1 hunks)
  • test/src/lib/LibDecimalFloat.pow.t.sol (1 hunks)
🧰 Additional context used
🧠 Learnings (3)
📓 Common learnings
Learnt from: 0xgleb
PR: rainlanguage/rain.math.float#59
File: crates/float/src/lib.rs:233-242
Timestamp: 2025-06-17T10:17:56.205Z
Learning: In the rainlanguage/rain.math.float repository, the maintainer 0xgleb prefers to handle documentation additions and improvements in separate issues rather than inline with feature PRs.
Learnt from: 0xgleb
PR: rainlanguage/rain.math.float#70
File: crates/float/src/evm.rs:38-43
Timestamp: 2025-07-03T11:20:50.456Z
Learning: In the rainlanguage/rain.math.float codebase, the user 0xgleb prefers not to add explanatory comments for well-established Rust idioms like the double `?` pattern, as these are self-explanatory to experienced Rust developers and don't need over-commenting.
Learnt from: rouzwelt
PR: rainlanguage/rain.math.float#76
File: test_js/float.test.ts:9-32
Timestamp: 2025-07-17T02:38:44.698Z
Learning: In the rainlanguage/rain.math.float repository, the user rouzwelt accepts non-null assertions in test files because tests should throw and fail immediately when something goes wrong, making it clear where the issue occurred.
Learnt from: 0xgleb
PR: rainlanguage/rain.math.float#58
File: src/concrete/DecimalFloat.sol:175-182
Timestamp: 2025-06-16T13:17:28.513Z
Learning: In the rainlanguage/rain.math.float codebase, there's an established naming convention where functions accepting a `Float` type parameter consistently use `float` as the parameter name, even though it shadows the type name. This pattern is used throughout `LibDecimalFloat.sol` and should be maintained for consistency in related contracts like `DecimalFloat.sol`.
test/src/lib/LibDecimalFloat.pow.t.sol (3)

Learnt from: 0xgleb
PR: #58
File: src/concrete/DecimalFloat.sol:175-182
Timestamp: 2025-06-16T13:17:28.513Z
Learning: In the rainlanguage/rain.math.float codebase, there's an established naming convention where functions accepting a Float type parameter consistently use float as the parameter name, even though it shadows the type name. This pattern is used throughout LibDecimalFloat.sol and should be maintained for consistency in related contracts like DecimalFloat.sol.

Learnt from: rouzwelt
PR: #83
File: src/concrete/DecimalFloat.sol:248-251
Timestamp: 2025-07-24T04:32:14.171Z
Learning: In the rainlanguage/rain.math.float project, functions in DecimalFloat.sol that return tuples from LibDecimalFloat calls must unpack the tuple into local variables before returning them (rather than returning directly) to maintain compatibility with Slither static analysis checks.

Learnt from: thedavidmeister
PR: #30
File: test/src/lib/LibDecimalFloat.gt.t.sol:33-36
Timestamp: 2025-04-25T03:58:01.307Z
Learning: In the rain.math.float library, all values of Float (which is a type alias for bytes32) are considered valid and can be safely used with methods like gt(), lt(), or eq() without causing reverts.

src/lib/LibDecimalFloat.sol (5)

Learnt from: 0xgleb
PR: #58
File: src/concrete/DecimalFloat.sol:175-182
Timestamp: 2025-06-16T13:17:28.513Z
Learning: In the rainlanguage/rain.math.float codebase, there's an established naming convention where functions accepting a Float type parameter consistently use float as the parameter name, even though it shadows the type name. This pattern is used throughout LibDecimalFloat.sol and should be maintained for consistency in related contracts like DecimalFloat.sol.

Learnt from: rouzwelt
PR: #83
File: src/concrete/DecimalFloat.sol:248-251
Timestamp: 2025-07-24T04:32:14.171Z
Learning: In the rainlanguage/rain.math.float project, functions in DecimalFloat.sol that return tuples from LibDecimalFloat calls must unpack the tuple into local variables before returning them (rather than returning directly) to maintain compatibility with Slither static analysis checks.

Learnt from: 0xgleb
PR: #64
File: src/concrete/DecimalFloat.sol:0-0
Timestamp: 2025-06-17T10:02:01.394Z
Learning: In the rain.math.float project, the Float type is designed with a specific bit layout: 224-bit signed coefficient and 32-bit signed exponent, represented as 32 bytes total. The LibDecimalFloat.unpack function returns values that are guaranteed to fit within int224 and int32 ranges due to this internal representation, making explicit range checks before casting unnecessary.

Learnt from: 0xgleb
PR: #64
File: src/concrete/DecimalFloat.sol:0-0
Timestamp: 2025-06-17T10:02:01.394Z
Learning: In the rain.math.float project, the Float type is designed with a specific bit layout: 224-bit signed coefficient and 32-bit signed exponent, represented as 32 bytes total. The LibDecimalFloat.unpack function returns values that are guaranteed to fit within int224 and int32 ranges due to this internal representation, making explicit range checks before casting unnecessary.

Learnt from: thedavidmeister
PR: #30
File: test/src/lib/LibDecimalFloat.gt.t.sol:33-36
Timestamp: 2025-04-25T03:58:01.307Z
Learning: In the rain.math.float library, all values of Float (which is a type alias for bytes32) are considered valid and can be safely used with methods like gt(), lt(), or eq() without causing reverts.

⏰ 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). (7)
  • GitHub Check: rainix (ubuntu-latest, rainix-sol-static)
  • GitHub Check: rainix (ubuntu-latest, test-wasm-build)
  • GitHub Check: rainix (ubuntu-latest, rainix-sol-test)
  • GitHub Check: rainix (ubuntu-latest, rainix-rs-static)
  • GitHub Check: rainix (macos-latest, rainix-rs-test)
  • GitHub Check: rainix (ubuntu-latest, rainix-rs-test)
  • GitHub Check: git-clean
🔇 Additional comments (2)
src/lib/LibDecimalFloat.sol (1)

646-646: LGTM! The zero handling avoids undefined logarithmic calculations.

The special case correctly prevents log10(0) which is undefined, and Float.wrap(0) appropriately represents zero in the Float format.

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

47-53: LGTM! Comprehensive test coverage for zero base power operations.

The fuzz test effectively validates that zero raised to any power returns zero, which covers the new special case logic in the pow function. The test correctly uses packLossless(0, exponentA) to create various zero representations and isZero() to verify the result.

Comment thread src/lib/LibDecimalFloat.sol Outdated
Comment on lines +643 to +647
if (signedCoefficientA == 0) {
// If a is zero, then a^b is always zero, regardless of b.
// This is a special case because log10(0) is undefined.
return Float.wrap(0);
}
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.

💡 Verification agent

🧩 Analysis chain

Verify the mathematical correctness of 0^0 = 0 convention.

The implementation returns zero for all cases where the base is zero, including 0^0. Mathematically, 0^0 is often considered indeterminate, but many systems define it as 1 by convention for practical reasons.

Please verify if this behavior aligns with the expected mathematical conventions for your use case:


🌐 Web query:

What is the standard mathematical convention for 0^0 in floating point libraries and programming languages?

💡 Result:

The standard mathematical convention for (0^0) is somewhat debated in pure mathematics—it is often considered an indeterminate form. However, in most floating point libraries and programming languages, the convention is to define:

[
0^0 = 1
]

This choice is practical for reasons such as ensuring consistency in combinatorics, power series expansions, and function implementations (e.g., (x^0 = 1) for any (x), including zero).

Summary for common programming languages and libraries:

  • C / C++ (math.h / cmath): pow(0,0) typically returns 1.
  • Python: 0**0 returns 1.
  • Java: Math.pow(0,0) returns 1.0.
  • JavaScript: Math.pow(0,0) and 0**0 both return 1.
  • Ruby: 0**0 returns 1.
  • Julia: 0^0 returns 1.

In most IEEE 754 floating point environments, this behavior is standard to avoid dealing with undefined or NaN results in many practical programming contexts.

If you want, I can check some current documentation or behavior for specific languages or libraries as well.


Refine zero‐base handling in pow to align with IEEE 754 conventions

The current special-case in src/lib/LibDecimalFloat.sol unconditionally returns 0 for all 0^b, including 0^0 and 0^(negative), which diverges from most floating-point libraries where:

  • 0^0 == 1
  • 0^(positive) == 0
  • 0^(negative) is undefined (should revert or signal error)

Please update the block at lines 643–647 as follows:

-       if (signedCoefficientA == 0) {
-           // If a is zero, then a^b is always zero, regardless of b.
-           // This is a special case because log10(0) is undefined.
-           return Float.wrap(0);
-       }
+       if (signedCoefficientA == 0) {
+           // Handle zero base:
+           //   0^0 = 1 by convention
+           //   0^(>0) = 0
+           //   0^(<0) is undefined → revert
+           int256 rawB = Float.unwrap(b);
+           if (rawB == 0) {
+               return Float.wrap(1);
+           } else if (rawB > 0) {
+               return Float.wrap(0);
+           } else {
+               revert("LibDecimalFloat: zero to negative power");
+           }
+       }
  • File: src/lib/LibDecimalFloat.sol
  • Lines: ~643–647
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (signedCoefficientA == 0) {
// If a is zero, then a^b is always zero, regardless of b.
// This is a special case because log10(0) is undefined.
return Float.wrap(0);
}
if (signedCoefficientA == 0) {
// Handle zero base:
// 0^0 = 1 by convention
// 0^(>0) = 0
// 0^(<0) is undefined → revert
int256 rawB = Float.unwrap(b);
if (rawB == 0) {
return Float.wrap(1);
} else if (rawB > 0) {
return Float.wrap(0);
} else {
revert("LibDecimalFloat: zero to negative power");
}
}
🤖 Prompt for AI Agents
In src/lib/LibDecimalFloat.sol around lines 643 to 647, the current code returns
0 for all cases where the base is zero, which incorrectly handles 0^0 and
0^(negative). Update the logic to return 1 when the exponent is zero, return 0
when the exponent is positive, and revert or signal an error when the exponent
is negative, to align with IEEE 754 conventions.

@thedavidmeister thedavidmeister merged commit 9440452 into main Jul 29, 2025
8 checks passed
@coderabbitai coderabbitai Bot mentioned this pull request Jul 30, 2025
4 tasks
@coderabbitai coderabbitai Bot mentioned this pull request Aug 18, 2025
4 tasks
This was referenced Sep 1, 2025
This was referenced Sep 10, 2025
@coderabbitai coderabbitai Bot mentioned this pull request Sep 25, 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