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.
- sign() = : This function returns 1, 0, or -1 according to whether is positive, zero, or negative.
- abs() = : This function returns the absolute value of . If , then this function returns . Otherwise, this function returns .
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.
- ceiling() = : This function returns the closest integer that is greater than or equal to .
- floor() = : This function returns the closest integer that is less than or equal to .
- round() = : This function returns the integer that is closest to .
- truncate() = : This function removes the fractional part from a real number.
- modf() = (, ): This function splits a real number into its integer part and its fractional part.
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 , then an error is thrown.
- pow(, ) = : This function returns . It does the same thing as .
- pow_mod(, , ) = : This function returns mod . All of the arguments must be integers, and must not be negative.
- exp() = : This function returns .
- sqrt() = : This function returns the square root of . Note that the math module does not support imaginary numbers. Therefore, if , this function throws an error.
- log() = : This function returns .
- log10() = : This function returns .
- log2() = : This function returns .
- log(, ) = : This function returns .
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.
- to_radians() = : This function converts from degrees to radians.
- to_degrees() = : This function converts from radians to degrees.
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.
- sin() = : This function returns the sine of , where is given in radians.
- cos() = : This function returns the cosine of , where is given in radians.
- tan() = : This function returns the tangent of , where is given in radians.
- sec() = : This function returns the secant of , where is given in radians. If cos() is , then this function throws an error.
- csc() = : This function returns the cosecant of , where is given in radians. If sin() is , then this function throws an error.
- cot() = : This function returns the cotangent of , where is given in radians. If tan() is , then this function throws an error.
- asin() = : This function returns the arc sine of , in radians. The returned value is in the range [-pi / 2, pi / 2]. must be in the range [, ]; otherwise, this function throws an error.
- acos() = : This function returns the arc cosine of , in radians. The returned value is in the range [, pi]. must be in the range [, ]; otherwise, this function throws an error.
- atan() = : This function returns the arc tangent of , in radians. The returned value is in the range [-pi / 2, pi / 2].
- atan2(, ) = : This function returns the arc tangent of / , in radians. and are coordinates. The returned value is in the range [-pi, pi].
- asec() = : This function returns the arc secant of , in radians. The returned value is in the range [, pi]. must be in the range (, ] or [, ); otherwise, this function throws an error.
- acsc() = : This function returns the arc cosecant of , in radians. The returned value is in the range [-pi / 2, pi / 2]. must be in the range (, ] or [, ); otherwise, this function throws an error.
- acot() = : This function returns the arc cotangent of , in radians. The returned value is in the range [-pi / 2, pi / 2].
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.
- sinh() = : This function returns the hyperbolic sine of .
- cosh() = : This function returns the hyperbolic cosine of .
- tanh() = : This function returns the hyperbolic tangent of .
- sech() = : This function returns the hyperbolic secant of .
- csch() = : This function returns the hyperbolic cosecant of . If is , then this function throws an error.
- coth() = : This function returns the hyperbolic cotangent of . If is , then this function throws an error.
- asinh() = : This function returns the arc hyperbolic sine of .
- acosh() = : This function returns the arc hyperbolic cosine of . If , then this function throws an error.
- atanh() = : This function returns the arc hyperbolic tangent of . must be in the range (, ); otherwise, this function throws an error.
- asech() = : This function returns the arc hyperbolic secant of . must be in the range (, ]; otherwise, this function throws an error.
- acsch() = : This function returns the arc hyperbolic cosecant of . If is , then this function throws an error.
- acoth() = : This function returns the arc hyperbolic cotangent of . must be in the range (, ) or (, ); otherwise, this function throws an error.
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.
- random() = : This function returns a random integer.
- random2() = : This function returns a random integer. It uses an environment-dependent seed.
- rand_max() = : This function returns the maximum random integer.
- random() = : This function returns a random integer. At the same time, it changes the seed of the random number generator.
- random(, ) = : This function returns a random integer in the range ...
- frand() = : This function returns a random real number between 0.0 and 1.0, inclusive.
- frand(, ) = : This function returns a random real number between and , inclusive.
A.2.8 Other Built-ins
- binom(, ) = : This function returns the binomial coefficient .
- even(): This predicate is true if is an even integer.
- gcd(, ) = : This function returns the greatest common divisor of integer and integer .
- odd(): This predicate is true if is an odd integer.
- prime(): This predicate is true if is a prime number.
- primes() = : This function returns a list of prime numbers that are less than or equal to .