Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Arithmetic Operations

Binary operators

ExpressionDescriptionRaises?
a + bAdditionNo
a - bSubtractionNo
a * bMultiplicationNo
a / bTruncating division (toward zero)Yes (zero div)
a // bFloor division (rounds toward −∞)Yes (zero div)
a % bFloor modulo (Python semantics)Yes (zero div)
divmod(a,b)Floor quotient and remainderYes (zero div)
a ** bExponentiationYes (negative exp)

Unary operators

ExpressionDescription
-aNegation
+aUnary plus (returns copy)
abs(a)Absolute value
if a:Nonzero test (Bool(a) is not supported)
~aBitwise NOT (two’s complement)

In-place operators

+=, -=, *=, //=, %=, <<=, >>= are all supported and perform true in-place mutation to reduce memory allocation.

var a = BInt("12345678901234567890")
var b = BInt(12345)
print(a + b)   # 12345678901234580235
print(a - b)   # 12345678901234555545
print(a * b)   # 152407406035740740602050
print(a // b)  # 1000054994024671
print(a % b)   # 4395
print(BInt(2) ** 10)  # 1024