Skip to content

Commit

Permalink
refactor: apply naming suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
MerlinEgalite committed Nov 9, 2023
1 parent 70f15f7 commit a2a4042
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
14 changes: 7 additions & 7 deletions src/libraries/MathLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ library MathLib {
/// @dev ln(2).
int256 internal constant LN2_INT = 0.693147180559945309 ether;

/// @dev Above this limit `wExp` would overflow. Instead, we return a capped value.
int256 internal constant UPPER_LIMIT = 135.999582271169154765 ether;
/// @dev Above this limit `wExp` would overflow. Instead, we return an upper value.
int256 internal constant WEXP_UPPER_BOUND = 135.999582271169154765 ether;

/// @dev The value of wExp(UPPER_LIMIT).
uint256 internal constant CAPPED_VALUE =
115792089237316195323137357242501015631897353894317901381819896896488577433600;
/// @dev The value of wExp(WEXP_UPPER_BOUND).
uint256 internal constant WEXP_UPPER_VALUE =
115792089237316195323137357242501015631897353894317901381819.896896488577433600 ether;

/// @dev Returns an approximation of exp.
function wExp(int256 x) internal pure returns (uint256) {
unchecked {
// Return a capped value to avoid overflows.
if (x >= UPPER_LIMIT) return CAPPED_VALUE;
// Return an upper value to avoid overflows.
if (x >= WEXP_UPPER_BOUND) return WEXP_UPPER_VALUE;

// Return zero if x < -(2**255-1) + (ln(2)/2).
if (x < type(int256).min + LN2_INT / 2) return 0;
Expand Down
4 changes: 2 additions & 2 deletions test/MathLibTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ contract MathLibTest is Test {
}

function testWExpTooLarge(int256 x) public {
x = bound(x, MathLib.UPPER_LIMIT, type(int256).max);
assertEq(MathLib.wExp(x), MathLib.CAPPED_VALUE);
x = bound(x, MathLib.WEXP_UPPER_BOUND, type(int256).max);
assertEq(MathLib.wExp(x), MathLib.WEXP_UPPER_VALUE);
}
}

0 comments on commit a2a4042

Please sign in to comment.