Functions and Predicates

Visual Prolog has a full range of built-in mathematical functions and predicates that operate on integral and real values. The complete list is given in Table 9.¿À·ù! Ã¥°¥ÇÇ°¡ Á¤ÀǵǾî ÀÖÁö ¾Ê½À´Ï´Ù.

Table 9.3: Visual Prolog Arithmetic Predicates and Functions

Name

Description

X mod Y

Returns the remainder (modulos) of X divided by Y.

X div Y

Returns the quotient of X divided by Y.

abs(X)

If X is bound to a positive value val, abs(X) returns that value; otherwise, it returns -1 * val.

cos(X)

The trigonometric functions require that X be bound to

sin(X)

a value representing an angle in radians.

tan(X)

Returns the tangent of its argument.

arctan(X)

Returns the arc tangent of the real value to which X is bound.

exp(X)

e raised to the value to which X is bound.

ln(X)

Logarithm of X, base e.

log(X)

Logarithm of X, base 10.

sqrt(X)

Square root of X.

random(X)

Binds X to a random real; 0 <= X < 1.

random(X, Y)

Binds Y to a random integer; 0 <= Y < X.

round(X)

Returns the rounded value of X. The result still being a real

trunc(X)

Truncates X. The result still being a real

val(domain,X)

Explicit conversion between numeric domains.

1. Generating Random Numbers

Visual Prolog provides two standard predicates for generating random numbers. One returns a random real between 0 and 1, while the other returns a random integer between 0 and a given integer. Additionally, the random numbering sequence may be re-initialized.

(1) random/1

This version of random returns a random real number that satisfies the constraints

    0 <= RandomReal < 1.

random/1 takes this format:

    random(RandomReal)                                      /* (o) */

(2) random/2

This version of random takes two arguments, in this format:

    random(MaxValue, RandomInt)                          /* (i, o) */

It binds RandomInt to a random integer that satisfies the constraints

    0 <= RandomInt < MaxValue

random/2 is much faster than random/1 because random/2 only uses integer arithmetic.

(3) randominit/1

randominit will initialize the random number generator and is of the following form:

    randominit(Seed)                                        /* (i) */

The random number seed is initially 1, and the Seed argument to randominit sets this seed value. The main use for randominit is to provide repeatable sequences of pseudo random numbers for statistical testing. Note that both the integer and floating point versions of random use the same seed and basic number generator.

(4) Example

Program 1 uses random/1 to select three names from five at random.

/* Program ch9e01.pro */

2. Integer and Real Arithmetic

Visual Prolog provides predicates and functions for modulos function, integer division, square roots and absolute values, trigonometry, transcendental functions, rounding (up or down), and truncation. They are summarized in Table 9.¿À·ù! Ã¥°¥ÇÇ°¡ Á¤ÀǵǾî ÀÖÁö ¾Ê½À´Ï´Ù. and explained in the following pages.

(1) mod/2

mod performs the function X modulo Y (where X and Y are integers).

    X mod Y                                              /* (i, i) */

The expression Z = X mod Y binds Z to the result. For example,

    Z = 7 mod 4                                  /* Z will equal 3 */
    Y = 4 mod 7
                                  /* Y will equal 4 */

(2) div/2

div performs the integer division X/Y (where X and Y are integers).

    X div Y                                              /* (i, i) */

The expression Z = X div Y binds Z to the integer part of the result. For example,

    Z = 7 div 4                                  /* Z will equal 1 */
    Y = 4 div 7
                                  /* Y will equal 0 */

(3) abs/1

abs returns the absolute value of its argument.

    abs(X)                                                  /* (i) */

The expression Z = abs(X) binds Z (if it's free) to the result, or succeed/fail if Z is already bound. For example,

    Z = abs(-7)                                  /* Z will equal 7 */

(4) cos/1

cos returns the cosine of its argument.

    cos(X)                                                  /* (i) */

The expression Z = cos(X) binds Z (if it's free) to the result, or succeed/fail if Z is already bound. For example,

    Pi = 3.141592653,
    Z = cos(Pi)
                                 /* Z will equal -1 */

(5) sin/1

sin returns the sine of its argument.

    sin(X)                                                  /* (i) */

The expression Z = sin(X) binds Z (if it's free) to the result, or succeed/fail if Z is already bound. For example:

    Pi = 3.141592653,
    Z = sin(Pi)
                           /* Z will almost equal 0 */

(6) tan/1

tan returns the tangent of its argument.

    tan(X)                                                  /* (i) */

The expression Z = tan(X) binds Z (if it's free) to the result, or succeed/fail if Z is already bound. For example,

    Pi = 3.141592653,
    Z = tan(Pi)
                           /* Z will almost equal 0 */

(7) arctan/1

arctan returns the arc tangent of the real value to which X is bound.

    arctan(X)                                               /* (i) */

The expression Z = arctan(X) binds Z (if it's free) to the result, or succeed/fail if Z is already bound. For example,

    Pi = 3.141592653,
    Z = arctan(Pi)
                    /* Z will equal 1.2626272556 */

(8) exp/1

exp returns e raised to the value to which X is bound.

    exp(X)                                                  /* (i) */

The expression Z = exp(X) binds Z (if it's free) to the result, or succeed/fail if Z is already bound. For example,

    Z = exp(2.5)                      /* Z will equal 12.182493961 */

(9) ln/1

ln returns the natural logarithm of X (base e).

    ln(X                                                    /* (i) */

The expression Z = ln(X) binds Z (if it's free) to the result, or succeed/fail if Z is already bound. For example,

    Z = ln(12.182493961)                       /* Z will equal 2.5 */

(10) log/1

log returns the base 10 logarithm of X.

    log(X)                                                  /* (i) */

The expression Z = log(X) binds Z (if it's free) to the result, or succeed/fail if Z is already bound. For example,

    Z = log(2.5)                     /* Z will equal 0.39794000867 */

(11) sqrt/1

sqrt returns the positive square root of X.

    sqrt(X)                                                 /* (i) */

The expression Z = sqrt(X) binds Z (if it's free) to the result, or succeed/fail if Z is already bound. For example,

    Z = sqrt(25)                                 /* Z will equal 5 */

(12) round/1

round returns the rounded value of X.

    round(X)                                                /* (i) */

round rounds X up or down to the nearest integral value of X, but performs no type conversion. For example,

    Z1 = round(4.51)                            /* Z1 will equal 5 */
    Z2 = round(3.40)
                            /* Z2 will equal 3 */

Both Z1 and Z2 are floating point values following the above; only the fractional parts of the arguments to round have been rounded up or down.

(13) trunc/1

trunc truncates X to the right of the decimal point, discarding any fractional part. Just like round, trunc performs no type conversion.

    trunc(X)                                                /* (i) */

For example,

    Z = trunc(4.7)                               /* Z will equal 4 */

Again, Z is a floating point number.

(14) val/2

val provides general purpose conversion between the numeric domains, in cases where you want full control over the result of the operation. val observes any possible overflow condition. The format is

    Result = val(returndom,Expr)

where Expr will be evaluated (if it's an expression), the result converted to returndom and unified with Result. Visual Prolog also has a cast function that will convert uncritically between any domains; this is described in chapter 11.

(15) Exercise

Use the trigonometric functions in Visual Prolog to display a table of sine, cosine, and tangent values on the screen. The left column of the table should contain angle values in degrees, starting at 0 degrees and continuing to 360 degrees in steps of 15 degrees.

Note: Because the trigonometric functions take values expressed in radians, you must convert radians to angles to obtain entries for the left column.

    Degrees = Radians * 180/3.14159265...