Skip to content

Commit 7b92a45

Browse files
committed
import Math#average implementation from OZ, add additional test case
1 parent 720f4d5 commit 7b92a45

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

contracts/utils/Math.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ library Math {
1111
*/
1212
function average(uint256 a, uint256 b) internal pure returns (uint256) {
1313
unchecked {
14-
return (a >> 1) + (b >> 1) + (a & b & 1);
14+
return (a & b) + ((a ^ b) >> 1);
1515
}
1616
}
1717

test/utils/Math.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ describe('Math', function () {
4040
ethers.constants.MaxUint256,
4141
),
4242
).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));
4350
});
4451
});
4552

0 commit comments

Comments
 (0)