# Arithmetic

Author(s): Daniel Cabeza, Manuel Hermenegildo.

Version: 1.10#6 (2004/8/7, 21:46:39 CEST)

Version of last change: 0.9#18 (1999/3/23, 21:6:13 MET)

Arithmetic is performed by built-in predicates which take as arguments arithmetic expressions (see `arithexpression/1`) and evaluate them. Terms representing arithmetic expressions can be created dynamically, but at the time of evaluation, each variable in an arithmetic expression must be bound to a non-variable expression (the term must be ground). For example, given the code in the first line a possible shell interaction follows:

```evaluate(Expression, Answer) :- Answer is Expression.

?- _X=24*9, evaluate(_X+6, Ans).

Ans = 222 ?

yes
```

## Usage and interface (`arithmetic`)

• Library usage: These predicates are builtin in Ciao, so nothing special has to be done to use them.
• Exports:
• Predicates: `is/2`, `</2`, `=</2`, `>/2`, `>=/2`, `=:=/2`, `=\=/2`.
• Regular Types: `arithexpression/1`.

## Documentation on exports (`arithmetic`)

PREDICATE: is/2:

`Val is Exp`

The arithmetic expression `Exp` is evaluated and the result is unified with `Val`

Usage: `X is +arithexpression` * ISO *

• The following properties hold upon exit: `X` is a number. (`basic_props:num/1`)
• The following properties hold globally: This predicate is understood natively by CiaoPP. (`basic_props:native/1`)

PREDICATE: </2:

`Exp1 < Exp2`

The numeric value of `Exp1` is less than the numeric value of `Exp2` when both are evaluated as arithmetic expressions.

Usage: `+arithexpression < +arithexpression` * ISO *

• The following properties hold globally: This predicate is understood natively by CiaoPP. (`basic_props:native/1`)

PREDICATE: =</2:

`Exp1 =< Exp2`

The numeric value of `Exp1` is less than or equal to the numeric value of `Exp2` when both are evaluated as arithmetic expressions.

Usage: `+arithexpression =< +arithexpression` * ISO *

• The following properties hold globally: This predicate is understood natively by CiaoPP. (`basic_props:native/1`)

PREDICATE: >/2:

`Exp1 > Exp2`

The numeric value of `Exp1` is greater than the numeric value of `Exp2` when both are evaluated as arithmetic expressions.

Usage: `+arithexpression > +arithexpression` * ISO *

• The following properties hold globally: This predicate is understood natively by CiaoPP. (`basic_props:native/1`)

PREDICATE: >=/2:

`Exp1 >= Exp2`

The numeric value of `Exp1` is greater than or equal to the numeric value of `Exp2` when both are evaluated as arithmetic expressions.

Usage: `+arithexpression >= +arithexpression` * ISO *

• The following properties hold globally: This predicate is understood natively by CiaoPP. (`basic_props:native/1`)

PREDICATE: =:=/2:

`Exp1 =:= Exp2`

The numeric values of `Exp1` and `Exp2` are equal when both are evaluated as arithmetic expressions.

Usage: `+arithexpression =:= +arithexpression` * ISO *

• The following properties hold globally: This predicate is understood natively by CiaoPP. (`basic_props:native/1`)

PREDICATE: =\=/2:

`Exp1 =\= Exp2`

The numeric values of `Exp1` and `Exp2` are not equal when both are evaluated as arithmetic expressions.

Usage: `+arithexpression =\= +arithexpression` * ISO *

• The following properties hold globally: This predicate is understood natively by CiaoPP. (`basic_props:native/1`)

REGTYPE: arithexpression/1:

An arithmetic expression is a term built from numbers and evaluable functors that represent arithmetic functions. An arithmetic expression evaluates to a number, which may be an integer ( `int/1`) or a float ( `flt/1`). The evaluable functors allowed in an arithmetic expression are listed below, together with an indication of the functions they represent. All evaluable functors defined in ISO-Prolog are implemented, as well as some other useful or traditional. Unless stated otherwise, an expression evaluates to a float if any of its arguments is a float, otherwise to an integer.

• `- /1`: sign reversal. * ISO *
• `+ /1`: identity.
• `-- /1`: decrement by one.
• `++ /1`: increment by one.
• `+ /2`: addition. * ISO *
• `- /2`: subtraction. * ISO *
• `* /2`: multiplication. * ISO *
• `// /2`: integer division. Float arguments are truncated to integers, result always integer. * ISO *
• `/ /2`: division. Result always float. * ISO *
• `rem/2`: integer remainder. The result is always an integer, its sign is the sign of the first argument. * ISO *
• `mod/2`: modulo. The result is always a positive integer. * ISO *
• `abs/1`: absolute value. * ISO *
• `sign/1`: sign of. * ISO *
• `float_integer_part/1`: float integer part. Result always float. * ISO *
• `float_fractional_part/1`: float fractional part. Result always float. * ISO *
• `truncate/1`: The result is the integer equal to the integer part of the argument. * ISO *
• `integer/1`: same as `truncate/1`.
• `float/1`: conversion to float. * ISO *
• `floor/1`: largest integer not greater than. * ISO *
• `round/1`: integer nearest to. * ISO *
• `ceiling/1`: smallest integer not smaller than. * ISO *
• `** /2`: exponentiation. Result always float. * ISO *
• `>> /2`: integer bitwise right shift. * ISO *
• `<< /2`: integer bitwise left shift. * ISO *
• `/\ /2`: integer bitwise and. * ISO *
• `\/ /2`: integer bitwise or. * ISO *
• `\ /1`: integer bitwise complement. * ISO *
• `# /2`: integer bitwise exclusive or (xor).
• `exp/1`: exponential (e to the power of). Result always float. * ISO *
• `log/1`: natural logarithm (base e). Result always float. * ISO *
• `sqrt/1`: square root. Result always float. * ISO *
• `sin/1`: sine. Result always float. * ISO *
• `cos/1`: cosine. Result always float. * ISO *
• `atan/1`: arc tangent. Result always float. * ISO *
• `gcd/2`: Greatest common divisor. Arguments must evaluate to integers, result always integer.

In addition to these functors, a list of just a number evaluates to this number. Since a quoted string is just a list of integers, this allows a quoted character to be used in place of its ASCII code; e.g. `"A"` behaves within arithmetic expressions as the integer 65. Note that this is not ISO-compliant, and that can be achieved by using the ISO notation `0'A`.

Arithmetic expressions, as described above, are just data structures. If you want one evaluated you must pass it as an argument to one of the arithmetic predicates defined in this library.

Usage: `arithexpression(E)`

• Description: `E` is an arithmetic expression.