We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 720f4d5 commit 7b92a45Copy full SHA for 7b92a45
contracts/utils/Math.sol
@@ -11,7 +11,7 @@ library Math {
11
*/
12
function average(uint256 a, uint256 b) internal pure returns (uint256) {
13
unchecked {
14
- return (a >> 1) + (b >> 1) + (a & b & 1);
+ return (a & b) + ((a ^ b) >> 1);
15
}
16
17
test/utils/Math.ts
@@ -40,6 +40,13 @@ describe('Math', function () {
40
ethers.constants.MaxUint256,
41
),
42
).to.equal(ethers.constants.MaxUint256);
43
+
44
+ expect(
45
+ await instance.average(
46
+ ethers.constants.MaxUint256,
47
+ ethers.constants.MaxUint256.sub(ethers.constants.One),
48
+ ),
49
+ ).to.equal(ethers.constants.MaxUint256.sub(ethers.constants.One));
50
});
51
52
0 commit comments