-
Notifications
You must be signed in to change notification settings - Fork 386
library/math: Added standard math functions #49
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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. | ||
| 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; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks strange, how can There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lol
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think he's asking why the function is called
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be something like |
||
| y = y_a * delta + y_b *delta; | ||
| return x, y; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More formally, the smallest
ysuch thaty * y >= x.