Numeric protocols
These protocols describe the operations available on built-in numeric types.
Protocol hierarchy
Number (Times[Self], Minus): core numeric behavior such as+,-,*,**, unary+and-,abs,real,imag, andconjugateReal (Number): float-style conversion and rounding operations such as__float__,trunc,floor,ceil, androundRealFloat (Real): floating-point real numbersRational (Real): values withnumeratoranddenominatorIntegral (Rational, Logical): integer-style operations such as//,%, shifts, bit operations, and indexing
Built-in implementations
int,bigint, and the fixed-width signed and unsigned integer types implementIntegralfloatimplementsRealFloatcompleximplementsNumber
Use the narrowest constraint that matches the operations you need:
Choose Number when you need ordinary arithmetic.
Choose Integral when you need integer-only operations such
as floor division, modulo, or bit shifts.
def square[A(Number)](x: A) -> A:
return x * x
def bucket[A(Integral)](x: A, size: A) -> A:
return x // size
Numeric constraints affect both operator availability and result
types. For example, Div[A] separates the type of the result
from the type of the operands, which is why integer division through
/ can return a different type than floor division through
//.