Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ Refactor the is_negative Function Into a Proper sign Function #187

Merged
merged 5 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
216 changes: 108 additions & 108 deletions .gas-snapshot

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## [`0.0.5`](https://github.com/pcaversaccio/snekmate/releases/tag/v0.0.5) (Unreleased)

### ♻️ Refactoring

- **Utility Functions**
- [`Math`](https://github.com/pcaversaccio/snekmate/blob/v0.0.5/src/utils/Math.vy): Refactor the `is_negative` function into a proper `sign` function that returns the indication of the sign of a 32-byte signed integer. ([#187](https://github.com/pcaversaccio/snekmate/pull/187))

### 👀 Full Changelog

- [`v0.0.4...v0.0.5`](https://github.com/pcaversaccio/snekmate/compare/v0.0.4...v0.0.5)
Expand Down
2 changes: 1 addition & 1 deletion lib/solady
15 changes: 9 additions & 6 deletions src/utils/Math.vy
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
- `uint256_average` (`external` `pure` function),
- `int256_average` (`external` `pure` function),
- `ceil_div` (`external` `pure` function),
- `is_negative` (`external` `pure` function),
- `sign` (`external` `pure` function),
- `mul_div` (`external` `pure` function),
- `log_2` (`external` `pure` function),
- `log_10` (`external` `pure` function),
Expand Down Expand Up @@ -92,14 +92,17 @@ def ceil_div(x: uint256, y: uint256) -> uint256:

@external
@pure
def is_negative(x: int256) -> bool:
def sign(x: int256) -> int256:
"""
@dev Returns `True` if a 32-byte signed integer is negative.
@notice Note that this function returns `False` for 0.
@dev Returns the indication of the sign of a 32-byte signed integer.
@notice The function returns `-1` if `x < 0`, `0` if `x == 0`, and `1`
if `x > 0`. For more details on finding the sign of a signed
integer, please refer to:
https://graphics.stanford.edu/~seander/bithacks.html#CopyIntegerSign.
@param x The 32-byte signed integer variable.
@return bool The verification whether `x` is negative or not.
@return int256 The 32-byte sign indication (`1`, `0`, or `-1`) of `x`.
"""
return (x ^ 1 < empty(int256))
return unsafe_sub(convert((x > 0), int256), convert((x < 0), int256))


@external
Expand Down
28 changes: 16 additions & 12 deletions test/utils/Math.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,16 @@ contract MathTest is Test {
math.ceil_div(1, 0);
}

function testIsNegative() public {
assertEq(math.is_negative(0), false);
assertEq(math.is_negative(-1), true);
assertEq(math.is_negative(-1 * -1), false);
assertEq(math.is_negative(-1 * 100), true);
assertEq(math.is_negative(0 * -1), false);
assertEq(math.is_negative(int256(type(int16).min) * 2), true);
assertEq(math.is_negative(type(int256).min + type(int16).max), true);
function testSign() public {
assertEq(math.sign(0), 0);
assertEq(math.sign(-1), -1);
assertEq(math.sign(-1 * -1), 1);
assertEq(math.sign(-1 * 100), -1);
assertEq(math.sign(0 * -1), 0);
assertEq(math.sign(int256(type(int16).min) * 2), -1);
assertEq(math.sign(int256(type(int16).max) * 2), 1);
assertEq(math.sign(type(int256).min + type(int16).max), -1);
assertEq(math.sign(type(int256).max - type(int16).max), 1);
}

function testMulDivDivisionByZero() public {
Expand Down Expand Up @@ -482,11 +484,13 @@ contract MathTest is Test {
}
}

function testFuzzIsNegative(int256 x) public {
if (x >= 0) {
assertEq(math.is_negative(x), false);
function testFuzzSign(int256 x) public {
if (x > 0) {
assertEq(math.sign(x), 1);
} else if (x < 0) {
assertEq(math.sign(x), -1);
} else {
assertEq(math.is_negative(x), true);
assertEq(math.sign(x), 0);
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/utils/interfaces/IMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface IMath {

function ceil_div(uint256 x, uint256 y) external pure returns (uint256);

function is_negative(int256 x) external pure returns (bool);
function sign(int256 x) external pure returns (int256);

function mul_div(
uint256 x,
Expand Down
Loading