Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Math.average #1170

Merged
merged 2 commits into from Aug 13, 2018
Merged

Math.average #1170

merged 2 commits into from Aug 13, 2018

Conversation

nventuro
Copy link
Contributor

@nventuro nventuro commented Aug 8, 2018

The need for an average function has come up a couple times.

I also wanted to try out the new assert semantics as described in #1120.

@nventuro nventuro added feature New contracts, functions, or helpers. contracts Smart contract code. labels Aug 8, 2018
@nventuro nventuro requested a review from frangio August 8, 2018 15:24
function average(uint256 _a, uint256 _b) internal pure returns (uint256) {
// (_a + _b) / 2 can overflow, so we distribute
uint256 c = (_a / 2) + (_b / 2) + ((_a % 2 + _b % 2) / 2);
assert((_a <= c && c <= _b) || (_a >= c && c >= _b));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@leonardoalt how do you feel about these sort of assertions? Do you consider them necessary and correct, or do you think they may add too much unnecessary overhead (and gas costs)?

Copy link
Contributor

@frangio frangio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the assertion unnecessary and somewhat arbitrary, as there are other conditions that should be checked in order to verify the correctness of the operation.

The implementation is good. Perhaps we should consider putting the operation in SafeMath, given that it does have overflow protection. What do you think?

@nventuro
Copy link
Contributor Author

nventuro commented Aug 8, 2018

I've always understood SafeMath as 'safe implementations of operations already available (e.g +, -, etc.)', so I wouldn't place it there.

@frangio frangio assigned frangio and unassigned frangio Aug 8, 2018
@nventuro nventuro self-assigned this Aug 11, 2018
@nventuro
Copy link
Contributor Author

I've removed the assertion, since we don't yet have a criteria for this kind of stuff. Maybe once we get a FV module?

@nventuro nventuro merged commit d51e387 into OpenZeppelin:master Aug 13, 2018
@nventuro nventuro deleted the average branch August 13, 2018 23:07
@leonardoalt
Copy link
Contributor

@nventuro I agree with @frangio and you on the assertion removal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contracts Smart contract code. feature New contracts, functions, or helpers.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants