Built-in functions and operators

Numerics calculator powered by math.js engine and support big range of math functions and operators.

Use help("name") in calculator to get help on the given method

Operators

The following operators are available in the expression parser of math.js.

Also we have

  • a%b for a% of b, 32%220 is 32% of 220 and equals 70.4
  • a! for factorial of a
  • #a converts a from radians to degrees

Functions:

Calculator supports common math functions like sin(x), cos(x), sqrt(x), etc. And powerful function shortcuts like a^b, a!, #a, a%b.

Essentials

  • boolean(a) - Convert a string or number into a boolean.
    • boolean(0)
    • boolean(1)
    • true
    • boolean(3)
    • true
    • boolean("true")
    • true
    • boolean("false")
    • boolean([1, 0, 1, 1])
    • [[true, false, true, true]]
  • complex(re,im) - Create a complex number.
    • complex()
    • 0
    • complex(2, 3)
    • 2 + 3i
    • complex("7 - 2i")
    • 7 - 2i
  • index(a,b,c,...) - Create an index to get or replace a subset of a matrix
  • matrix(a) - Create a matrix.
  • number(x) - Create a number or convert a string or boolean into a number.
  • string(x) - Create a string or convert a value to a string
  • unit(x) - Create unit
    • 5.5 mm
    • 5.5 mm
    • 3 inch
    • 3 inch
    • unit(7.1, "kilogram")
    • 7.1 kilogram
    • unit("23 deg")
    • 23 deg

Utility Functions

  • clone(x) Clone an object
    • clone(3.5); // returns number 3.5
    • clone(2 - 4i); // returns Complex 2 - 4i
    • clone(45 deg); // returns Unit 45 deg
    • clone([[1, 2], [3, 4]]); // returns Array [[1, 2], [3, 4]]
    • clone("hello world"); // returns string "hello world"
  • help(string) - show help on using given function
    • help(square)
  • format(a) - Format a value of any type as string.
  • forEach(x, callback) Iterate over all elements of a matrix/array, and executes the given callback function.
  • format(value [, precision]) Format a value of any type into a string.
  • ifElse(condition, trueExpr, falseExpr) Execute a conditional expression.
  • map(x, callback) Create a new matrix or array with the results of the callback function executed on each entry of the matrix/array.
  • print(template, values [, precision]) Interpolate values into a string template.
  • typeof(x) Determine the type of a variable
    • typeof(3.5); // returns 'number'
    • typeof(2 - 4i); // returns 'complex'
    • typeof(45 deg); // returns 'unit'
    • typeof("hello world"); // returns 'string'

