Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > by-pkgid > 2fc07611b08d4a735fd34d5eb60d8e16 > files > 2185

ciao-1.10p8-3mdv2010.0.i586.rpm

<HTML>
<HEAD>
<!-- Created by texi2html 1.56k + clip patches and <A href="http://www.clip.dia.fi.upm.es/Software">lpdoc</A> from ciao.texi on 28 January 2007 -->

<LINK rel="stylesheet" href="ciao.css" type="text/css">
<TITLE>The Ciao Prolog System               - Arithmetic</TITLE>
</HEAD>
<BODY> 
Go to the <A HREF="ciao_1.html">first</A>, <A HREF="ciao_22.html">previous</A>, <A HREF="ciao_24.html">next</A>, <A HREF="ciao_241.html">last</A> section, <A HREF="ciao_toc.html">table of contents</A>.
<P><HR><P>


<H1><A NAME="SEC150" HREF="ciao_toc.html#TOC150">Arithmetic</A></H1>
<P>
<A NAME="IDX1528"></A>


<P>
<STRONG>Author(s):</STRONG> Daniel Cabeza, Manuel Hermenegildo.


<P>
<STRONG>Version:</STRONG> 1.10#7 (2006/4/26, 19:22:13 CEST)


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


<P>
Arithmetic is performed by built-in predicates which take as arguments arithmetic expressions (see 
<A NAME="IDX1529"></A>
<CODE>arithexpression/1</CODE>) 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: 

<PRE>
evaluate(Expression, Answer) :- Answer is Expression.

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

Ans = 222 ?

yes
</PRE>


<UL>
<LI><A HREF="ciao_23.html#SEC151">Usage and interface (arithmetic)</A>
<LI><A HREF="ciao_23.html#SEC152">Documentation on exports (arithmetic)</A>
</UL>



<H2><A NAME="SEC151" HREF="ciao_toc.html#TOC151">Usage and interface (<CODE>arithmetic</CODE>)</A></H2>

<div class="cartouche">

<UL>

<LI><STRONG>Library usage:</STRONG>

These predicates are builtin in Ciao, so nothing special has to be done to use them.

<LI><STRONG>Exports:</STRONG>


<UL>

<LI><EM>Predicates:</EM>

<A NAME="IDX1530"></A>
<CODE>is/2</CODE>, 
<A NAME="IDX1531"></A>
<CODE>&#60;/2</CODE>, 
<A NAME="IDX1532"></A>
<CODE>=&#60;/2</CODE>, 
<A NAME="IDX1533"></A>
<CODE>&#62;/2</CODE>, 
<A NAME="IDX1534"></A>
<CODE>&#62;=/2</CODE>, 
<A NAME="IDX1535"></A>
<CODE>=:=/2</CODE>, 
<A NAME="IDX1536"></A>
<CODE>=\=/2</CODE>.

<LI><EM>Regular Types:</EM>

<A NAME="IDX1537"></A>
<CODE>arithexpression/1</CODE>.

</UL>

</UL>

</div class="cartouche">



<H2><A NAME="SEC152" HREF="ciao_toc.html#TOC152">Documentation on exports (<CODE>arithmetic</CODE>)</A></H2>
<P>
<A NAME="IDX1538"></A>
<A NAME="IDX1539"></A>
<DL>
<DT><span class="define">PREDICATE:</span> <B>is/2:</B>
<DD><A NAME="IDX1540"></A>


<P>
<CODE>Val is Exp</CODE>


<P>
The arithmetic expression <CODE>Exp</CODE> is evaluated and the result is unified with <CODE>Val</CODE>


<P>
<STRONG>Usage:</STRONG> <CODE>X is +arithexpression</CODE>
<KBD> * ISO * </KBD>

<UL>
<LI><EM>The following properties hold upon exit:</EM>

<CODE>X</CODE> is a number.
 (<CODE>basic_props:num/1</CODE>)
<LI><EM>The following properties hold globally:</EM>

