dsa-connectors-old/contracts/common/math.sol

36 lines
975 B
Solidity
Raw Normal View History

2020-05-02 09:40:50 +00:00
pragma solidity ^0.6.0;
contract DSMath {
2020-05-02 14:33:39 +00:00
uint constant WAD = 10 ** 18;
uint constant RAY = 10 ** 27;
2020-05-02 09:40:50 +00:00
function add(uint x, uint y) internal pure returns (uint z) {
require((z = x + y) >= x, "math-not-safe");
}
2020-05-05 01:54:10 +00:00
function sub(uint x, uint y) internal pure returns (uint z) {
require((z = x - y) <= x, "ds-math-sub-underflow");
}
2020-05-02 09:40:50 +00:00
function mul(uint x, uint y) internal pure returns (uint z) {
require(y == 0 || (z = x * y) / y == x, "math-not-safe");
}
function wmul(uint x, uint y) internal pure returns (uint z) {
z = add(mul(x, y), WAD / 2) / WAD;
}
function wdiv(uint x, uint y) internal pure returns (uint z) {
z = add(mul(x, WAD), y / 2) / y;
}
2020-05-02 14:33:39 +00:00
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;
}
2020-05-02 09:40:50 +00:00
}