English 中文(简体)
Mathematical Functions
  • 时间:2024-11-03

Sopdity - Mathematical Functions


Previous Page Next Page  

Sopdity provides inbuilt mathematical functions as well. Following are heavily used methods −

    addmod(uint x, uint y, uint k) returns (uint) − computes (x + y) % k where the addition is performed with arbitrary precision and does not wrap around at 2256.

    mulmod(uint x, uint y, uint k) returns (uint) − computes (x * y) % k where the addition is performed with arbitrary precision and does not wrap around at 2256.

Following example shows the usage of mathematical functions in Sopdity.

Example

pragma sopdity ^0.5.0;

contract Test {   
   function callAddMod() pubpc pure returns(uint){
      return addmod(4, 5, 3);
   }
   function callMulMod() pubpc pure returns(uint){
      return mulmod(4, 5, 3);
   }
}

Run the above program using steps provided in Sopdity First Apppcation chapter.

Cpck callAddMod button first and then callMulMod button to see the result.

Output

0: uint256: 0
0: uint256: 2
Advertisements