Solidity——數學函式



Solidity 也提供內建數學函式。以下是一些使用頻率很高的函式 −

  • addmod(uint x, uint y, uint k) 返回 (uint) − 計算 (x + y) % k,其中加法透過任意精度執行,不會在 2256 處取模。

  • mulmod(uint x, uint y, uint k) 返回 (uint) − 計算 (x * y) % k,其中加法透過任意精度執行,不會在 2256 處取模。

以下示例顯示了在 Solidity 中使用數學函式。

示例

pragma solidity ^0.5.0;

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

使用 Solidity 第一個應用程式 章節中提供的步驟執行上述程式。

首先單擊 callAddMod 按鈕,然後單擊 callMulMod 按鈕以檢視結果。

輸出

0: uint256: 0
0: uint256: 2
廣告
© . All rights reserved.