Arithmetic functions

  • abs(a) for the absolute value of a
  • add(a, b) for sum of a and b
  • ceil(a) for integer closest to a and not less than a
  • compare(x, y) - Compare two values. Returns 1 when x > y, -1 when x < y, and 0 when x == y. For matrices, the function is evaluated element wise.
  • cube(a) for cube of a
  • divide(a,b) divide a by b
  • edivide(x, y) Divide two matrices element wise. The function accepts both matrices and scalar values.
  • emultiply(a,b) multiply two values element wise.
  • epow(a,b) Calculates the power of x to y element wise
  • equal(a,b) Check equality of two values. Returns 1 if the values are equal, and 0 if not.
  • exp(a) for exponent of a
  • fix(a) round a value towards zero.If x is complex, both real and imaginary part are rounded towards zero.
  • floor(a) for integer closest to and not greater than a
  • gcd(a,b,c,...) for greatest common divisor of given numbers
  • larger(a,b) check if value x is larger than y. Returns 1 if x is larger than y, and 0 if not
  • largereq(a,b) check if value x is larger or equal to y. Returns 1 if x is larger or equal to y, and 0 if not.
  • lcm(a,b,c,...) for least common multiple of given numbers
  • log(a[, b]) for log of a base b, where b optional and defaults to e. Compute the logarithm of a value. If no base is provided, the natural logarithm of a is calculated. If base if provided, the logarithm is calculated for the specified base. log(a, base) is defined as log(a) / log(base).
  • ln(a) for log of a base e
  • lg(a) or log10(a) - Compute the 10-base logarithm of a value a.
  • max(a, b) for the maximum of a and b
  • min(a, b) for the minimum of a and b
  • mod(a, b) Calculates the modulus, the remainder of an integer division a/b
  • multiply(a,b) for multiplying a to b
  • norm(x [, p]) Calculate the norm of a number, vector or matrix. The second parameter p is optional. If not provided, it defaults to 2.
  • pow(a, b) or a^b for a to the power b
  • random() for pseudorandom number in the range 0 to 1
  • round(a [,n]) for integer closest to a
  • sign(a) - Compute the sign of a value. The sign of a value x is 1 when x>1, -1 when x<0, and 0 when x=0.
  • smaller(a,b) - Check if value x is smaller than value y. Returns 1 if x is smaller than y, and 0 if not.
  • smallereq(a,b) - Check if value x is smaller or equal to value y. Returns 1 if x is smaller than y, and 0 if not.
  • subtract(a,b) - subtract two values
  • sqrt(a) for square root of a
  • square(a) - Compute the square of a value. The square of x is x * x.
  • unary(a) - Inverse the sign of a value.
  • unequal(a,b) - Check unequality of two values. Returns 1 if the values are unequal, and 0 if they are equal.
  • xgcd(a,b) for extended greatest-common-divisor or extended eucledian algorithm

Binary functions

  • base(a,to,from) for converting number a from base from to base to
  • bin(a) for converting number a from base 10 to base 2
  • dec(a,from) for converting number a from base from to base 10
  • hex(a) for converting number a from base 10 to base 16

Statistics

  • max(a, b, c, ...) Compute the maximum value of a matrix or a list with values. In case of a multi dimensional array, the maximum of the flattened array will be calculated. When dim is provided, the maximum over the selected dimension will be calculated. Parameter dim is zero-based.
  • mean(a, b, c, ...) Compute the mean value of matrix or a list with values. In case of a multi dimensional array, the mean of the flattened array will be calculated. When dim is provided, the maximum over the selected dimension will be calculated. Parameter dim is zero-based.
  • median(a, b, c, ...) Compute the median of a matrix or a list with values. The values are sorted and the middle value is returned. In case of an even number of values, the average of the two middle values is returned. Supported types of values are: Number, BigNumber, Unit. In case of a (multi dimensional) array or matrix, the median of all elements will be calculated.
  • min(a, b, c, ...) Compute the maximum value of a matrix or a list of values. In case of a multi dimensional array, the maximum of the flattened array will be calculated. When dim is provided, the maximum over the selected dimension will be calculated. Parameter dim is zero-based.
  • prod(a, b, c, ...) Compute the product of a matrix or a list with values. In case of a (multi dimensional) array or matrix, the sum of all elements will be calculated.
  • std(a, b, c, ...) Compute the standard deviation of a matrix or a list with values. The standard deviations is defined as the square root of the variance: std(A) = sqrt(var(A)). In case of a (multi dimensional) array or matrix, the standard deviation over all elements will be calculated.
  • sum(a, b, c, ...) Compute the sum of a matrix or a list with values. In case of a (multi dimensional) array or matrix, the sum of all elements will be calculated.
  • var(a, b, c, ...) Compute the variance of a matrix or a list with values. In case of a (multi dimensional) array or matrix, the variance over all elements will be calculated.

