Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
O
openzeppelin-contracts-upgradeable
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
俞永鹏
openzeppelin-contracts-upgradeable
Commits
d9632fa2
Commit
d9632fa2
authored
Jul 11, 2021
by
github-actions
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Transpile
37a95ee0
parent
3c10a0c3
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
7 additions
and
2 deletions
+7
-2
MathUpgradeable.sol
contracts/utils/math/MathUpgradeable.sol
+2
-2
Math.test.js
test/utils/math/Math.test.js
+5
-0
No files found.
contracts/utils/math/MathUpgradeable.sol
View file @
d9632fa2
...
...
@@ -25,8 +25,8 @@ library MathUpgradeable {
* zero.
*/
function average(uint256 a, uint256 b) internal pure returns (uint256) {
// (a + b) / 2 can overflow
, so we distribute
.
return (a
/ 2) + (b / 2) + (((a % 2) + (b % 2)) / 2)
;
// (a + b) / 2 can overflow.
return (a
& b) + (a ^ b) / 2
;
}
/**
...
...
test/utils/math/Math.test.js
View file @
d9632fa2
...
...
@@ -54,6 +54,11 @@ contract('Math', function (accounts) {
const
b
=
new
BN
(
'84346'
);
expect
(
await
this
.
math
.
average
(
a
,
b
)).
to
.
be
.
bignumber
.
equal
(
bnAverage
(
a
,
b
));
});
it
(
'is correctly calculated with two max uint256 numbers'
,
async
function
()
{
const
a
=
MAX_UINT256
;
expect
(
await
this
.
math
.
average
(
a
,
a
)).
to
.
be
.
bignumber
.
equal
(
bnAverage
(
a
,
a
));
});
});
describe
(
'ceilDiv'
,
function
()
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment