2

Does Solidity support signed integer and negative input arguments or would require comparisons with 0 be meaningless?

E.g

function transfer(address _to, uint256 _amount) public returns (bool success) {
        require( _to != 0x0);
        require(balances[msg.sender] >= _amount && _amount >= 0);
        balances[msg.sender] = (balances[msg.sender]).sub(_amount);
        balances[_to] = (balances[_to]).add(_amount);
        Transfer(msg.sender, _to, _amount);
             return true;
         }
5

Yes, Solidity supports signed integers, e.g. int256. Here's some code you can try in Remix to prove to yourself that it works as expected:

pragma solidity ^0.4.20;

contract Test {
    function isNegative(int256 x) public pure returns (bool) {
        return x < 0;
    }
}
  • in regards to the respected transfer & transferFrom functions, would it make sense to check for amount >= 0? I've always read that it would be meaningless – NowsyMe Feb 22 '18 at 7:54
  • It depends whether they accept signed or unsigned integers. The ERC20 token standard dictates that they use unsigned integers (uint256), so checking for a negative number would make no sense. – user19510 Feb 22 '18 at 7:55
  • So the erc20 token standard does actually not support signed ints however the EVM does. Is this how It should be understood? Allowing signed integers would therefore mean a token would not be erc20 compliant – NowsyMe Feb 22 '18 at 8:30
  • 1
    Underflow what if you removed what check? I'm a little lost. I haven't seen any actual code except what I put in my answer (which does no arithmetic and thus can't overflow or underflow). – user19510 Feb 22 '18 at 11:31
  • 1
    The amount >= 0 check there is indeed meaningless. It can never be false, because amount is of type uint256. – user19510 Feb 22 '18 at 23:44

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.