Arithmetic OperatorsΒΆ
An arithmetic expression is an expression that results in a 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 |
---|---|---|---|
Lt(a, b) |
< |
1 if a is less than b, 0 otherwise | Int(1) < Int(5) |
Gt(a, b) |
> |
1 if a is greater than b, 0 otherwise | Int(1) > Int(5) |
Le(a, b) |
<= |
1 if a is no greater than b, 0 otherwise | Int(1) <= Int(5) |
Ge(a, b) |
>= |
1 if a is no less than b, 0 otherwise | Int(1) >= Int(5) |
Add(a, b) |
+ |
a + b, error (panic) if overflow | Int(1) + Int(5) |
Minus(a, b) |
- |
a - b, error if underflow | Int(5) - Int(1) |
Mul(a, b) |
* |
a * b, error if overflow | Int(2) * Int(3) |
Div(a, b) |
/ |
a / b, error if devided by zero | Int(3) / Int(2) |
Mod(a, b) |
% |
a % b, modulo operation | Int(7) % Int(3) |
Eq(a, b) |
== |
1 if a equals b, 0 otherwise | Int(7) == Int(7) |
Neq(a, b) |
!= |
0 if a equals b, 1 otherwise | Int(7) != Int(7) |
And(a, b) |
1 if a > 0 && b > 0, 0 otherwise | And(Int(1), Int(1)) |
|
Or(a, b) |
1 if a > 0 || b > 0, 0 otherwise | Or(Int(1), Int(0)) |
|
Not(a) |
1 if a equals 0, 0 otherwise | Not(Int(0)) |
|
BitwiseAnd(a,b) |
& |
a & b, bitwise and operation | Int(1) & Int(3) |
BitwiseOr(a,b) |
| |
a | b, bitwise or operation | Int(2) | Int(5) |
BitwiseXor(a,b) |
^ |
a ^ b, bitwise xor operation | Int(3) ^ Int(7) |
BitwiseNot(a) |
~ |
~a, bitwise complement operation | ~Int(1) |
All these operators takes two TealType.uint64
values.
In addition, Eq(a, b)
(==
) and Neq(a, b)
(!=
) also work for byte slices.
For example, Arg(0) == Arg(1)
and Arg(0) != Arg(1)
are valid PyTeal expressions.
Both And
and Or
also support more than 2 arguements when called as functions:
And(a, b, ...)
Or(a, b, ...)
The associativity and precedence of the overloaded Python arithmatic operators are the same as the original python operators . For example:
Int(1) + Int(2) + Int(3)
is equivalent toAdd(Add(Int(1), Int(2)), Int(3))
Int(1) + Int(2) * Int(3)
is equivalent toAdd(Int(1), Mul(Int(2), Int(3)))