0

In JavaScript, there is "Math.abs":

enter image description here

Is there a similar function in solidity itself or some other popular library such as SafeMath?

1

It's a bit funny that the Solidity documentation hints about such a function abs (https://solidity.readthedocs.io/en/v0.5.3/types.html) but I can't get it to compile at least in Remix. So I assume there is no such function and the documentation is just a bit off (wouldn't be the first time).

There are probably some libraries which contain that functionality but no idea which and where.

Anyway, it's easy to enough to implement it yourself - you gave the formula already :)

Note that you can't just cast int into uint and be done with it - it's not allowed. And even if it was, -1 should probably cast into Max(uint) - 1.

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.