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

Fix incorrect comment 36 #61

Merged
merged 3 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 3 additions & 3 deletions src/libraries/MathLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ library MathLib {
unchecked {
// Revert if x > ln(2^256-1) ~ 177.
require(x <= 177.44567822334599921 ether, ErrorsLib.WEXP_OVERFLOW);
// Return zero if x < -(2**255-1) + (ln(2)/2).
// Return zero if x < -2**255 + (ln(2)/2).
MathisGD marked this conversation as resolved.
Show resolved Hide resolved
MathisGD marked this conversation as resolved.
Show resolved Hide resolved
MathisGD marked this conversation as resolved.
Show resolved Hide resolved
if (x < type(int256).min + LN2_INT / 2) return 0;

// Decompose x as x = q * ln(2) + r with q an integer and -ln(2)/2 <= r <= ln(2)/2.
// q = x / ln(2) rounded half toward zero.
int256 roundingAdjustment = (x < 0) ? -(LN2_INT / 2) : (LN2_INT / 2);
// Safe unchecked because x is bounded.
int256 q = (x + roundingAdjustment) / LN2_INT;
// Safe unchecked because |q * LN2_INT| <= |x|.
// Safe unchecked because |q * LN2_INT - x| <= LN2_INT/2.
int256 r = x - q * LN2_INT;

// Compute e^r with a 2nd-order Taylor polynomial.
// Safe unchecked because |r| < 1, expR < 2 and the sum is positive.
// Safe unchecked because |r| < 1e18, and the sum is positive.
uint256 expR = uint256(WAD_INT + r + (r * r) / WAD_INT / 2);

// Return e^x = 2^q * e^r.
Expand Down
4 changes: 2 additions & 2 deletions test/MathLibTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ contract MathLibTest is Test {
}

function testWExpSmall(int256 x) public {
// Bound between -(2**255-1) + ln(2)/2 and ln(1e-18).
// Bound between -2**255 + ln(2)/2 and ln(1e-18).
x = bound(x, type(int256).min + LN2_INT / 2, -178 ether);
assertEq(MathLib.wExp(x), 0);
}

function testWExpTooSmall(int256 x) public {
// Bound between -(2**255-1) and -(2**255-1) + ln(2)/2 - 1.
// Bound between -2**255 and -2**255 + ln(2)/2 - 1.
x = bound(x, type(int256).min, type(int256).min + LN2_INT / 2 - 1);
assertEq(MathLib.wExp(x), 0);
}
Expand Down