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

library/math: Added standard math functions #49

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions library/math.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/// Standard math library.
contract Math {
/// @dev Returns the square root of x.
Copy link
Contributor

Choose a reason for hiding this comment

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

More formally, the smallest y such that y * y >= x.

function sqrt(uint x) returns (uint) {
uint y = x;
while( true ) {
uint z = (y + (x/y))/2;
uint w = (z + (x/z))/2;
if( w == y) {
if( w < y ) return w;
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks strange, how can w < y here?

Choose a reason for hiding this comment

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

lol

Copy link
Author

Choose a reason for hiding this comment

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

Hehe oops

else return y;
}
y = w;
}
}

/// @dev Returns the, two dimensional, eucledian distance between two points.
function 2dEucDist(uint x_a, uint y_a, uint x_b, uint y_b) returns (uint) {
return sqrt((x_a - y_b) ** 2 + (y_a, - x_b) ** 2);
}

/// @dev Returns the linear interpolation between a and b
function lerp(uint x_a, uint y_a, uint x_b, uint y_b, uint delta) returns (uint x, uint y) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why lerp?

Copy link
Author

Choose a reason for hiding this comment

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

Not sure what you mean with why?

Are you asking why I needed it here or are you asking about the reasoning behind adding it to a math lib?

Choose a reason for hiding this comment

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

I think he's asking why the function is called lerp. FYI lerp is the canonical shortened name (I believe) for the Linear Interpolation function.

Copy link
Author

Choose a reason for hiding this comment

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

Yes I see now. I should have paid closer attention to the implementation when asked to rename the functions.

x = x_a * delta + x_b * delta;
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this be something like x = x_a * delta + x_b * (1 - delta) (of course, delta should be a fractional type for this to work)?

y = y_a * delta + y_b *delta;
return x, y;
}
}