tweak and add explanatory comments to libraries
This commit is contained in:
@ -1,14 +1,17 @@
|
||||
pragma solidity =0.5.16;
|
||||
|
||||
// a library for performing various math operations
|
||||
|
||||
library Math {
|
||||
function min(uint x, uint y) internal pure returns (uint z) {
|
||||
z = x <= y ? x : y;
|
||||
z = x < y ? x : y;
|
||||
}
|
||||
|
||||
// babylonian method (https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method)
|
||||
function sqrt(uint y) internal pure returns (uint z) {
|
||||
if (y > 3) {
|
||||
uint x = y / 2 + 1;
|
||||
z = y;
|
||||
uint x = y / 2 + 1;
|
||||
while (x < z) {
|
||||
z = x;
|
||||
x = (y / x + x) / 2;
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
pragma solidity =0.5.16;
|
||||
|
||||
// a library for performing overflow-safe math, courtesy of DappHub (https://github.com/dapphub/ds-math)
|
||||
|
||||
library SafeMath {
|
||||
function add(uint x, uint y) internal pure returns (uint z) {
|
||||
require((z = x + y) >= x, "ds-math-add-overflow");
|
||||
|
||||
@ -1,15 +1,20 @@
|
||||
pragma solidity =0.5.16;
|
||||
|
||||
// a library for handling binary fixed point numbers (https://en.wikipedia.org/wiki/Q_(number_format))
|
||||
|
||||
// range: [0, 2**112 - 1] (theoretical upper bound of 2**112 - (1 / 2**112) does not apply in this setting)
|
||||
// resolution: 1 / 2**112
|
||||
|
||||
library UQ112x112 {
|
||||
uint224 constant Q112 = 2**112;
|
||||
|
||||
// encode a uint112 as a UQ112.112 fixed point number s.t. `y := z / 2**112`
|
||||
// encode a uint112 as a UQ112x112
|
||||
function encode(uint112 y) internal pure returns (uint224 z) {
|
||||
z = uint224(y) * Q112; // never overflows
|
||||
}
|
||||
|
||||
// divide a UQ112.112 by a uint112 and return the result as a UQ112.112
|
||||
function qdiv(uint224 x, uint112 y) internal pure returns (uint224 z) {
|
||||
// divide a UQ112x112 by a uint112, returning a UQ112.112
|
||||
function uqdiv(uint224 x, uint112 y) internal pure returns (uint224 z) {
|
||||
z = x / uint224(y);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user