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
d51e3875
Unverified
Commit
d51e3875
authored
Aug 13, 2018
by
Nicolás Venturo
Committed by
GitHub
Aug 13, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Math.average (#1170)
* Added Math.average * Removed assertion.
parent
e493d2a6
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
33 additions
and
0 deletions
+33
-0
Math.sol
contracts/math/Math.sol
+5
-0
MathMock.sol
contracts/mocks/MathMock.sol
+4
-0
Math.test.js
test/library/Math.test.js
+24
-0
No files found.
contracts/math/Math.sol
View file @
d51e3875
...
...
@@ -13,4 +13,9 @@ library Math {
function min(uint256 _a, uint256 _b) internal pure returns (uint256) {
return _a < _b ? _a : _b;
}
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);
}
}
contracts/mocks/MathMock.sol
View file @
d51e3875
...
...
@@ -12,4 +12,8 @@ contract MathMock {
function min(uint256 _a, uint256 _b) public pure returns (uint256) {
return Math.min(_a, _b);
}
function average(uint256 _a, uint256 _b) public pure returns (uint256) {
return Math.average(_a, _b);
}
}
test/library/Math.test.js
View file @
d51e3875
...
...
@@ -37,4 +37,28 @@ contract('Math', function () {
result
.
should
.
be
.
bignumber
.
equal
(
min
);
});
});
describe
(
'average'
,
function
()
{
function
bnAverage
(
a
,
b
)
{
return
a
.
plus
(
b
).
div
(
2
).
truncated
();
}
it
(
'is correctly calculated with two odd numbers'
,
async
function
()
{
const
a
=
new
BigNumber
(
57417
);
const
b
=
new
BigNumber
(
95431
);
(
await
this
.
math
.
average
(
a
,
b
)).
should
.
be
.
bignumber
.
equal
(
bnAverage
(
a
,
b
));
});
it
(
'is correctly calculated with two even numbers'
,
async
function
()
{
const
a
=
new
BigNumber
(
42304
);
const
b
=
new
BigNumber
(
84346
);
(
await
this
.
math
.
average
(
a
,
b
)).
should
.
be
.
bignumber
.
equal
(
bnAverage
(
a
,
b
));
});
it
(
'is correctly calculated with one even and one odd number'
,
async
function
()
{
const
a
=
new
BigNumber
(
57417
);
const
b
=
new
BigNumber
(
84346
);
(
await
this
.
math
.
average
(
a
,
b
)).
should
.
be
.
bignumber
.
equal
(
bnAverage
(
a
,
b
));
});
});
});
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