This predicate is understood natively by CiaoPP.
 (<CODE>basic_props:native/1</CODE>)
</UL>

</DL>

<P>
<A NAME="IDX1541"></A>
<A NAME="IDX1542"></A>
<DL>
<DT><span class="define">PREDICATE:</span> <B>&#60;/2:</B>
<DD><A NAME="IDX1543"></A>


<P>
<CODE>Exp1 &#60; Exp2</CODE>


<P>
The numeric value of <CODE>Exp1</CODE> is less than the numeric value of <CODE>Exp2</CODE> when both are evaluated as arithmetic expressions.


<P>
<STRONG>Usage:</STRONG> <CODE>+arithexpression &#60; +arithexpression</CODE>
<KBD> * ISO * </KBD>

<UL>
<LI><EM>The following properties hold globally:</EM>

This predicate is understood natively by CiaoPP.
 (<CODE>basic_props:native/1</CODE>)
</UL>

</DL>

<P>
<A NAME="IDX1544"></A>
<A NAME="IDX1545"></A>
<DL>
<DT><span class="define">PREDICATE:</span> <B>=&#60;/2:</B>
<DD><A NAME="IDX1546"></A>


<P>
<CODE>Exp1 =&#60; Exp2</CODE>


<P>
The numeric value of <CODE>Exp1</CODE> is less than or equal to the numeric value of <CODE>Exp2</CODE> when both are evaluated as arithmetic expressions.


<P>
<STRONG>Usage:</STRONG> <CODE>+arithexpression =&#60; +arithexpression</CODE>
<KBD> * ISO * </KBD>

<UL>
<LI><EM>The following properties hold globally:</EM>

This predicate is understood natively by CiaoPP.
 (<CODE>basic_props:native/1</CODE>)
</UL>

</DL>

<P>
<A NAME="IDX1547"></A>
<A NAME="IDX1548"></A>
<DL>
<DT><span class="define">PREDICATE:</span> <B>&#62;/2:</B>
<DD><A NAME="IDX1549"></A>


<P>
<CODE>Exp1 &#62; Exp2</CODE>


<P>
The numeric value of <CODE>Exp1</CODE> is greater than the numeric value of <CODE>Exp2</CODE> when both are evaluated as arithmetic expressions.


<P>
<STRONG>Usage:</STRONG> <CODE>+arithexpression &#62; +arithexpression</CODE>
<KBD> * ISO * </KBD>

<UL>
<LI><EM>The following properties hold globally:</EM>

This predicate is understood natively by CiaoPP.
 (<CODE>basic_props:native/1</CODE>)
</UL>

</DL>

<P>
<A NAME="IDX1550"></A>
<A NAME="IDX1551"></A>
<DL>
<DT><span class="define">PREDICATE:</span> <B>&#62;=/2:</B>
<DD><A NAME="IDX1552"></A>


<P>
<CODE>Exp1 &#62;= Exp2</CODE>


<P>
The numeric value of <CODE>Exp1</CODE> is greater than or equal to the numeric value of <CODE>Exp2</CODE> when both are evaluated as arithmetic expressions.


<P>
<STRONG>Usage:</STRONG> <CODE>+arithexpression &#62;= +arithexpression</CODE>
<KBD> * ISO * </KBD>

<UL>
<LI><EM>The following properties hold globally:</EM>

This predicate is understood natively by CiaoPP.
 (<CODE>basic_props:native/1</CODE>)
</UL>

</DL>

<P>
<A NAME="IDX1553"></A>
<A NAME="IDX1554"></A>
<DL>
<DT><span class="define">PREDICATE:</span> <B>=:=/2:</B>
<DD><A NAME="IDX1555"></A>


<P>
<CODE>Exp1 =:= Exp2</CODE>


<P>
The numeric values of <CODE>Exp1</CODE> and <CODE>Exp2</CODE> are equal when both are evaluated as arithmetic expressions.


