2020-06-26 00:31:25 +08:00
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
Copyright 2020 DODO ZOO.
|
|
|
|
|
SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
pragma solidity 0.6.9;
|
|
|
|
|
pragma experimental ABIEncoderV2;
|
|
|
|
|
|
|
|
|
|
import {SafeMath} from "./SafeMath.sol";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @title DecimalMath
|
|
|
|
|
* @author DODO Breeder
|
|
|
|
|
*
|
|
|
|
|
* @notice Functions for fixed point number with 18 decimals
|
|
|
|
|
*/
|
|
|
|
|
library DecimalMath {
|
|
|
|
|
using SafeMath for uint256;
|
|
|
|
|
|
2020-11-30 16:08:11 +08:00
|
|
|
uint256 internal constant ONE = 10**18;
|
|
|
|
|
uint256 internal constant ONE2 = 10**36;
|
2020-06-26 00:31:25 +08:00
|
|
|
|
2020-10-23 01:16:52 +08:00
|
|
|
function mulFloor(uint256 target, uint256 d) internal pure returns (uint256) {
|
2020-11-22 13:18:44 +08:00
|
|
|
return target.mul(d) / (10**18);
|
2020-10-23 01:16:52 +08:00
|
|
|
}
|
|
|
|
|
|
2020-09-25 10:51:03 +08:00
|
|
|
function mulCeil(uint256 target, uint256 d) internal pure returns (uint256) {
|
2020-11-22 13:18:44 +08:00
|
|
|
return target.mul(d).divCeil(10**18);
|
2020-09-25 10:51:03 +08:00
|
|
|
}
|
|
|
|
|
|
2020-06-26 00:31:25 +08:00
|
|
|
function divFloor(uint256 target, uint256 d) internal pure returns (uint256) {
|
2020-11-22 13:18:44 +08:00
|
|
|
return target.mul(10**18).div(d);
|
2020-06-26 00:31:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function divCeil(uint256 target, uint256 d) internal pure returns (uint256) {
|
2020-11-22 13:18:44 +08:00
|
|
|
return target.mul(10**18).divCeil(d);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function reciprocalFloor(uint256 target) internal pure returns (uint256) {
|
|
|
|
|
return uint256(10**36).div(target);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function reciprocalCeil(uint256 target) internal pure returns (uint256) {
|
|
|
|
|
return uint256(10**36).divCeil(target);
|
2020-06-26 00:31:25 +08:00
|
|
|
}
|
2021-09-09 19:29:50 +08:00
|
|
|
|
|
|
|
|
function powFloor(uint256 target, uint256 e) internal pure returns (uint256) {
|
|
|
|
|
if (e == 0) {
|
2021-09-20 14:52:23 +08:00
|
|
|
return 10 ** 18;
|
2021-09-09 19:29:50 +08:00
|
|
|
} else if (e == 1) {
|
|
|
|
|
return target;
|
|
|
|
|
} else {
|
|
|
|
|
uint p = powFloor(target, e.div(2));
|
|
|
|
|
p = p.mul(p) / (10**18);
|
|
|
|
|
if (e % 2 == 1) {
|
|
|
|
|
p = p.mul(target) / (10**18);
|
|
|
|
|
}
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-26 00:31:25 +08:00
|
|
|
}
|