0

need to verify something, suppose i have this:

uint256 a =10;
uint256 b = 20;
uint256 c;
if (b > a) {
 c = b-a;
 (-c.mul(10))  // i want to multiply with -c intentionally
}

this -c gonna give error, because uint256 can't be negetive. Can i do like this here?

uint256 a =10;
uint256 b = 20;
uint256 c;
if (b > a) {
  c = b-a;
  -int256(c);
  (c.mul(10));    // Now will c become negetive here?
}

Will -int256(c)` work??

And if i do (0 - c).mul(10), will it work?

N.B: avoiding typecasting because i will have o change my code broadly.

3
  • why are u using safe math?? its unnecessary with in solidity 8+
    – Matt Jaf
    Commented Apr 27, 2023 at 1:22
  • What should I use then ?Normal operators like * and /?
    – Hacek
    Commented Apr 27, 2023 at 7:24
  • Yes you can securely use normal operators with solidity 8+, it has it built in. The EVM will error before it under or overflows. Try it out in remix. If your trying to disable this feature you can use unchecked { //code.. }
    – Matt Jaf
    Commented May 2, 2023 at 23:33

1 Answer 1

0

cant you do it like this?

function test(uint256 a, uint256 b) public pure returns (int256 c) {
    c = (b >= a) ? int256(b - a) * 10 : int256(a - b) * -10;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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