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)
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))

All these operators takes two TealType.uint64 values. In addition, Eq(a, b) (==) is polymorphic: it also takes two TealType.bytes values. For example, Arg(0) == Arg(1) is a valid PyTeal expression.

Both And and Or also support more than 2 arguements:

  • 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 to Add(Add(Int(1), Int(2)), Int(3))
  • Int(1) + Int(2) * Int(3) is equivalent to Add(Int(1), Mul(Int(2), Int(3)))