-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathNumberMath.sol
48 lines (43 loc) · 1.44 KB
/
NumberMath.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
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.13;
import "@openzeppelin/contracts/utils/math/Math.sol";
import "@openzeppelin/contracts/utils/math/SignedMath.sol";
/**
* @title NumberMath
* @notice Library for additional math functions that are not included in the OpenZeppelin libraries.
*/
library NumberMath {
error DivisionByZero();
/**
* @notice Divides `a` by `b`, rounding the result away from zero if there is a remainder
* @param a Dividend
* @param b Divisor
* @return Resulting quotient
*/
function divOut(uint256 a, uint256 b) internal pure returns (uint256) {
if (b == 0) revert DivisionByZero();
return Math.ceilDiv(a, b);
}
/**
* @notice Divides `a` by `b`, rounding the result away from zero if there is a remainder
* @param a Dividend
* @param b Divisor
* @return Resulting quotient
*/
function divOut(int256 a, int256 b) internal pure returns (int256) {
return sign(a) * sign(b) * int256(divOut(SignedMath.abs(a), SignedMath.abs(b)));
}
/**
* @notice Returns the sign of an int256
* @dev Returns: -1 for negative
* 0 for zero
* 1 for positive
* @param a int256 to find the sign of
* @return Sign of the int256
*/
function sign(int256 a) internal pure returns (int256) {
if (a > 0) return 1;
if (a < 0) return -1;
return 0;
}
}