github.com/ethereum-optimism/optimism@v1.7.2/packages/contracts-bedrock/src/libraries/Arithmetic.sol (about)

     1  // SPDX-License-Identifier: MIT
     2  pragma solidity ^0.8.0;
     3  
     4  import { SignedMath } from "@openzeppelin/contracts/utils/math/SignedMath.sol";
     5  import { FixedPointMathLib } from "@rari-capital/solmate/src/utils/FixedPointMathLib.sol";
     6  
     7  /// @title Arithmetic
     8  /// @notice Even more math than before.
     9  library Arithmetic {
    10      /// @notice Clamps a value between a minimum and maximum.
    11      /// @param _value The value to clamp.
    12      /// @param _min   The minimum value.
    13      /// @param _max   The maximum value.
    14      /// @return The clamped value.
    15      function clamp(int256 _value, int256 _min, int256 _max) internal pure returns (int256) {
    16          return SignedMath.min(SignedMath.max(_value, _min), _max);
    17      }
    18  
    19      /// @notice (c)oefficient (d)enominator (exp)onentiation function.
    20      ///         Returns the result of: c * (1 - 1/d)^exp.
    21      /// @param _coefficient Coefficient of the function.
    22      /// @param _denominator Fractional denominator.
    23      /// @param _exponent    Power function exponent.
    24      /// @return Result of c * (1 - 1/d)^exp.
    25      function cdexp(int256 _coefficient, int256 _denominator, int256 _exponent) internal pure returns (int256) {
    26          return (_coefficient * (FixedPointMathLib.powWad(1e18 - (1e18 / _denominator), _exponent * 1e18))) / 1e18;
    27      }
    28  }