This repository has been archived by the owner on May 15, 2021. It is now read-only.
/
FixedPoint.sol
93 lines (80 loc) · 2.98 KB
/
FixedPoint.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
Copyright 2020 PoolTogether Inc.
This file is part of PoolTogether.
PoolTogether 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 under version 3 of the License.
PoolTogether 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 PoolTogether. If not, see <https://www.gnu.org/licenses/>.
*/
pragma solidity 0.5.12;
import "@openzeppelin/contracts-ethereum-package/contracts/math/SafeMath.sol";
/**
* @author Brendan Asselstine
* @notice Provides basic fixed point math calculations.
*
* This library calculates integer fractions by scaling values by 1e18 then performing standard integer math.
*/
library FixedPoint {
using SafeMath for uint256;
// The scale to use for fixed point numbers. Same as Ether for simplicity.
uint256 public constant SCALE = 1e18;
/**
* A struct representing a fixed point 18 number (ie Ether).
*/
struct Fixed18 {
uint256 mantissa;
}
/**
* Calculates a Fixed18 mantissa given the numerator and denominator
*
* The mantissa = (numerator * 1e18) / denominator
*
* @param numerator The mantissa numerator
* @param denominator The mantissa denominator
* @return The mantissa of the fraction
*/
function calculateMantissa(uint256 numerator, uint256 denominator) public pure returns (uint256) {
uint256 mantissa = numerator.mul(SCALE);
mantissa = mantissa.div(denominator);
return mantissa;
}
/**
* Multiplies a Fixed18 number by an integer.
*
* @param f The Fixed18 number
* @param b The whole integer to multiply
* @return An integer that is the result of multiplying the params.
*/
function multiplyUint(Fixed18 storage f, uint256 b) public view returns (uint256) {
uint256 result = f.mantissa.mul(b);
result = result.div(SCALE);
return result;
}
/**
* Divides an integer by a Fixed18 number.
*
* @param dividend The integer to divide
* @param divisor The Fixed18 number to act as the divisor
* @return An integer that is the result of dividing an integer by a Fixed18 number
*/
function divideUintByFixed(uint256 dividend, Fixed18 storage divisor) public view returns (uint256) {
return divideUintByMantissa(dividend, divisor.mantissa);
}
/**
* Divides an integer by a fixed point 18 mantissa
*
* @param dividend The integer to divide
* @param mantissa The fixed point 18 number to serve as the divisor
* @return An integer that is the result of dividing an integer by a fixed point 18 mantissa
*/
function divideUintByMantissa(uint256 dividend, uint256 mantissa) public pure returns (uint256) {
uint256 result = SCALE.mul(dividend);
result = result.div(mantissa);
return result;
}
}