You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This page documents all user-facing math functions in Pythonic, using a clear tabular format with concise examples.
Overflow Policy
Many arithmetic and aggregation functions accept an optional policy parameter of type Overflow, which controls how overflows are handled:
Policy
Enum Value
Description
Overflow::Throw
0
Default for functions and operators. Throw an exception on overflow (safe, Python-like).
Overflow::Promote
1
Promote to a larger type on overflow (never throws, but may use bigger type).
Overflow::Wrap
2
Wrap around on overflow (C++-like, may lose data, never throws).
Overflow::None_of_them
3
Raw C++ arithmetic (no checks, maximum performance).
Policy Selection Guide:
Use Case
Recommended Policy
Production code (safety first)
Overflow::Throw
Scientific computing (large values)
Overflow::Promote
Performance-critical inner loops
Overflow::None_of_them
Embedded/systems programming
Overflow::Wrap
Usage Example:
add(1, 2); // Uses Overflow::Throw by defaultadd(1, 2, Overflow::Promote); // Promotes type on overflowadd(1, 2, Overflow::Wrap); // Wraps on overflowadd(1, 2, Overflow::None_of_them);// Raw C++ arithmetic (fastest)// Operator overloads use None_of_them by default for performance
var a = 1000000, b = 1000000;
var c = a * b; // Uses Overflow::None_of_them (raw C++ multiplication)
Type Promotion Sequence
When using Overflow::Promote, types are promoted in the following order:
Rank
Type
Description
0
bool
Boolean (never promoted TO)
1
unsigned int
Smallest unsigned integer
2
int
Smallest signed integer
3
unsigned long
Medium unsigned integer
4
long
Medium signed integer
5
unsigned long long
Largest unsigned integer
6
long long
Largest signed integer
7
float
Single-precision floating-point
8
double
Double-precision floating-point
9
long double
Extended-precision floating-point
Promotion Strategy
The promotion system uses different strategies based on input types:
Input Types
Promotion Strategy
Both unsigned
Try unsigned containers (uint → ulong → ulong_long), then floating
At least one signed
Try signed containers (int → long → long_long), then floating
Any floating-point
Use floating containers only (float → double → long double)
Smart Fit Behavior
The smart_promote() function finds the smallest container that can hold the result:
// Both unsigned → result fits in smallest unsigned type
var a = 100u, b = 200u;
var result = add(a, b, Overflow::Promote); // Returns unsigned int (300)// Large result → promoted to larger type
var big = std::numeric_limits<int>::max();
var result = add(big, 1, Overflow::Promote); // Returns long long// Division always uses floating-point for precision
var x = 5, y = 2;
var result = div(x, y, Overflow::Promote); // Returns float (2.5)
Arithmetic Operations (Overflow-Aware)
Function
Description
Example
add(a, b, policy)
Addition with overflow policy. Default is Overflow::Throw.
add(1, 2) add(1, 2, Overflow::Promote)
sub(a, b, policy)
Subtraction with overflow policy. Default is Overflow::Throw.
sub(5, 3) sub(5, 3, Overflow::Wrap)
mul(a, b, policy)
Multiplication with overflow policy. Default is Overflow::Throw.
mul(2, 3) mul(2, 3, Overflow::Promote)
div(a, b, policy)
Division with overflow policy. Default is Overflow::Throw.
div(6, 2) div(6, 2, Overflow::Wrap)
mod(a, b, policy)
Modulo with overflow policy. Default is Overflow::Throw.
mod(7, 3) mod(7, 3, Overflow::Promote)
Use the main function for full control; helper variants like add_throw, add_promote, add_wrap are shortcuts.
Power & Roots
Function
Description
Example
pow(base, exp, policy)
Power with overflow policy. Default is Overflow::Throw.
Least common multiple. Default is Overflow::Throw.
lcm(6, 8) lcm(6, 8, Overflow::Wrap)
factorial(n, policy)
Factorial. Default is Overflow::Throw.
factorial(5) factorial(5, Overflow::Promote)
Constants
Constant
Description
Example
pi()
Archimedes' constant
pi()
e()
Euler's number
e()
Random Functions
Function
Description
Example
random_int(min, max)
Random integer in [min, max]
random_int(1, 10)
random_float(min, max)
Random float in [min, max)
random_float(0, 1)
random_choice(list)
Random element from list
random_choice(list(1,2,3))
random_choice_set(set)
Random element from set
random_choice_set(set(1,2,3))
fill_random(count, min, max)
List of random ints
fill_random(5, 1, 10)
fill_randomf(count, min, max)
List of random floats
fill_randomf(5, 0, 1)
fill_randomn(count, mean, stddev)
List of normal floats
fill_randomn(5, 0, 1)
fill_random_set(count, min, max)
Set of random ints
fill_random_set(5, 1, 10)
fill_randomf_set(count, min, max)
Set of random floats
fill_randomf_set(5, 0, 1)
fill_randomn_set(count, mean, stddev)
Set of normal floats
fill_randomn_set(5, 0, 1)
Calculator API
Function
Description
Example
calculator()
Starts interactive CLI calculator
pythonic::calculator::calculator();
Calculator
Calculator class for programmatic use
pythonic::calculator::Calculator calc;
process(line)
Process a line (expression/assignment)
calc.process("var a=10. a+5");
Notes
For arithmetic, power, lcm, factorial, and product, the policy parameter controls overflow: Overflow::Throw (default), Overflow::Promote, or Overflow::Wrap.
Helper functions like lcm_throw, lcm_promote, lcm_wrap, etc., are shortcuts for common overflow policies.
All functions operate on var objects and support mixed numeric types.
Constants like pi() and e() return high-precision values as var.
Random functions use a thread-local engine for thread safety.