Skip to content

Commit

Permalink
Update contracts/utils/math/SignedMath.sol
Browse files Browse the repository at this point in the history
Co-authored-by: Francisco Giordano <[email protected]>
  • Loading branch information
Amxx and frangio authored Jan 7, 2022
1 parent 39ea6c8 commit 4299249
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions contracts/utils/math/SignedMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ library SignedMath {
}

/**
* @dev Returns the average of two signed numbers. The result is rounded
* towards zero.
* @dev Returns the average of two signed numbers without overflow.
* The result is rounded towards zero.
*/
function average(int256 a, int256 b) internal pure returns (int256) {
// (a + b) / 2 can overflow, and the unsigned formula doesn't simply translate to signed integers
// Formula from the book "Hacker's Delight"
int256 x = (a & b) + ((a ^ b) >> 1);
return x + (int256(uint256(x) >> 255) & (a ^ b));
}
Expand Down

0 comments on commit 4299249

Please sign in to comment.