55 lines
1.5 KiB
Solidity
55 lines
1.5 KiB
Solidity
/*
|
|
|
|
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;
|
|
|
|
uint256 internal constant ONE = 10**18;
|
|
uint256 internal constant ONE2 = 10**36;
|
|
|
|
function mulFloor(uint256 target, uint256 d) internal pure returns (uint256) {
|
|
return target.mul(d) / (10**18);
|
|
}
|
|
|
|
function mulCeil(uint256 target, uint256 d) internal pure returns (uint256) {
|
|
return target.mul(d).divCeil(10**18);
|
|
}
|
|
|
|
function divFloor(uint256 target, uint256 d) internal pure returns (uint256) {
|
|
return target.mul(10**18).div(d);
|
|
}
|
|
|
|
function divCeil(uint256 target, uint256 d) internal pure returns (uint256) {
|
|
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);
|
|
}
|
|
|
|
function multiMulWithDiv(uint256 x, uint256 y, uint256 z) internal pure returns (uint256) {
|
|
uint256 a = x.div(z); uint256 b = x.mod(z); // x = a * z + b
|
|
uint256 c = y.div(z); uint256 d = y.mod(z); // y = c * z + d
|
|
return a.mul(b).mul(z).add(a.mul(d)).add(b.mul(c)).add(b.mul(d).div(z));
|
|
}
|
|
}
|