Update Math `try*` operations return reference (#4775)

pull/4728/head
Ernesto García 1 year ago committed by GitHub
parent 552cffde56
commit ef699fa6a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 20
      contracts/utils/math/Math.sol
  2. 8
      docs/modules/ROOT/pages/utilities.adoc

@ -20,9 +20,9 @@ library Math {
}
/**
* @dev Returns the addition of two unsigned integers, with an overflow flag.
* @dev Returns the addition of two unsigned integers, with an success flag (no overflow).
*/
function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
function tryAdd(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) {
unchecked {
uint256 c = a + b;
if (c < a) return (false, 0);
@ -31,9 +31,9 @@ library Math {
}
/**
* @dev Returns the subtraction of two unsigned integers, with an overflow flag.
* @dev Returns the subtraction of two unsigned integers, with an success flag (no overflow).
*/
function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
function trySub(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) {
unchecked {
if (b > a) return (false, 0);
return (true, a - b);
@ -41,9 +41,9 @@ library Math {
}
/**
* @dev Returns the multiplication of two unsigned integers, with an overflow flag.
* @dev Returns the multiplication of two unsigned integers, with an success flag (no overflow).
*/
function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
function tryMul(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) {
unchecked {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
@ -56,9 +56,9 @@ library Math {
}
/**
* @dev Returns the division of two unsigned integers, with a division by zero flag.
* @dev Returns the division of two unsigned integers, with a success flag (no division by zero).
*/
function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
function tryDiv(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) {
unchecked {
if (b == 0) return (false, 0);
return (true, a / b);
@ -66,9 +66,9 @@ library Math {
}
/**
* @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag.
* @dev Returns the remainder of dividing two unsigned integers, with a success flag (no division by zero).
*/
function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
function tryMod(uint256 a, uint256 b) internal pure returns (bool success, uint256 result) {
unchecked {
if (b == 0) return (false, 0);
return (true, a % b);

@ -82,10 +82,10 @@ contract MyContract {
using SignedMath for int256;
function tryOperations(uint256 a, uint256 b) internal pure {
(bool overflowsAdd, uint256 resultAdd) = x.tryAdd(y);
(bool overflowsSub, uint256 resultSub) = x.trySub(y);
(bool overflowsMul, uint256 resultMul) = x.tryMul(y);
(bool overflowsDiv, uint256 resultDiv) = x.tryDiv(y);
(bool succededAdd, uint256 resultAdd) = x.tryAdd(y);
(bool succededSub, uint256 resultSub) = x.trySub(y);
(bool succededMul, uint256 resultMul) = x.tryMul(y);
(bool succededDiv, uint256 resultDiv) = x.tryDiv(y);
// ...
}

Loading…
Cancel
Save