[Ethereum] the maximum input value for function uint256 parameter

solidity

folks!

I am new to Solidity, and have a question, that may sound silly, but I can't really figure it out. So, I have this simple function:

function uintTest(uint256 n) returns (uint256) {
        return n;
    }

I thought, the max value for uint256 is 2**256, however, the function does not work for input numbers that have more than 16 digits, and in Remix, throws an "assertion failed" error. I can't understand why.

Best Answer

The max value is 2^256-1. The assertion you're hitting is in Remix. The full error message is:

Error encoding arguments: Error: Assertion failed

The issue is that Remix uses JSON to parse the argument list, and JavaScript only supports numbers up to 2^53-1. The reason you're seeing the issue where you do in terms of number of 9s is because

999999999999999 < 2**53-1 < 9999999999999999

To fix the issue, just put double quotes around the number you're passing in. It's safe to just always wrap your numbers in quotes to avoid such precision issues.

EDIT:

In version ^0.8.0 of solidity, you can get the max value like so:

function getMaxUint() public pure returns(uint256){
        unchecked{
            return uint256(0) - 1;
        }
    }
Related Topic