Files
dodo-contractV2/contracts/lib/DecimalMath.sol

49 lines
1.2 KiB
Solidity
Raw Normal View History

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
}
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-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
}
}