updated math.sol

This commit is contained in:
Thrilok Kumar 2020-05-02 20:03:39 +05:30
parent 10372bbdbd
commit d9a2bf5671

View File

@ -1,6 +1,8 @@
pragma solidity ^0.6.0; pragma solidity ^0.6.0;
contract DSMath { contract DSMath {
uint constant WAD = 10 ** 18;
uint constant RAY = 10 ** 27;
function add(uint x, uint y) internal pure returns (uint z) { function add(uint x, uint y) internal pure returns (uint z) {
require((z = x + y) >= x, "math-not-safe"); require((z = x + y) >= x, "math-not-safe");
@ -10,7 +12,6 @@ contract DSMath {
require(y == 0 || (z = x * y) / y == x, "math-not-safe"); require(y == 0 || (z = x * y) / y == x, "math-not-safe");
} }
uint constant WAD = 10 ** 18;
function wmul(uint x, uint y) internal pure returns (uint z) { function wmul(uint x, uint y) internal pure returns (uint z) {
z = add(mul(x, y), WAD / 2) / WAD; z = add(mul(x, y), WAD / 2) / WAD;
@ -20,4 +21,12 @@ contract DSMath {
z = add(mul(x, WAD), y / 2) / y; z = add(mul(x, WAD), y / 2) / y;
} }
function rdiv(uint x, uint y) internal pure returns (uint z) {
z = add(mul(x, RAY), y / 2) / y;
}
function rmul(uint x, uint y) internal pure returns (uint z) {
z = add(mul(x, y), RAY / 2) / RAY;
}
} }