diff --git a/contracts/libraries/Math.sol b/contracts/libraries/Math.sol index f30240d..1cab10d 100644 --- a/contracts/libraries/Math.sol +++ b/contracts/libraries/Math.sol @@ -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; diff --git a/contracts/libraries/SafeMath.sol b/contracts/libraries/SafeMath.sol index e2b9912..68c1d9c 100644 --- a/contracts/libraries/SafeMath.sol +++ b/contracts/libraries/SafeMath.sol @@ -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"); diff --git a/contracts/libraries/UQ112x112.sol b/contracts/libraries/UQ112x112.sol index 5d20019..234cad3 100644 --- a/contracts/libraries/UQ112x112.sol +++ b/contracts/libraries/UQ112x112.sol @@ -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); } }