-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Shumo Chu
committed
Feb 4, 2020
1 parent
516e3a9
commit 35d2094
Showing
2 changed files
with
54 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,39 @@ | ||
.. _arithmetic_expressions: | ||
|
||
Arithmetic Expression | ||
===================== | ||
Arithmetic Expressions | ||
====================== | ||
|
||
An arithmetic expression is an expression that results in a :code:`TealType.uint64` value. | ||
In PyTeal, arithmetic expressions include integer arithmetics operators and boolean operators. | ||
We overloaded all integer arithmetics operator in Python. | ||
|
||
==================== =========== ================================================= ========================= | ||
Operator Overloaded Semantics Example | ||
==================== =========== ================================================= ========================= | ||
:code:`Lt(a, b)` :code:`<` `1` if a is less than b, `0` otherwise :code:`Int(1) < Int(5)` | ||
:code:`Gt(a, b)` :code:`>` `1` if a is greater than b, `0` otherwise :code:`Int(1) > Int(5)` | ||
:code:`Le(a, b)` :code:`<=` `1` if a is no greater than b, `0` otherwise :code:`Int(1) <= Int(5)` | ||
:code:`Ge(a, b)` :code:`>=` `1` if a is no less than b, `0` otherwise :code:`Int(1) >= Int(5)` | ||
:code:`Add(a, b)` :code:`+` `a + b`, error (panic) if overflow :code:`Int(1) + Int(5)` | ||
:code:`Minus(a, b)` :code:`-` `a - b`, error if underflow :code:`Int(5) - Int(1)` | ||
:code:`Mul(a, b)` :code:`*` `a * b`, error if overflow :code:`Int(2) * Int(3)` | ||
:code:`Div(a, b)` :code:`/` `a / b`, error if devided by zero :code:`Int(3) / Int(2)` | ||
:code:`Mod(a, b)` :code:`%` `a % b`, modulo operation :code:`Int(7) % Int(3)` | ||
:code:`Eq(a, b)` :code:`==` `1` if a equals b, `0` otherwise :code:`Int(7) == Int(7)` | ||
==================== =========== ================================================= ========================= | ||
|
||
|
||
All these operators takes two :code:`TealType.uint64` values. | ||
In addition, :code:`Eq(a, b)` (:code:`==`) is polymorphic: | ||
it also takes two `TealType.bytes` values. For example, :code:`Arg(0) == Arg(1)` is a valid PyTeal expression. | ||
|
||
While using overloaded Python arithmatic operators, the associativity and precedence is the same as the | ||
`original python operators <https://docs.python.org/3/reference/expressions.html#operator-precedence>`_ . For example: | ||
|
||
* :code:`Int(1) + Int(2) + Int(3)` is equivalent to :code:`Add(Add(Int(1), Int(2)), Int(3))` | ||
* :code:`Int(1) + Int(2) * Int(3)` is equivalent to :code:`Add(Int(1), Mul(Int(2), Int(3)))` | ||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters