Ethereum: How multiplication and division works in solidity?

Ethereum: How the multiplication and the department for solidity work

================================================================== ==== ============================================================== ========= =======================. =====================.

In the event of solidity, the order of the operations for specific calculations is of crucial importance. Two common arithmetic operations that can cause confusion are multiplication and division. In this article we will examine how these operations work in solidity by focusing specifically on how to calculate them.

Priority and order of the operations

——————————————– ——————————————– ——————————————– ——————————————- –

Solidity follows a specific priority rule in relation to arithmetic operations:

  • Clamps: First evaluate the expressions within brackets.

  • Exhibitor (for example **): Then evaluate the exhibitors.

  • Multiplication and department (e.g. 2 * 3): The order is a multiplication in front of the department.

  • Addition and subtraction (e.g. 5 + 3): The order is additionally before the subtraction.

Let us examine the example you have specified:

`Solidity

Quantity = (15-10) 10/15 (10000 - 5000) / 10000;

"" ""

By breaking this expression, we can follow the priority rules:

  • Rate the expressions within brackets: "(15-10)" and "(10000 - 5000)" "

*15-10 = 5(exponentially not applicable)

*10000 – 5000 = 5000(exponentially not applicable)

  • Multiply510:5 * 10 = 50 '

  • Share50 ‘by 15’:50/15 = 3.333 ...(Due to the arithmetics with a floating comma, the division is not exactly)

  • Finally, multiply the result with (5000/10000): (3.333 ...) * (5) = 16.666 ...

So the correct calculation should actually be:

"" ""

Amount = 50/15 * 5000/10000;

"" ""

Not5 * 10/15 ‘.

Current mistakes

————–

To avoid current mistakes if you work with arithmetic operations of solidity:

  • always evaluate the expressions within the brackets.

  • Pay attention to the exhibitors (e.g. **) and make sure that you perform the operation correctly.

  • During the division, use floating comma arithmetic to avoid problems due to the entire division.

Best practices

————–

To write a precise and effective solidity code:

  • Use brackets to group operations for more clarity.

  • Follow the priority order carefully.

  • Pay attention to exponentials (e.g. **) when carrying out calculations with a large number or exponential growth.

By understanding the functioning of multiplication and division into solidey, you can write more precise and reliable intelligent contracts that benefit from the unique features of language.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *