Numbers and math

Round numbers, take roots, logarithms and powers of e, and work with angles in degrees or radians, in the Calculator and the Sheet.

What the group is for

These functions round numbers, take roots and logarithms, raise e to a power, and convert angles with sine, cosine and tangent. Each one takes a single number and returns a number. They work the same way in the Calculator and in a Sheet.

Constants

pi: the number pi, 3.14159.

e: Euler's number, 2.71828, the base of ln and exp.

Type a constant without parentheses. pi() returns the error E_TYPE. Function and constant names ignore case, so PI and Sqrt(2) work.

pi => 3.14159
e => 2.71828
2 * pi => 6.2832

Round a number

round(x): rounds x to the nearest whole number. A half rounds up, toward the larger number, so round(2.5) returns 3 and round(-2.5) returns -2.

floor(x): the largest whole number less than or equal to x.

ceil(x): the smallest whole number greater than or equal to x.

abs(x): returns x with its sign dropped.

Each of the four takes exactly one argument. round has no argument for the number of decimals. To round to two decimals, multiply by 100, round, and divide by 100.

round(2.5) => 3
round(-2.5) => -2
floor(-2.5) => -3
ceil(-2.5) => -2
abs(-3.75) => 3.75
round(2.346 * 100) / 100 => 2.35

The precision button in the Calculator changes only the decimals shown. Use round when a later step of the formula needs the rounded value.

Roots, powers and logarithms

sqrt(x): the square root of x.

exp(x): e raised to the power x.

ln(x): the natural logarithm of x, base e.

log(x): the base 10 logarithm of x.

log takes one argument and is always base 10; log(8, 2) returns the error E_ARITY. For a logarithm in another base b, divide ln(x) by ln(b). For a root other than the square root, raise to a fractional power with ^.

sqrt(2) => 1.414
exp(1) => 2.718
ln(e) => 1
log(1000) => 3
ln(8) / ln(2) => 3
27 ^ (1 / 3) => 3

exp(r * t) is the growth of 1 at a continuous yearly rate r over t years. ln(b / a) is the log return from a price a to a price b.

exp(0.05 * 2) => 1.10517
ln(110 / 100) => 0.09531

Work with angles

sin(x), cos(x), tan(x): the sine, cosine and tangent of the angle x.

asin(x), acos(x), atan(x): the angle whose sine, cosine or tangent is x.

Angles are in degrees by default. In the Calculator, click the angle button to switch between deg and rad; asin, acos and atan then return their angle in the unit the button shows. A Sheet always uses degrees. To use a radian value in degree mode, multiply it by 180 / pi.

sin(30) => 0.5
cos(60) => 0.5
tan(45) => 1
asin(0.5) => 30
atan(1) => 45
sin(pi / 6 * 180 / pi) => 0.5

Values outside a function's range

The math functions return no error for an argument outside their range. The result is a value that the Calculator and the Sheet display as NaN, ∞ or -∞:

  • sqrt(-1), ln(-1) and asin(2) show NaN.
  • ln(0) and log(0) show -∞.
  • Division by zero follows the same rule: 1 / 0 shows ∞.

A NaN or infinite value passes through arithmetic. Write a formula gives the result of a comparison with NaN. Test the argument first when the input can fall outside the range. A comparison such as x >= 0 returns 1 or 0.

Series and matrices

The functions on this page take a single number. A Series or a Matrix argument, such as sqrt(iv30.series("SPY")), returns the error E_TYPE.

The operators +, -, *, / and ^ work point by point on a Series. Use ^ 0.5 for the square root of each point. A number computed with the functions on this page works as the other operand. Matrix operators are described in Matrices.

iv30.series("SPY") ^ 0.5 => a Series of square roots, one per trading day
iv30.series("SPY") * sqrt(30 / 365) => a Series of 30-day moves as a fraction of price

Combine with other groups

Use these functions with the number a data function returns. The 30-day implied volatility is a yearly figure; multiply it by sqrt(30 / 365) for the expected move over 30 calendar days, as a fraction of the price.

iv30("SPY") * sqrt(30 / 365) => a number for the 30-day move as a fraction of price
round(iv30("SPY") * 1000) / 10 => a number for IV30 in percent to one decimal

Series covers functions that take a whole history. Matrices covers matrix math. The Function reference lists the signature of every function, and Errors explains E_TYPE and E_ARITY.