<P>
<STRONG>Usage:</STRONG> <CODE>+arithexpression =:= +arithexpression</CODE>
<KBD> * ISO * </KBD>

<UL>
<LI><EM>The following properties hold globally:</EM>

This predicate is understood natively by CiaoPP.
 (<CODE>basic_props:native/1</CODE>)
</UL>

</DL>

<P>
<A NAME="IDX1556"></A>
<A NAME="IDX1557"></A>
<DL>
<DT><span class="define">PREDICATE:</span> <B>=\=/2:</B>
<DD><A NAME="IDX1558"></A>


<P>
<CODE>Exp1 =\= Exp2</CODE>


<P>
The numeric values of <CODE>Exp1</CODE> and <CODE>Exp2</CODE> are not equal when both are evaluated as arithmetic expressions.


<P>
<STRONG>Usage:</STRONG> <CODE>+arithexpression =\= +arithexpression</CODE>
<KBD> * ISO * </KBD>

<UL>
<LI><EM>The following properties hold globally:</EM>

This predicate is understood natively by CiaoPP.
 (<CODE>basic_props:native/1</CODE>)
</UL>

</DL>

<P>
<A NAME="IDX1559"></A>
<A NAME="IDX1560"></A>
<DL>
<DT><span class="define">REGTYPE:</span> <B>arithexpression/1:</B>
<DD><A NAME="IDX1561"></A>


<P>
An arithmetic expression is a term built from numbers and 
<A NAME="IDX1562"></A>
evaluable functors that represent arithmetic functions. An arithmetic expression evaluates to a number, which may be an integer (
<A NAME="IDX1563"></A>
<CODE>int/1</CODE>) or a float (
<A NAME="IDX1564"></A>
<CODE>flt/1</CODE>). 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 
<A NAME="IDX1565"></A>
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. 



<UL>

<LI>

<A NAME="IDX1566"></A>
<CODE>- /1</CODE>: sign reversal.  <KBD> * ISO * </KBD>

<LI>

<A NAME="IDX1567"></A>
<CODE>+ /1</CODE>: identity. 

<LI>

<A NAME="IDX1568"></A>
<CODE>-- /1</CODE>: decrement by one. 

<LI>

<A NAME="IDX1569"></A>
<CODE>++ /1</CODE>: increment by one. 

<LI>

<A NAME="IDX1570"></A>
<CODE>+ /2</CODE>: addition.  <KBD> * ISO * </KBD>

<LI>

<A NAME="IDX1571"></A>
<CODE>- /2</CODE>: subtraction.  <KBD> * ISO * </KBD>

<LI>

<A NAME="IDX1572"></A>
<CODE>* /2</CODE>: multiplication.  <KBD> * ISO * </KBD>

<LI>

<A NAME="IDX1573"></A>
<CODE>// /2</CODE>: integer division. Float arguments are truncated to integers, result always integer.  <KBD> * ISO * </KBD>

<LI>

<A NAME="IDX1574"></A>
<CODE>/ /2</CODE>: division. Result always float.  <KBD> * ISO * </KBD>

<LI>

<A NAME="IDX1575"></A>
<CODE>rem/2</CODE>: integer remainder. The result is always an integer, its sign is the sign of the first argument.  <KBD> * ISO * </KBD>

<LI>

<A NAME="IDX1576"></A>
<CODE>mod/2</CODE>: modulo. The result is always a positive integer.  <KBD> * ISO * </KBD>

<LI>

<A NAME="IDX1577"></A>
<CODE>abs/1</CODE>: absolute value.  <KBD> * ISO * </KBD>

<LI>

<A NAME="IDX1578"></A>
<CODE>sign/1</CODE>: sign of.  <KBD> * ISO * </KBD>

<LI>

<A NAME="IDX1579"></A>
<CODE>float_integer_part/1</CODE>: float integer part. Result always float.  <KBD> * ISO * </KBD>

<LI>

