Skip to content

fix: check y == 0 before abs(x) in SD59x18 powu to handle MIN_SD59x18#279

Merged
PaulRBerg merged 3 commits into
PaulRBerg:mainfrom
BhariGowda:fix/powu-zero-exponent-before-abs
May 30, 2026
Merged

fix: check y == 0 before abs(x) in SD59x18 powu to handle MIN_SD59x18#279
PaulRBerg merged 3 commits into
PaulRBerg:mainfrom
BhariGowda:fix/powu-zero-exponent-before-abs

Conversation

@BhariGowda

Copy link
Copy Markdown
Contributor

Summary

powu(MIN_SD59x18, 0) currently reverts instead of returning UNIT.

abs() is called before the y == 0 short-circuit check, and abs(MIN_SD59x18)
always reverts. Mathematically, x^0 = 1 for all x including MIN_SD59x18.

Compare with pow() which correctly handles y == 0 before touching x.

Fix

Move the y == 0 guard before the abs(x) call:

function powu(SD59x18 x, uint256 y) pure returns (SD59x18 result) {
    if (y == 0) return UNIT;
    uint256 xAbs = uint256(abs(x).unwrap());

BhariGowda and others added 3 commits May 28, 2026 09:44
- add regression coverage for MIN_SD59x18 with zero exponent
- qualify powu requirements for the zero-exponent path
@PaulRBerg

Copy link
Copy Markdown
Owner

Good catch. Thanks @BhariGowda. I've fixed the NatSpec and the formatting, and updated the tests. Merging.

@PaulRBerg
PaulRBerg merged commit 5d1d751 into PaulRBerg:main May 30, 2026
3 checks passed
@PaulRBerg

Copy link
Copy Markdown
Owner

The fix is available in v4.1.2, already shipped to npm.

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.

2 participants