From 95ac512ff57dc8aaddebb498dddbc814593526c8 Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Sun, 20 Oct 2024 11:44:33 +0700 Subject: [PATCH] Create MathLib.sol --- .../contracts/libraries/MathLib.sol | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 blockchain_integration/pi_network/pi-network-layer2-scaling/contracts/libraries/MathLib.sol diff --git a/blockchain_integration/pi_network/pi-network-layer2-scaling/contracts/libraries/MathLib.sol b/blockchain_integration/pi_network/pi-network-layer2-scaling/contracts/libraries/MathLib.sol new file mode 100644 index 000000000..c4d717a81 --- /dev/null +++ b/blockchain_integration/pi_network/pi-network-layer2-scaling/contracts/libraries/MathLib.sol @@ -0,0 +1,46 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +library MathLib { + // Safe addition + function safeAdd(uint256 a, uint256 b) internal pure returns (uint256) { + require(a + b >= a, "MathLib: addition overflow"); + return a + b; + } + + // Safe subtraction + function safeSub(uint256 a, uint256 b) internal pure returns (uint256) { + require(b <= a, "MathLib: subtraction underflow"); + return a - b; + } + + // Safe multiplication + function safeMul(uint256 a, uint256 b) internal pure returns (uint256) { + if (a == 0) { + return 0; + } + require(a * b / a == b, "MathLib: multiplication overflow"); + return a * b; + } + + // Safe division + function safeDiv(uint256 a, uint256 b) internal pure returns (uint256) { + require(b > 0, "MathLib: division by zero"); + return a / b; + } + + // Calculate the average of two numbers + function average(uint256 a, uint256 b) internal pure returns (uint256) { + return safeDiv(safeAdd(a, b), 2); + } + + // Calculate the maximum of two numbers + function max(uint256 a, uint256 b) internal pure returns (uint256) { + return a >= b ? a : b; + } + + // Calculate the minimum of two numbers + function min(uint256 a, uint256 b) internal pure returns (uint256) { + return a < b ? a : b; + } +}