Trigonometry

  • acos(a) for arc cosine of a
  • asin(a) for arc sine of a
  • atan(a) for arc tangent of a
  • atan2(a, b) - Computes the principal value of the arc tangent of a/b in radians.
  • cos(a) for cosine of a. Simply add "#" to indicate grads: e.g. cos(#90)
  • cosh(x) Calculate the hyperbolic cosine of a value, defined as cosh(x) = 1/2 * (exp(x) + exp(-x)).
  • cot(a) - Compute the cotangent of x in radians. Defined as 1/tan(a)
  • coth(x) Calculate the hyperbolic cotangent of a value, defined as coth(x) = 1 / tanh(x).
  • csc(a) - Compute the cosecant of x in radians. Defined as 1/sin(a)
  • csch(x) Calculate the hyperbolic cosecant of a value, defined as csch(x) = 1 / sinh(x).
  • sec(a) - Compute the secant of x in radians. Defined as 1/cos(a)
  • sech(x) Calculate the hyperbolic secant of a value, defined as sech(x) = 1 / cosh(x).
  • sin(a) for sine of a. Simply add "#" to indicate grads: e.g. sin(#90)
  • sinh(x) Calculate the hyperbolic sine of a value, defined as sinh(x) = 1/2 * (exp(x) - exp(-x)).
  • tan(a) for tangent of a
    • tan(2*15)
  • tanh(x) Calculate the hyperbolic tangent of a value, defined as tanh(x) = (exp(2 * x) - 1) / (exp(2 * x) + 1).

For matrices, the functions above are evaluated element wise.

Complex numbers

  • complex(a,b) or a+bi - Create a complex number.
    • x=2+3i
    • y=2-3i
    • x*y
  • re(x) - Get the real part of a complex number.
  • im(x) - Get the imaginary part of a complex number.
  • arg(x) -Compute the argument of a complex value. If x = a+bi, the argument is computed as atan2(b, a).
  • conj(x) - Compute the complex conjugate of a complex value. If x = a+bi, the complex conjugate is a-bi.
    • conj(2+3i)

Matrix operations

Calculator supports Matrix and Range types and allows this methods on matrices: clone, concat, det, diag, eye, inv, ones, size, squeeze, transpose, zeros.

Examples:

Functions:

  • clone(A) - Clone a variable. Creates a copy of primitive variables,and a deep copy of matrices
  • concat(a,b,...[,dim]) - Concatenate matrices. By default, the matrices are concatenated by the first dimension. The dimension on which to concatenate can be provided as last argument.
  • det(A) - Calculate the determinant of a matrix
  • diag(A) - Create a diagonal matrix or retrieve the diagonal of a matrix. When x is a vector, a matrix with the vector values on the diagonal will be returned. When x is a matrix, a vector with the diagonal values of the matrix is returned.When k is provided, the k-th diagonal will be filled in or retrieved, if k is positive, the values are placed on the super diagonal. When k is negative, the values are placed on the sub diagonal.
  • eye(m,n) - Returns the identity matrix with size m-by-n. The matrix has ones on the diagonal and zeros elsewhere.
  • inv(A) - Calculate the inverse of a matrix
  • ones(n,m) - Create a matrix containing ones.
  • range(n,m[,step]) or n:m - Create a range. Lower bound of the range is included, upper bound is excluded.
  • size(A) - Calculate the size of a matrix.
  • squeeze(A) - Remove singleton dimensions from a matrix.
  • subset(A,index[,replacement]) - Get or set a subset of a matrix or string. Indexes are one-based. Both the ranges lower-bound and upper-bound are included.
  • transpose(A) or A' - Transpose a matrix
  • zeros(m,n) - Create a matrix containing zeros.

Probablity

  • combinations(n, k) Compute the number of ways of picking k unordered outcomes from n possibilities. Combinations only takes integer arguments. The following condition must be enforced: k <= n.
  • factorial(a) - Compute the factorial of a value
  • distribution( name [arg1,arg2,...]) Create a distribution object of a specific type. A distribution object contains functions `random([size,] [min,] [max])`, `randomInt([size,] [min,] [max])`, and `pickRandom(array)`. Available types of distributions: "uniform", "normal". Note that the function distribution is currently not available via the expression parser.
  • permutations(n, k) Compute the number of ways of obtaining an ordered subset of k elements from a set of n elements. Permutations only takes integer arguments. The following condition must be enforced: k <= n.
  • pickRandom([min, max]) - Pick a random entry from a given array.
  • random([min, max]) - Return a random number.
  • randomInt([min, max]) - Return a random integer number

Constants:

Following built-in constants are supported

  • pi, PI - is the ratio of any circle's circumference to its diameter
  • e, E - is the unique real number that the value of the derivative of the f(x) = ex at x = 0 is equal to 1
  • tau - Tau is the ratio constant of a circle's circumference to radius, equal to 2 * pi, approximately 6.2832.
  • i - Imaginary unit, defined as i*i=-1. A complex number is described as a + b*i, where a is the real part, and b is the imaginary part.
  • LN2 - Returns the natural logarithm of 2, approximately equal to 0.693
  • LN10 - Returns the natural logarithm of 10, approximately equal to 2.302
  • LOG2E - Returns the natural logarithm of 10, approximately equal to 2.302
  • LOG10E - Returns the base-10 logarithm of E, approximately equal to 0.434
  • SQRT1_2 - Returns the square root of 1/2, approximately equal to 0.707
  • SQRT2 - Returns the square root of 2, approximately equal to 1.414
  • Infinity - Infinity, a number which is larger than the maximum number that can be handled by a floating point number.
  • true Boolean value true
  • false - Boolean value false
  • NaN - Not a number e.g. 0/0

Unit Conversions

Change the unit of a value.

Syntax:

  • x to unit
  • x in unit
  • to(x, unit)

Examples

Units

  • Length
    • meter, m
    • inch
    • foot, ft
    • yard, yd
    • mile, mi
    • link, li
    • rod, rd
    • chain, ch
    • angstrom
    • mil - 1/1000 inch
  • Surface
    • m2
    • sqin
    • sqft
    • sqyd
    • sqmi
    • sqrd
    • sqch
    • sqmil
  • Volume
    • m3
    • litre, L
    • cuin
    • cuft
    • cuyd
    • teaspoon
    • tablespoon
  • Liquid Volume
    • minim
    • fluiddram, fldr
    • fluidounce, floz
    • gill, gi
    • cup, cp
    • pint, pt
    • quart, qt
    • gallon, gal
    • beerbarrel, bbl
    • oilbarrel, obl
    • hogshead
  • Mass
    • gram, g
    • tonne, ton
    • grain, gr
    • dram, dr
    • ounce,oz
    • poundmass, lbm
    • hundredweight, cwt
    • stick
  • Time
    • second, seconds, s
    • minute, minutes, min
    • hour, hours, h
    • day, days
  • Angles
    • rad
    • deg
    • grad
    • cycle
  • Electric current
    • ampere, A
  • Temperature
    • kelvin, K
    • celsisus, degC
    • fahrenheight, degF
    • rankine, degR
  • Amount of Substance
    • mole, mol
  • Luminous intensity
    • candela, cd
  • Force
    • newton, N
    • poundforce, lbf
  • Binary
    • bits, b
    • bytes, B

Prefixes

  • Prefixes
    • deca, da
    • hecto, h
    • kilo, k
    • mega, M
    • giga, G
    • tera, T
    • peta, P
    • exa, E
    • zetta, Z
    • yotta, Y
    • deci, d
    • centi, c
    • milli, m
    • micro, u
    • nano, n
    • pico, p
    • femto, f
    • atto, a
    • zepto, z
    • yocto, y
  • Binary Prefixes
    • kilo, k
    • mega, M
    • giga, G
    • tera, T
    • peta, P
    • exa, E
    • zetta, Z
    • yotta, Y
    • kibi, Ki
    • mebi, Mi
    • gibi, Gi
    • tebi, Ti
    • pebi, Pi
    • exi, Ei
    • zebi, Zi
    • yobi, Yi

Need more functions and variables? Read about custom functions.