Mathematical functions and constants

This page describes the mathematical functions and constants that are available in Phonometrica. A subset of these functions (abs, sqrt, exp, log, sin, cos, floor and ceil) also accept an Array argument, in which case the operation is applied to each element in the array and a new array is returned.

Arithmetic notes

  • Numbers distinguish Integer and Float values (1 vs 1.0).

  • Float division by zero yields inf (it does not raise an error).

  • Integer division uses the div keyword operator; 1 div 0 raises a math error.

  • The modulo operation uses the mod keyword operator (the % operator does not exist).

Global functions

abs(x as Number)

Returns the absolute value of x. If x is an Integer, the result is an Integer.


abs(x as Array)

Returns a copy of the array in which abs has been applied to each element.


acos(x as Number)

Returns the arccosine of x.


asin(x as Number)

Returns the arcsine of x.


atan(x as Number)

Returns the arctangent of x.


atan2(y as Number, x as Number)

Returns the four-quadrant inverse tangent of y and x.


ceil(x as Number)

Returns the smallest integer no smaller than x.


ceil(x as Array)

Returns a copy of the array in which ceil has been applied to each element.


cos(x as Number)

Returns the cosine of x.


cos(x as Array)

Returns a copy of the array in which cos has been applied to each element.


exp(x as Number)

Returns the exponential of x.


exp(x as Array)

Returns a copy of the array in which exp has been applied to each element.


floor(x as Number)

Returns the largest integer that is no larger than x.


floor(x as Array)

Returns a copy of the array in which floor has been applied to each element.


log(x as Number)

Returns the natural logarithm of x.


log(x as Array)

Returns a copy of the array in which log has been applied to each element.


log2(x as Number)

Returns the logarithm of x in base 2.


log10(x as Number)

Returns the logarithm of x in base 10.


max(x as Number, y as Number)

Returns the larger value between x and y.


max(x as Integer, y as Integer)

Returns the larger value between x and y.


min(x as Number, y as Number)

Returns the smaller value between x and y.


min(x as Integer, y as Integer)

Returns the smaller value between x and y.


random()

Returns a pseudo-random value in the interval [0, 1[ according to a uniform distribution.


round(x as Number)

Rounds x to the nearest integer (halfway cases are rounded away from zero). If x is an Integer, it is returned unchanged.


round(x as Number, ndigits as Integer)

Rounds x to ndigits digits after the decimal point.

print(round(3.14159, 2))   # prints 3.14

sin(x as Number)

Returns the sine of x.


sin(x as Array)

Returns a copy of the array in which sin has been applied to each element.


sqrt(x as Number)

Returns the square root of x.


sqrt(x as Array)

Returns a copy of the array in which sqrt has been applied to each element.


tan(x as Number)

Returns the tangent of x.

Constants

E

Returns the value of e, the base of the natural logarithm (approximately 2.718281).


PI

Returns the value of pi (approximately 3.141593).

These constants are ordinary global bindings: a local or module variable with the same name shadows them. (The old PHI and SQRT2 constants no longer exist.)