Skip to content

Commit

Permalink
improve docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Lohann committed Apr 29, 2024
1 parent 07bcce5 commit d513400
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions contracts/utils/math/Math.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ library Math {
*/
function saturatingAdd(uint256 a, uint256 b) internal pure returns (uint256) {
unchecked {
a = a + b;
b = 0 - SafeCast.toUint(a < b);
return a | b;
uint256 c = a + b;
// equivalent to: c < a ? type(uint256).max : c
return c | (0 - SafeCast.toUint(c < a));
}
}

Expand All @@ -45,7 +45,7 @@ library Math {
*/
function saturatingSub(uint256 a, uint256 b) internal pure returns (uint256) {
unchecked {
// equivalent to: a >= b ? a - b : 0
// equivalent to: a > b ? a - b : 0
return (a - b) * SafeCast.toUint(a > b);
}
}
Expand All @@ -56,7 +56,7 @@ library Math {
function trySub(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) {
unchecked {
success = a >= b;
// equivalent to: success ? c : 0
// equivalent to: success ? (a - b) : 0
result = SafeCast.toUint(success) * (a - b);
}
}
Expand Down

0 comments on commit d513400

Please sign in to comment.