mirror of
https://github.com/Instadapp/Gelato-automations.git
synced 2024-07-29 22:28:07 +00:00
96 lines
2.8 KiB
Solidity
96 lines
2.8 KiB
Solidity
// "SPDX-License-Identifier: AGPL-3.0-or-later"
|
|
/// math.sol -- mixin for inline numerical wizardry
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
pragma solidity 0.7.4;
|
|
|
|
function add(uint256 x, uint256 y) pure returns (uint256 z) {
|
|
require((z = x + y) >= x, "ds-math-add-overflow");
|
|
}
|
|
|
|
function sub(uint256 x, uint256 y) pure returns (uint256 z) {
|
|
require((z = x - y) <= x, "ds-math-sub-underflow");
|
|
}
|
|
|
|
function mul(uint256 x, uint256 y) pure returns (uint256 z) {
|
|
require(y == 0 || (z = x * y) / y == x, "ds-math-mul-overflow");
|
|
}
|
|
|
|
function min(uint256 x, uint256 y) pure returns (uint256 z) {
|
|
return x <= y ? x : y;
|
|
}
|
|
|
|
function max(uint256 x, uint256 y) pure returns (uint256 z) {
|
|
return x >= y ? x : y;
|
|
}
|
|
|
|
function imin(int256 x, int256 y) pure returns (int256 z) {
|
|
return x <= y ? x : y;
|
|
}
|
|
|
|
function imax(int256 x, int256 y) pure returns (int256 z) {
|
|
return x >= y ? x : y;
|
|
}
|
|
|
|
uint256 constant WAD = 10**18;
|
|
uint256 constant RAY = 10**27;
|
|
|
|
//rounds to zero if x*y < WAD / 2
|
|
function wmul(uint256 x, uint256 y) pure returns (uint256 z) {
|
|
z = add(mul(x, y), WAD / 2) / WAD;
|
|
}
|
|
|
|
//rounds to zero if x*y < WAD / 2
|
|
function rmul(uint256 x, uint256 y) pure returns (uint256 z) {
|
|
z = add(mul(x, y), RAY / 2) / RAY;
|
|
}
|
|
|
|
//rounds to zero if x*y < WAD / 2
|
|
function wdiv(uint256 x, uint256 y) pure returns (uint256 z) {
|
|
z = add(mul(x, WAD), y / 2) / y;
|
|
}
|
|
|
|
//rounds to zero if x*y < RAY / 2
|
|
function rdiv(uint256 x, uint256 y) pure returns (uint256 z) {
|
|
z = add(mul(x, RAY), y / 2) / y;
|
|
}
|
|
|
|
// This famous algorithm is called "exponentiation by squaring"
|
|
// and calculates x^n with x as fixed-point and n as regular unsigned.
|
|
//
|
|
// It's O(log n), instead of O(n) for naive repeated multiplication.
|
|
//
|
|
// These facts are why it works:
|
|
//
|
|
// If n is even, then x^n = (x^2)^(n/2).
|
|
// If n is odd, then x^n = x * x^(n-1),
|
|
// and applying the equation for even x gives
|
|
// x^n = x * (x^2)^((n-1) / 2).
|
|
//
|
|
// Also, EVM division is flooring and
|
|
// floor[(n-1) / 2] = floor[n / 2].
|
|
//
|
|
function rpow(uint256 x, uint256 n) pure returns (uint256 z) {
|
|
z = n % 2 != 0 ? x : RAY;
|
|
|
|
for (n /= 2; n != 0; n /= 2) {
|
|
x = rmul(x, x);
|
|
|
|
if (n % 2 != 0) {
|
|
z = rmul(z, x);
|
|
}
|
|
}
|
|
}
|