<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <HTML> <HEAD> <META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <META name="GENERATOR" content="hevea 1.06-7 of 2001-11-14"> <TITLE> Arithmetic constraints </TITLE> </HEAD> <BODY TEXT=black BGCOLOR=white> <A HREF="manual059.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A> <A HREF="manual054.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A> <A HREF="manual061.html"><IMG SRC ="next_motif.gif" ALT="Next"></A> <HR> <TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#66dbff"><DIV ALIGN=center><TABLE> <TR><TD><FONT SIZE=4><B><A NAME="htoc321">8.6</A></B></FONT></TD> <TD WIDTH="100%" ALIGN=center><FONT SIZE=4><B>Arithmetic constraints</B></FONT></TD> </TR></TABLE></DIV></TD> </TR></TABLE><UL> <LI><A HREF="manual060.html#toc263"> FD arithmetic expressions</A> <LI><A HREF="manual060.html#toc264"> Partial AC: <TT>(#=)/2</TT> - constraint equal, <TT>(#\=)/2</TT> - constraint not equal,<BR> <TT>(#<)/2</TT> - constraint less than, <TT>(#=<)/2</TT> - constraint less than or equal,<BR> <TT>(#>)/2</TT> - constraint greater than, <TT>(#>=)/2</TT> - constraint greater than or equal</A> <LI><A HREF="manual060.html#toc265"> Full AC: <TT>(#=#)/2</TT> - constraint equal, <TT>(#\=#)/2</TT> - constraint not equal,<BR> <TT>(#<#)/2</TT> - constraint less than, <TT>(#=<#)/2</TT> - constraint less than or equal,<BR> <TT>(#>#)/2</TT> - constraint greater than, <TT>(#>=#)/2</TT> - constraint greater than or equal</A> <LI><A HREF="manual060.html#toc266"> <TT>fd_prime/1</TT>, <TT>fd_not_prime/1</TT></A> </UL> <BR> <A NAME="toc263"></A><TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#98e7ff"><DIV ALIGN=center><TABLE> <TR><TD><B><A NAME="htoc322">8.6.1</A></B></TD> <TD WIDTH="100%" ALIGN=center><B>FD arithmetic expressions</B></TD> </TR></TABLE></DIV></TD> </TR></TABLE> <A NAME="FD-arithmetic-expressions"></A> An FD arithmetic expression is a Prolog term built from integers, variables (Prolog or FD variables), and functors (or operators) that represent arithmetic functions. The following table details the components of an FD arithmetic expression:<BR> <TABLE BORDER=1 CELLSPACING=0 CELLPADDING=1> <TR><TD VALIGN=top ALIGN=left NOWRAP>FD Expression</TD> <TD VALIGN=top ALIGN=left><DIV ALIGN=left>Result</DIV></TD> </TR> <TR><TD VALIGN=top ALIGN=left NOWRAP>Prolog variable</TD> <TD VALIGN=top ALIGN=left><DIV ALIGN=left>domain <TT>0..fd_max_integer</TT></DIV></TD> </TR> <TR><TD VALIGN=top ALIGN=left NOWRAP>FD variable <TT>X</TT></TD> <TD VALIGN=top ALIGN=left><DIV ALIGN=left>domain of <TT>X</TT></DIV></TD> </TR> <TR><TD VALIGN=top ALIGN=left NOWRAP>integer number <TT>N</TT></TD> <TD VALIGN=top ALIGN=left><DIV ALIGN=left>domain <TT>N..N</TT></DIV></TD> </TR> <TR><TD VALIGN=top ALIGN=left NOWRAP><TT>+ E</TT></TD> <TD VALIGN=top ALIGN=left><DIV ALIGN=left>same as <TT>E</TT></DIV></TD> </TR> <TR><TD VALIGN=top ALIGN=left NOWRAP><TT>- E</TT></TD> <TD VALIGN=top ALIGN=left><DIV ALIGN=left>opposite of <TT>E</TT></DIV></TD> </TR> <TR><TD VALIGN=top ALIGN=left NOWRAP><TT>E1 + E2</TT></TD> <TD VALIGN=top ALIGN=left><DIV ALIGN=left>sum of <TT>E1</TT> and <TT>E2</TT></DIV></TD> </TR> <TR><TD VALIGN=top ALIGN=left NOWRAP><TT>E1 - E2</TT></TD> <TD VALIGN=top ALIGN=left><DIV ALIGN=left>subtraction of <TT>E2</TT> from <TT>E1</TT></DIV></TD> </TR> <TR><TD VALIGN=top ALIGN=left NOWRAP><TT>E1 * E2</TT></TD> <TD VALIGN=top ALIGN=left><DIV ALIGN=left>multiplication of <TT>E1</TT> by <TT>E2</TT></DIV></TD> </TR> <TR><TD VALIGN=top ALIGN=left NOWRAP><TT>E1 / E2</TT></TD> <TD VALIGN=top ALIGN=left><DIV ALIGN=left>integer division of <TT>E1</TT> by <TT>E2</TT> (only succeeds if the remainder is 0)</DIV></TD> </TR> <TR><TD VALIGN=top ALIGN=left NOWRAP><TT>E1 ** E2</TT></TD> <TD VALIGN=top ALIGN=left><DIV ALIGN=left><TT>E1</TT> raised to the power of <TT>E2 </TT>(<TT>E1</TT> or <TT>E2</TT> must be an integer)</DIV></TD> </TR> <TR><TD VALIGN=top ALIGN=left NOWRAP><TT>min(E1,E2)</TT></TD> <TD VALIGN=top ALIGN=left><DIV ALIGN=left>minimum of <TT>E1</TT> and <TT>E2</TT></DIV></TD> </TR> <TR><TD VALIGN=top ALIGN=left NOWRAP><TT>max(E1,E2)</TT></TD> <TD VALIGN=top ALIGN=left><DIV ALIGN=left>maximum of <TT>E1</TT> and <TT>E2</TT></DIV></TD> </TR> <TR><TD VALIGN=top ALIGN=left NOWRAP><TT>dist(E1,E2)</TT></TD> <TD VALIGN=top ALIGN=left><DIV ALIGN=left>distance, i.e. |<TT>E1 - E2|</TT></DIV></TD> </TR> <TR><TD VALIGN=top ALIGN=left NOWRAP><TT>E1 // E2</TT></TD> <TD VALIGN=top ALIGN=left><DIV ALIGN=left>quotient of the integer division of <TT>E1</TT> by <TT>E2</TT></DIV></TD> </TR> <TR><TD VALIGN=top ALIGN=left NOWRAP><TT>E1 rem E2</TT></TD> <TD VALIGN=top ALIGN=left><DIV ALIGN=left>remainder of the integer division of <TT>E1</TT> by <TT>E2</TT></DIV></TD> </TR> <TR><TD VALIGN=top ALIGN=left NOWRAP><TT>quot_rem(E1,E2,R)</TT></TD> <TD VALIGN=top ALIGN=left><DIV ALIGN=left>quotient of the integer division of <TT>E1</TT> by <TT>E2</TT> <BR> (<TT>R</TT> is the remainder of the integer division of <TT>E1</TT> by <TT>E2</TT>)</DIV></TD> </TR></TABLE><BR> FD expressions are not restricted to be linear. However non-linear constraints usually yield less constraint propagation than linear constraints.<BR> <BR> <TT>+</TT>, <TT>-</TT>, <TT>*</TT>, <TT>/</TT>, <TT>//</TT>, <TT>rem</TT> and <TT>**</TT> are predefined infix operators. <TT>+</TT> and <TT>-</TT> are predefined prefix operators (section <A HREF="manual037.html#op/3:(Term-input/output)">7.14.10</A>).<BR> <BR> <B>Errors</B><BR> <TABLE CELLSPACING=2 CELLPADDING=0> <TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD> </TR> <TR><TD VALIGN=top ALIGN=left>a sub-expression is of the form <TT>_ ** E</TT> and <TT>E</TT> is a variable</TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD VALIGN=top ALIGN=left><TT>instantiation_error</TT></TD> </TR> <TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD> </TR> <TR><TD VALIGN=top ALIGN=left>a sub-expression <TT>E</TT> is neither a variable nor an integer nor an FD arithmetic functor</TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD VALIGN=top ALIGN=left><TT>type_error(fd_evaluable, E)</TT></TD> </TR> <TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD> </TR> <TR><TD VALIGN=top ALIGN=left>an expression is too complex</TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD VALIGN=top ALIGN=left><TT>resource_error(too_big_fd_constraint)</TT></TD> </TR> <TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD> </TR></TABLE><BR> <A NAME="toc264"></A><TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#98e7ff"><DIV ALIGN=center><TABLE> <TR><TD><B><A NAME="htoc323">8.6.2</A></B></TD> <TD WIDTH="100%" ALIGN=center><B>Partial AC: <TT>(#=)/2</TT> - constraint equal, <TT>(#\=)/2</TT> - constraint not equal,<BR> <TT>(#<)/2</TT> - constraint less than, <TT>(#=<)/2</TT> - constraint less than or equal,<BR> <TT>(#>)/2</TT> - constraint greater than, <TT>(#>=)/2</TT> - constraint greater than or equal</B></TD> </TR></TABLE></DIV></TD> </TR></TABLE> <A NAME="Partial-AC:-(:=)/2"></A> <BR> <B>Templates</B> <DL COMPACT=compact><DT><DD><TT> #=(?fd_evaluable, ?fd_evaluable)<BR> #\=(?fd_evaluable, ?fd_evaluable)<BR> #<(?fd_evaluable, ?fd_evaluable)<BR> #=<(?fd_evaluable, ?fd_evaluable)<BR> #>(?fd_evaluable, ?fd_evaluable)<BR> #>=(?fd_evaluable, ?fd_evaluable)</TT></DL> <B>Description</B><BR> <BR> <TT>FdExpr1 #= FdExpr2</TT> constrains <TT>FdExpr1</TT> to be equal to <TT>FdExpr2</TT>.<BR> <BR> <TT>FdExpr1 #\= FdExpr2</TT> constrains <TT>FdExpr1</TT> to be different from <TT>FdExpr2</TT>.<BR> <BR> <TT>FdExpr1 #< FdExpr2</TT> constrains <TT>FdExpr1</TT> to be less than <TT>FdExpr2</TT>.<BR> <BR> <TT>FdExpr1 #=< FdExpr2</TT> constrains <TT>FdExpr1</TT> to be less than or equal to <TT>FdExpr2</TT>.<BR> <BR> <TT>FdExpr1 #> FdExpr2</TT> constrains <TT>FdExpr1</TT> to be greater than <TT>FdExpr2</TT>.<BR> <BR> <TT>FdExpr1 #>= FdExpr2</TT> constrains <TT>FdExpr1</TT> to be greater than or equal to <TT>FdExpr2</TT>.<BR> <BR> <TT>FdExpr1</TT> and <TT>FdExpr2</TT> are arithmetic FD expressions (section <A HREF="#FD-arithmetic-expressions">8.6.1</A>).<BR> <BR> <TT>#=</TT>, <TT>#\=</TT>, <TT>#<</TT>, <TT>#=<</TT>, <TT>#></TT> and <TT>#>=</TT> are predefined infix operators (section <A HREF="manual037.html#op/3:(Term-input/output)">7.14.10</A>).<BR> <BR> These predicates post arithmetic constraints that are managed by the solver using a partial arc-consistency algorithm to reduce the domain of involved variables. In this scheme only the bounds of the domain of variables are updated. This leads to less propagation than full arc-consistency techniques (section <A HREF="#Full-AC:-(:=:)/2">8.6.3</A>) but is generally more efficient for arithmetic. These arithmetic constraints can be reified (section <A HREF="manual061.html#Boolean-and-reified-constraints">8.7</A>).<BR> <BR> <B>Errors</B><BR> <BR> Refer to the syntax of arithmetic FD expressions for possible errors (section <A HREF="#FD-arithmetic-expressions">8.6.1</A>).<BR> <BR> <B>Portability</B><BR> <BR> GNU Prolog predicates.<BR> <BR> <A NAME="toc265"></A><TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#98e7ff"><DIV ALIGN=center><TABLE> <TR><TD><B><A NAME="htoc324">8.6.3</A></B></TD> <TD WIDTH="100%" ALIGN=center><B>Full AC: <TT>(#=#)/2</TT> - constraint equal, <TT>(#\=#)/2</TT> - constraint not equal,<BR> <TT>(#<#)/2</TT> - constraint less than, <TT>(#=<#)/2</TT> - constraint less than or equal,<BR> <TT>(#>#)/2</TT> - constraint greater than, <TT>(#>=#)/2</TT> - constraint greater than or equal</B></TD> </TR></TABLE></DIV></TD> </TR></TABLE> <A NAME="Full-AC:-(:=:)/2"></A> <BR> <B>Templates</B> <DL COMPACT=compact><DT><DD><TT> #=#(?fd_evaluable, ?fd_evaluable)<BR> #\=#(?fd_evaluable, ?fd_evaluable)<BR> #<#(?fd_evaluable, ?fd_evaluable)<BR> #=<#(?fd_evaluable, ?fd_evaluable)<BR> #>#(?fd_evaluable, ?fd_evaluable)<BR> #>=#(?fd_evaluable, ?fd_evaluable)</TT></DL> <B>Description</B><BR> <BR> <TT>FdExpr1 #=# FdExpr2</TT> constrains <TT>FdExpr1</TT> to be equal to <TT>FdExpr2</TT>.<BR> <BR> <TT>FdExpr1 #\=# FdExpr2</TT> constrains <TT>FdExpr1</TT> to be different from <TT>FdExpr2</TT>.<BR> <BR> <TT>FdExpr1 #<# FdExpr2</TT> constrains <TT>FdExpr1</TT> to be less than <TT>FdExpr2</TT>.<BR> <BR> <TT>FdExpr1 #=<# FdExpr2</TT> constrains <TT>FdExpr1</TT> to be less than or equal to <TT>FdExpr2</TT>.<BR> <BR> <TT>FdExpr1 #># FdExpr2</TT> constrains <TT>FdExpr1</TT> to be greater than <TT>FdExpr2</TT>.<BR> <BR> <TT>FdExpr1 #>=# FdExpr2</TT> constrains <TT>FdExpr1</TT> to be greater than or equal to <TT>FdExpr2</TT>.<BR> <BR> <TT>FdExpr1</TT> and <TT>FdExpr2</TT> are arithmetic FD expressions (section <A HREF="#FD-arithmetic-expressions">8.6.1</A>).<BR> <BR> <TT>#=#</TT>, <TT>#\=#</TT>, <TT>#<#</TT>, <TT>#=<#</TT>, <TT>#>#</TT> and <TT>#>=#</TT> are predefined infix operators (section <A HREF="manual037.html#op/3:(Term-input/output)">7.14.10</A>).<BR> <BR> These predicates post arithmetic constraints that are managed by the solver using a full arc-consistency algorithm to reduce the domain of involved variables. In this scheme the full domain of variables is updated. This leads to more propagation than partial arc-consistency techniques (section <A HREF="#FD-arithmetic-expressions">8.6.1</A>) but is generally less efficient for arithmetic. These arithmetic constraints can be reified (section <A HREF="manual061.html#Boolean-FD-expressions">8.7.1</A>).<BR> <BR> <B>Errors</B><BR> <BR> Refer to the syntax of arithmetic FD expressions for possible errors (section <A HREF="#FD-arithmetic-expressions">8.6.1</A>).<BR> <BR> <B>Portability</B><BR> <BR> GNU Prolog predicates.<BR> <BR> <A NAME="toc266"></A><TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#98e7ff"><DIV ALIGN=center><TABLE> <TR><TD><B><A NAME="htoc325">8.6.4</A></B></TD> <TD WIDTH="100%" ALIGN=center><B><TT>fd_prime/1</TT>, <TT>fd_not_prime/1</TT></B></TD> </TR></TABLE></DIV></TD> </TR></TABLE> <BR> <B>Templates</B> <DL COMPACT=compact><DT><DD><TT> fd_prime(?fd_variable)<BR> fd_not_prime(?fd_variable)</TT></DL> <B>Description</B><BR> <BR> <TT>fd_prime(X)</TT> constraints <TT>X</TT> to be a prime number between <TT>0..vector_max</TT>. This constraint enforces a sparse representation for the domain of <TT>X</TT> (section <A HREF="manual055.html#Intro-FD">8.1</A>).<BR> <BR> <TT>fd_not_prime(X)</TT> constraints <TT>X</TT> to be a non prime number between <TT>0..vector_max</TT>. This constraint enforces a sparse representation for the domain of <TT>X</TT> (section <A HREF="manual055.html#Intro-FD">8.1</A>).<BR> <BR> <B>Errors</B><BR> <TABLE CELLSPACING=2 CELLPADDING=0> <TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD> </TR> <TR><TD VALIGN=top ALIGN=left><TT>X</TT> is neither an FD variable nor an integer</TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD VALIGN=top ALIGN=left><TT>type_error(fd_variable, X)</TT></TD> </TR> <TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD> </TR></TABLE><BR> <B>Portability</B><BR> <BR> GNU Prolog predicates.<BR> <BR> <HR SIZE=2> Copyright (C) 1999-2002 Daniel Diaz <BR> <BR> Verbatim copying and distribution of this entire article is permitted in any medium, provided this notice is preserved. <BR> <BR> <A HREF="index.html#copyright">More about the copyright</A> <HR> <A HREF="manual059.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A> <A HREF="manual054.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A> <A HREF="manual061.html"><IMG SRC ="next_motif.gif" ALT="Next"></A> </BODY> </HTML>