pragma solidity ^0.4.24;

contract setget{
    uint x;
    function setnumber(uint _x) public {
    function getnumber() public view returns(uint){
        return x;
  • 1. There is no question here. 2. There is no demonstration here of what you are describing (nor the negative value neither the error thrown). 3. If you call the function from the off-chain with a negative value, then it will be converted to unsigned in the on-chain side, using 2s-complement. 4. If you call it from the on-chain, then you're likely to get a compilation error, or at least a compilation warning, but in either case - not a runtime exception. – goodvibration Apr 8 '19 at 6:59
  • 5. If you want your code to be displayed properly, then indent it by 4 spaces. – goodvibration Apr 8 '19 at 7:00
  • how to accept only positive values i tried require(_x>0) and assert(_x>0) also... – Rama krishna Manchala Apr 8 '19 at 7:03
  • Like I said, the value will be converted to uint, so you'll need to add this assertion on the caller side, not in this function. Or you can use int instead of uint, and then require(_x>0) will become useful. You'll also need to change x=_x to x=uint(_x). – goodvibration Apr 8 '19 at 7:05
  • thank you.. i added it in the function itself and it worked... but if it accepts negative values then what is the main purpose of introducing uint then.. – Rama krishna Manchala Apr 8 '19 at 7:31

Both, uint and int are 256-bit long in Solidity, so arbitrary sequence of 256 bits may be interpreted in both ways. For example, sequence of 256 "ones" is -1 when interpreted as int, and is 115792089237316195423570985008687907853269984665640564039457584007913129639935 when interpreted as uint.

I assume you are passing negative value to your function via Web3 API. Your Web3 API implementation does not do range check. It just converts your negative number into a 256-bit sequence and passes it to your smart contract, and smart contract just interprets it as uint.

There is no reliable way for smart contract to know whether particular 256-bit sequence passed to it was initially representing int or uint. Though, one thing may probably help. The idea is that positive numbers have the same binary representation for both types, so in case value you got is less than or equals to maximum possible int value, then it has never been negative. So you may add the following line to the beginning of your method:

| improve this answer | |

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.