3

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.

5

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.

2

The reason 2^256 is not the correct number is that one of the possible values needs to be reserved for the zero value.

Imagine a 2 bit number - the max value is not 2^2 (= four) but rather 2^2-1 (= three). Brute forcing it:

00 = zero 01 = one 10 = two 11 = three

There are no more possible combinations of 2 bits - so there's no way to express the number four.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.