Skip to content
Permalink
Browse files
address uint(-1) failure case
closes #48
  • Loading branch information
NoahZinsmeister committed Jan 21, 2020
1 parent f04890e commit d1c8612ea3f44eb770012028b174cb63efe4046f
Showing 1 changed file with 1 addition and 1 deletion.
@@ -7,7 +7,7 @@ library Math {

function sqrt(uint y) internal pure returns (uint z) {
if (y > 3) {
uint x = (y + 1) / 2;
uint x = y / 2 + 1;
z = y;
while (x < z) {
z = x;

2 comments on commit d1c8612

@MicahZoltu
Copy link

@MicahZoltu MicahZoltu commented on d1c8612 Jan 22, 2020

Choose a reason for hiding this comment

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

Is there test coverage on this? I'm mildly surprised that (y + 1) / 2 ~= (y / 2) + 1 and it doesn't break some other edge case.

@NoahZinsmeister
Copy link
Member Author

@NoahZinsmeister NoahZinsmeister commented on d1c8612 Jan 22, 2020

Choose a reason for hiding this comment

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

@MicahZoltu it's just a (somewhat arbitrary) starting value. see https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method

in the process of fuzzing this function.

Please sign in to comment.