0

How is this calculated? According to solidity precedence, multiplication has precedence against division

Amount= (15- 10) * 10 / 15 * (10_000 - 5_000) / 10_000;

So Shouldn't it be

  1. 5 * 10 / 15 * 5_000 / 10_000

  2. 50 / 75_000 / 10_000

Not sure why remix and ChatGPT spit out answer 1

The step with AI was

From 1)

50/15 * 5_000 / 10_000

Why is the AI correct?

2 Answers 2

1

When it comes to the order of precedence of operators, according to the Solidity documentation, multiplication, division and modulo are evaluated before addition and substraction.

On the other hand, that computation will not even compile since it cannot be implicitly convertible to the expected type uint256. I recommend you read more on this here and have a look at all the available Solidity libraries for working with fixed or floating-point math.

2
  • So it's the same as PEMAS? Left to right? At first I thought multiplication should be done first regardless, thanks btw Commented Apr 10 at 8:17
  • I think you should also specify it's from left to right precedence if multiplication or division comes first. Regardless of either, whatever comes first will be calculated based on their operands Commented Apr 10 at 8:18
0

Could you please point out which refer link to According to solidity precedence, multiplication has precedence against division?

As my know, because solidity don't have float, and it will be run from left to right. It should be:

= (15- 10) * 10 / 15 * (10_000 - 5_000) / 10_000;
= 5 * 10 / 15 * 5_000 / 10_000
= 50 / 15 * 5_000 / 10_000
= 3 * 5_000 / 10_000
= 15_000 / 10_000
= 1
1
  • Don't know why this was downvoted, as I believe it is the right answer. To verify run the following code snippet in a function: uint256 a = 10; return (15 - 10) * a / 15 * (10_000 - 5_000) / 10_000;
    – BrainGrind
    Commented Oct 21 at 12:44

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.