Skip to content

Mathematics and statistics

function rank ƒ semantics
+ 2 a add
- 2 a subtract
* 2 a multiply
% 2 a divide
$ 2 A dot product, matrix multiply
& 2 a lesser
| 2 a greater
abs 1 a absolute value
acos 1 a arccosine
asin 1 a arcsine
atan 1 a arctangent
avg 1 A arithmetic mean
avgs 1 u arithmetic means
ceiling 1 a round up to integer
cor 2 A correlation
cos 1 a cosine
cov 2 A covariance
deltas 2 u differences
dev 1 A standard deviation
div 2 a integer division
ema 1 m exponential moving average
exp 1 a ex
floor 1 a round down to integer
inv 1 u matrix inverse
log 1 a natural logarithm
lsq 2 matrix divide
mavg 1 m moving average
max 1 A greatest
maxs 1 u maximums
mcount 1 m moving count
mdev 1 m moving deviation
med 1 A median
min 1 A least
mins 1 u minimums
mmax 1 m moving maximum
mmin 1 m moving minimum
mmu 2 matrix multiply
mod 2 a modulo
msum 1 m moving sum
prd 1 A product
prds 1 u products
ratios 1 u ratios
reciprocal 1 a reciprocal
scov 2 A statistical covariance
sdev 1 A statistical standard deviation
signum 1 a sign
sin 1 a sine
sqrt 1 a square root
sum 1 A sum
sums 1 u sums
svar 1 A statistical variance
tan 1 a tangent
til 1 natural numbers till
var 1 A variance
wavg 2 A weighted average
wsum 2 A weighted sum
xbar 2 round down
xexp 2 a xy
xlog 2 a base-x logarithm of y

ƒ – a: atomic; u: uniform; A: aggregate; m: moving

Domains and ranges

The domains and ranges of the mathematical functions have boolean, numeric, and temporal datatypes.

q)2+3 4 5
5 6 7
q)2012.05 2012.06m-2
2012.03 2012.04m
q)3.3 4.4 5.5*1b
3.3 4.4 5.5

Individual function articles tabulate domain and range datatypes.

Dictionaries and tables

The domains and ranges also extend to:

  • dictionaries where the value of the dictionary is in the domain
    
        q)3+`a`b`c!(42;2012.09.15;1b)
        a| 45
        b| 2012.09.18
        c| 4
        
  • simple tables where the value of the flip of the table is in the domain
    
        q)3%([]b:1 2 3;c:45 46 47)
        b   c
        --------------
        3   0.06666667
        1.5 0.06521739
        1   0.06382979
        
  • keyed tables where the value of the table is in the domain
    
        q)show v:([sym:`ibm`goog`msoft]qty:1000 2000 3000;p:1550 375 98)
        sym  | qty  p
        -----| ---------
        ibm  | 1000 1550
        goog | 2000 375
        msoft| 3000 98
        q)v+5
        sym  | qty  p
        -----| ---------
        ibm  | 1005 1555
        goog | 2005 380
        msoft| 3005 103
        

Mathematics with temporals

Temporal datatypes (timestamp, month, date, datetime, timespan, minute, second, time) are encoded as integer or float offsets from 2000.01.01 or 00:00.

Mathematical functions on temporals are applied to the underlying numerics. See domain/range tables for individual functions for the result datatypes.

Beyond addition and subtraction

Results for addition and subtraction are generally intuitive and useful; not always for other arithmetic functions.


q)2017.12.31+0 1 2
2017.12.31 2018.01.01 2018.01.02
q)2017.12m-0 1 2
2017.12 2017.11 2017.10m
q)2017.12m*0 1 2
2000.01 2017.12 2035.11m
q)2017.12m% 1 2 3
215 107.5 71.66667
q)00:10%2
5f
q)00:10:00%2
300f
q)00:10:00.000%2
300000f
q)00:10:00.000000000%2
3e+11

Aggregating nulls

avg, min, max and sum are special: they ignore nulls, in order to be similar to SQL92. But for nested x these functions preserve the nulls.

q)avg (1 2;0N 4)
0n 3