Appendix A

The math Module

On this page

Picat provides a math module, which has common mathematical constants and functions. The math module is imported by default.

A.1 Constants

The math module provides two constants.

A.2 Functions

The math module contains mathematical functions that serve a number of different purposes. Note that the arguments must all be numbers. If the arguments are not numbers, then Picat will throw an error.

A.2.1 Sign and Absolute Value

The following functions deal with the positivity and negativity of numbers.

Example

Picat> Val1 = sign(3), Val2 = sign(-3), Val3 = sign(0)
Val1 = 1
Val2 = -1
Val3 = 0
Picat> Val = abs(-3)
Val = 3

A.2.2 Rounding and Truncation

The math module includes the following functions for converting a real number into the integers that are closest to the number.

Example

Picat> Val1 = ceiling(-3.2), Val2 = ceiling(3)
Val1 = -3
Val2 = 3
Picat> Val1 = floor(-3.2), Val2 = floor(3)
Val1 = -4
Val2 = 3
Picat> Val1 = round(-3.2), Val2 = round(-3.5), Val3 = round(3.5)
Val1 = -3
Val2 = -4
Val3 = 4
Picat> Val1 = truncate(-3.2), Val2 = truncate(3)
Val1 = -3
Val2 = 3
Picat> IF = modf(3.2)
IF = (3.0 , 0.2)

A.2.3 Exponents, Roots, and Logarithms

The following functions provide exponentiation, root, and logarithmic functions. Note that, in the logarithmic functions, if X ≤ 0, then an error is thrown.

Example

Picat> P1 = pow(2, 5), P2 = exp(2)
P1 = 32
P2 = 7.38906
Picat> S = sqrt(1)
S = 1.0
Picat> E = log(7), T = log10(7), T2 = log2(7), B = log(7, 7)
E = 1.94591
T = 0.845098
T2 = 2.80735
B = 1.0

A.2.4 Converting Between Degrees and Radians

The math module has two functions to convert between degrees and radians.

Example

Picat> R = to_radians(180)
R = 3.14159
Picat> D = to_degrees(pi())
D = 180.0

A.2.5 Trigonometric Functions

The math module provides the following trigonometric functions.

Example

Picat> S = sin(pi()), C = cos(pi()), T = tan(pi())
S = 0.0
C = -1.0
T = 0.0
Picat> S = asin(0), C = acos(0), T = atan(0), T2 = atan2(-10, 10)
S = 0.0
C = 1.5708
T = 0.0
T2 = -0.785398
Picat> S = sec(pi()/4), C = csc(pi()/4), T = cot(pi()/4)
S = 1.41421
C = 1.41421
T = 1.0
Picat> S = asec(2), C = acsc(2), T = acot(0)
S = 1.0472
C = 0.5236
T = 1.5708

A.2.6 Hyperbolic Functions

The math module provides the following hyperbolic functions.

Example

Picat> S = sinh(pi()), C = cosh(pi()), T = tanh(pi())
S = 11.54874
C = 11.59195
T = 0.99627
Picat> S = sech(pi()/4), C = csch(pi()/4), T = coth(pi()/4)
S = 0.75494
C = 1.15118
T = 1.52487
Picat> S = asinh(0), C = acosh(1),  T = atanh(0)
S = 0.0
C = 0.0
T = 0.0

A.2.7 Random Numbers

The following functions provide access to a random number generator.

A.2.8 Other Built-ins