From 2a27c59617e57b1bbfc359fe18425c4b3c7364a9 Mon Sep 17 00:00:00 2001 From: Tanisha Chavan <151942801+Tanisha0708@users.noreply.github.com> Date: Mon, 4 Nov 2024 17:27:56 +0530 Subject: [PATCH] Update avg.sol MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit User-Defined Precision for Decimal Places In the original code, the average was calculated and stored as an integer, meaning any decimal part was lost due to Solidity’s lack of native floating-point support. I added a decimals parameter to the function so the user can specify how many decimal places they want in the result. This gives more flexibility, as the user can define the precision they need. 2. Simulating Decimals in Solidity Since Solidity doesn’t support floating-point numbers, I simulated decimals by multiplying the result by 10^decimals. This approach allows storing the average as an integer while retaining precision. For example, if the average is 456 and the user wants four decimal places, the contract now stores 4560000 (which represents 456.0000). This prevents loss of precision that would occur in calculations where the average isn’t a whole number. 3. Formatting Output as a String I added a helper function, getFormattedAvg, to convert the integer result into a string with the specified number of decimal places. This formatting is more user-friendly and visually represents the average with decimals. The function breaks down the avg into its integer and fractional parts based on the user-defined precision, then formats it as "integer.fractional". 4. Improved Precision Control By letting the user define decimals, this contract can avoid excessive precision that isn’t needed, reducing computation complexity. For example, if the user wants only 2 decimal places, there’s no need to calculate 10 or more places, saving gas costs. the user freindly access the avg value at till what he want a answer --- .../Basic/Arithmetic/Avg/avg.sol | 65 +++++++++++++++++-- 1 file changed, 58 insertions(+), 7 deletions(-) diff --git a/Blockchain Development/Basic/Arithmetic/Avg/avg.sol b/Blockchain Development/Basic/Arithmetic/Avg/avg.sol index db014953d..b0e94c0bb 100644 --- a/Blockchain Development/Basic/Arithmetic/Avg/avg.sol +++ b/Blockchain Development/Basic/Arithmetic/Avg/avg.sol @@ -3,12 +3,63 @@ pragma solidity 0.8.0; contract Avg { - // Average of three numbers - int public avg ; + // Average of three numbers with precision handling + int public avg; - function getAvg( int num1 , int num2, int num3 ) public returns( int ){ - int sum = num1 + num2 + num3 ; - avg = sum / 3 ; - return avg ; + function getAvg(int num1, int num2, int num3, uint decimals) public returns (int) { + int sum = num1 + num2 + num3; + + // Calculate average with desired precision + // Multiply by 10^decimals to get the required precision + int precisionMultiplier = int(10 ** decimals); + avg = (sum * precisionMultiplier) / 3; + + return avg; + } + + function getFormattedAvg(uint decimals) public view returns (string memory) { + // Convert the average to string with the specified number of decimal places + int integerPart = avg / int(10 ** decimals); + int fractionalPart = avg % int(10 ** decimals); + + // Format as a string + return string(abi.encodePacked( + intToString(integerPart), + ".", + uintToString(uint(fractionalPart), decimals) + )); + } + + // Helper function to convert integer part to string + function intToString(int _i) internal pure returns (string memory) { + if (_i == 0) return "0"; + bool negative = _i < 0; + uint i = uint(negative ? -_i : _i); + uint j = i; + uint length; + while (j != 0) { + length++; + j /= 10; + } + bytes memory bstr = new bytes(length + (negative ? 1 : 0)); + uint k = bstr.length; + while (i != 0) { + bstr[--k] = bytes1(uint8(48 + i % 10)); + i /= 10; + } + if (negative) { + bstr[0] = '-'; + } + return string(bstr); + } + + // Helper function to convert fractional part to string with fixed decimal places + function uintToString(uint _i, uint decimals) internal pure returns (string memory) { + bytes memory bstr = new bytes(decimals); + for (uint k = decimals; k > 0; k--) { + bstr[k - 1] = bytes1(uint8(48 + _i % 10)); + _i /= 10; + } + return string(bstr); } -} \ No newline at end of file +}