diff options
author | chriseth <chris@ethereum.org> | 2018-09-26 23:33:51 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-26 23:33:51 +0800 |
commit | a17ca003ccd125f68ea7d472a2e006c64c1cc1b1 (patch) | |
tree | 3bcf05bdef71f9cb0137d404d38d7181b530bacc | |
parent | a86e1d187b929124c27365a8f2e185545c4157b1 (diff) | |
parent | d47ea5bb4784b69e71a905f853cde972ed40ad52 (diff) | |
download | dexon-solidity-a17ca003ccd125f68ea7d472a2e006c64c1cc1b1.tar.gz dexon-solidity-a17ca003ccd125f68ea7d472a2e006c64c1cc1b1.tar.zst dexon-solidity-a17ca003ccd125f68ea7d472a2e006c64c1cc1b1.zip |
Merge pull request #5006 from ethereum/modSemantics
Document modulo semantics
-rw-r--r-- | docs/types.rst | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/docs/types.rst b/docs/types.rst index f9fc80ce..3dc95ac2 100644 --- a/docs/types.rst +++ b/docs/types.rst @@ -86,8 +86,8 @@ They wrap in two's complement notation, meaning that for example ``uint256(0) - uint256(1) == 2**256 - 1``. You have to take these overflows into account when designing safe smart contracts. -Division and Modulus -^^^^^^^^^^^^^^^^^^^^ +Division +^^^^^^^^ Since the type of the result of an operation is always the type of one of the operands, division on integers always results in an integer. @@ -96,7 +96,23 @@ In Solidity, division rounds towards zero. This mean that ``int256(-5) / int256( Note that in contrast, division on :ref:`literals<rational_literals>` results in fractional values of arbitrary precision. -Division by zero and modulus with zero throws a runtime exception. +.. note:: + Division by zero causes a failing assert. + +Modulo +^^^^^^ + +The modulo operation ``a % n`` yields the remainder ``r`` after the division of the operand ``a`` +by the operand ``n``, where ``q = int(a / n)`` and ``r = a - (n * q)``. This means that modulo +results in the same sign as its left operand (or zero) and ``a % n == -(abs(a) % n)`` holds for negative ``a``: + + * ``int256(5) % int256(2) == int256(1)`` + * ``int256(5) % int256(-2) == int256(1)`` + * ``int256(-5) % int256(2) == int256(-1)`` + * ``int256(-5) % int256(-2) == int256(-1)`` + +.. note:: + Modulo with zero causes a failing assert. Exponentiation ^^^^^^^^^^^^^^ |