The q* stdlib docs
qmath: fast floating-point arithmetic
qmath provides the math.* built-ins: fast floating-point arithmetic. Every argument is coerced to an f64, the operation runs in hardware double precision, and the result is returned as an Int when it has no fractional part and a Float otherwise. This is the fast, approximate counterpart to qnumber's exact arithmetic — use math.* when speed matters and small rounding is acceptable; use number.* when you need exactness.
What it gives you#
qmath registers five public built-ins. They accept any number of positional arguments (read in order from keys "0", "1", ...) and fold them into a single f64 result.
| Function | Behavior | Empty-args result |
|---|---|---|
| math.add | Sum of all arguments. | 0 |
| math.mul | Product of all arguments. | 1 (empty product) |
| math.double | First argument times 2. | 0 |
| math.max | Largest argument (fold from negative infinity). | negative infinity |
| math.min | Smallest argument (fold from positive infinity). | positive infinity |
Argument coercion to f64#
Each positional argument is converted to an f64 before the operation. The conversion is broad and lenient — even strings that look like numbers are accepted.
- Int / Timestamp
- Cast directly to f64.
- Float
- Used as-is.
- Decimal / BigInt
- Stringified then parsed to f64 (0.0 if parsing fails).
- Str
- Parsed to f64 (0.0 if it is not a number).
- any other type
- Contributes 0.0.
Int-vs-Float return#
After computing the f64 result, qmath returns an Int when the value is finite and has no fractional part (result.fract() == 0.0); otherwise it returns a Float. So math.add(3, 4, 5) yields Int 12, while math.double(2.5) yields Float 5.0 only if it has a fraction — 2.5 doubled is 5.0 which has no fraction, returning Int 5. A genuinely fractional result like math.double(1.25) returns Float 2.5.
Resource budget#
Each call decodes the arguments into a small Vec<f64> and builds a tiny result record — fixed, modest, scalar work. Every math.* function declares a 512-byte memory budget and zero storage.
Example#
Adding three integers; the total 12 has no fraction so it returns as an Int. This is the exact example used in the QQL documentation.
Request
SELECT math.add(3, 4, 5) FROM t
# wire form:
{ "0": {"Int": 3}, "1": {"Int": 4}, "2": {"Int": 5} }Response
{ "result": { "Int": 12 } }Output
12Request
math.double(21)
math.max(3, 9)Response
{ "result": { "Int": 42 } }
{ "result": { "Int": 9 } }Output
42
9