<A NAME="IDX1580"></A>
<CODE>float_fractional_part/1</CODE>: float fractional part. Result always float.  <KBD> * ISO * </KBD>

<LI>

<A NAME="IDX1581"></A>
<CODE>truncate/1</CODE>: The result is the integer equal to the integer part of the argument.  <KBD> * ISO * </KBD>

<LI>

<A NAME="IDX1582"></A>
<CODE>integer/1</CODE>: same as 
<A NAME="IDX1583"></A>
<CODE>truncate/1</CODE>. 

<LI>

<A NAME="IDX1584"></A>
<CODE>float/1</CODE>: conversion to float.  <KBD> * ISO * </KBD>

<LI>

<A NAME="IDX1585"></A>
<CODE>floor/1</CODE>: largest integer not greater than.  <KBD> * ISO * </KBD>

<LI>

<A NAME="IDX1586"></A>
<CODE>round/1</CODE>: integer nearest to.  <KBD> * ISO * </KBD>

<LI>

<A NAME="IDX1587"></A>
<CODE>ceiling/1</CODE>: smallest integer not smaller than.  <KBD> * ISO * </KBD>

<LI>

<A NAME="IDX1588"></A>
<CODE>** /2</CODE>: exponentiation. Result always float.  <KBD> * ISO * </KBD>

<LI>

<A NAME="IDX1589"></A>
<CODE>&#62;&#62; /2</CODE>: integer bitwise right shift.  <KBD> * ISO * </KBD>

<LI>

<A NAME="IDX1590"></A>
<CODE>&#60;&#60; /2</CODE>: integer bitwise left shift.  <KBD> * ISO * </KBD>

<LI>

<A NAME="IDX1591"></A>
<CODE>/\ /2</CODE>: integer bitwise and.  <KBD> * ISO * </KBD>

<LI>

<A NAME="IDX1592"></A>
<CODE>\/ /2</CODE>: integer bitwise or.  <KBD> * ISO * </KBD>

<LI>

<A NAME="IDX1593"></A>
<CODE>\ /1</CODE>: integer bitwise complement.  <KBD> * ISO * </KBD>

<LI>

<A NAME="IDX1594"></A>
<CODE># /2</CODE>: integer bitwise exclusive or (xor). 

<LI>

<A NAME="IDX1595"></A>
<CODE>exp/1</CODE>: exponential (<EM>e</EM> to the power of). Result always float.  <KBD> * ISO * </KBD>

<LI>

<A NAME="IDX1596"></A>
<CODE>log/1</CODE>: natural logarithm (base <EM>e</EM>). Result always float.  <KBD> * ISO * </KBD>

<LI>

<A NAME="IDX1597"></A>
<CODE>sqrt/1</CODE>: square root. Result always float.  <KBD> * ISO * </KBD>

<LI>

<A NAME="IDX1598"></A>
<CODE>sin/1</CODE>: sine. Result always float.  <KBD> * ISO * </KBD>

<LI>

<A NAME="IDX1599"></A>
<CODE>cos/1</CODE>: cosine. Result always float.  <KBD> * ISO * </KBD>

<LI>

<A NAME="IDX1600"></A>
<CODE>atan/1</CODE>: arc tangent. Result always float.  <KBD> * ISO * </KBD>

<LI>

<A NAME="IDX1601"></A>
<CODE>gcd/2</CODE>: Greatest common divisor. Arguments must evaluate to integers, result always integer. 

</UL>

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


<P>
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. 


<P>
<STRONG>Usage:</STRONG> <CODE>arithexpression(E)</CODE>

<UL>
<LI><EM>Description:</EM> <CODE>E</CODE> is an arithmetic expression.

</UL>

</DL>

<P><HR><P>
Go to the <A HREF="ciao_1.html">first</A>, <A HREF="ciao_22.html">previous</A>, <A HREF="ciao_24.html">next</A>, <A HREF="ciao_241.html">last</A> section, <A HREF="ciao_toc.html">table of contents</A>.
</BODY>
</HTML>