### 9.6 Arithmetic constraints

- FD arithmetic expressions
- Partial AC: `(#=)/2` - constraint equal, `(#``\``=)/2` - constraint not equal,
  `(#``<``)/2` - constraint less than, `(#=``<``)/2` - constraint less than or equal,
  `(#``>``)/2` - constraint greater than, `(#``>``=)/2` - constraint greater than or equal
- Full AC: `(#=#)/2` - constraint equal, `(#``\``=#)/2` - constraint not equal,
  `(#``<``#)/2` - constraint less than, `(#=``<``#)/2` - constraint less than or equal,
  `(#``>``#)/2` - constraint greater than, `(#``>``=#)/2` - constraint greater than or equal
- `fd_prime/1`, `fd_not_prime/1`

#### 9.6.1 FD arithmetic expressions

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:

FD Expression | Result
Prolog variable | domain `0..fd_max_integer`
FD variable `X` | domain of `X`
integer number `N` | domain `N..N`
`+ E` | same as `E`
`- E` | opposite of `E`
`E1 + E2` | sum of `E1` and `E2`
`E1 - E2` | subtraction of `E2` from `E1`
`E1 * E2` | multiplication of `E1` by `E2`
`E1 / E2` | integer division of `E1` by `E2` (only succeeds if the remainder is 0)
`E1 ** E2` | `E1` raised to the power of `E2` (`E1` or `E2` must be an integer)
`min(E1,E2)` | minimum of `E1` and `E2`
`max(E1,E2)` | maximum of `E1` and `E2`
`dist(E1,E2)` | distance, i.e. |`E1 - E2|`
`E1 // E2` | quotient of the integer division of `E1` by `E2`
`E1 rem E2` | remainder of the integer division of `E1` by `E2`
`quot_rem(E1,E2,R)` | quotient of the integer division of `E1` by `E2` (`R` is the remainder of the integer division of `E1` by `E2`)

FD expressions are not restricted to be linear. However non-linear constraints usually yield less constraint propagation than linear constraints.

`+`, `-`, `*`, `/`, `//`, `rem` and `**` are predefined infix operators. `+` and `-` are predefined prefix operators (section 8.14.10).

Errors

a sub-expression is of the form `_ ** E` and `E` is a variable |  | `instantiation_error`
a sub-expression `E` is neither a variable nor an integer nor an FD arithmetic functor |  | `type_error(fd_evaluable, E)`
an expression is too complex |  | `resource_error(too_big_fd_constraint)`

#### 9.6.2 Partial AC: `(#=)/2` - constraint equal, `(#\=)/2` - constraint not equal,
`(#<)/2` - constraint less than, `(#=<)/2` - constraint less than or equal,
`(#>)/2` - constraint greater than, `(#>=)/2` - constraint greater than or equal

Templates

  `#=(?fd_evaluable, ?fd_evaluable)
  #\=(?fd_evaluable, ?fd_evaluable)
  #<(?fd_evaluable, ?fd_evaluable)
  #=<(?fd_evaluable, ?fd_evaluable)
  #>(?fd_evaluable, ?fd_evaluable)
  #>=(?fd_evaluable, ?fd_evaluable)`

Description

`FdExpr1 #= FdExpr2` constrains `FdExpr1` to be equal to `FdExpr2`.

`FdExpr1 #\= FdExpr2` constrains `FdExpr1` to be different from `FdExpr2`.

`FdExpr1 #< FdExpr2` constrains `FdExpr1` to be less than `FdExpr2`.

`FdExpr1 #=< FdExpr2` constrains `FdExpr1` to be less than or equal to `FdExpr2`.

`FdExpr1 #> FdExpr2` constrains `FdExpr1` to be greater than `FdExpr2`.

`FdExpr1 #>= FdExpr2` constrains `FdExpr1` to be greater than or equal to `FdExpr2`.

`FdExpr1` and `FdExpr2` are arithmetic FD expressions (section 9.6.1).

`#=`, `#\=`, `#<`, `#=<`, `#>` and `#>=` are predefined infix operators (section 8.14.10).

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 9.6.3) but is generally more efficient for arithmetic. These arithmetic constraints can be reified (section 9.7).

Errors

Refer to the syntax of arithmetic FD expressions for possible errors (section 9.6.1).

Portability

GNU Prolog predicates.

#### 9.6.3 Full AC: `(#=#)/2` - constraint equal, `(#\=#)/2` - constraint not equal,
`(#<#)/2` - constraint less than, `(#=<#)/2` - constraint less than or equal,
`(#>#)/2` - constraint greater than, `(#>=#)/2` - constraint greater than or equal

Templates

  `#=#(?fd_evaluable, ?fd_evaluable)
  #\=#(?fd_evaluable, ?fd_evaluable)
  #<#(?fd_evaluable, ?fd_evaluable)
  #=<#(?fd_evaluable, ?fd_evaluable)
  #>#(?fd_evaluable, ?fd_evaluable)
  #>=#(?fd_evaluable, ?fd_evaluable)`

Description

`FdExpr1 #=# FdExpr2` constrains `FdExpr1` to be equal to `FdExpr2`.

`FdExpr1 #\=# FdExpr2` constrains `FdExpr1` to be different from `FdExpr2`.

`FdExpr1 #<# FdExpr2` constrains `FdExpr1` to be less than `FdExpr2`.

`FdExpr1 #=<# FdExpr2` constrains `FdExpr1` to be less than or equal to `FdExpr2`.

`FdExpr1 #># FdExpr2` constrains `FdExpr1` to be greater than `FdExpr2`.

`FdExpr1 #>=# FdExpr2` constrains `FdExpr1` to be greater than or equal to `FdExpr2`.

`FdExpr1` and `FdExpr2` are arithmetic FD expressions (section 9.6.1).

`#=#`, `#\=#`, `#<#`, `#=<#`, `#>#` and `#>=#` are predefined infix operators (section 8.14.10).

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 9.6.1) but is generally less efficient for arithmetic. These arithmetic constraints can be reified (section 9.7.1).

Errors

Refer to the syntax of arithmetic FD expressions for possible errors (section 9.6.1).

Portability

GNU Prolog predicates.

#### 9.6.4 `fd_prime/1`, `fd_not_prime/1`

Templates

  `fd_prime(?fd_variable)
  fd_not_prime(?fd_variable)`

Description

`fd_prime(X)` constraints `X` to be a prime number between `0..vector_max`. This constraint enforces a sparse representation for the domain of `X` (section 9.1).

`fd_not_prime(X)` constraints `X` to be a non prime number between `0..vector_max`. This constraint enforces a sparse representation for the domain of `X` (section 9.1).

Errors

`X` is neither an FD variable nor an integer |  | `type_error(fd_variable, X)`

Portability

GNU Prolog predicates.

Copyright (C) 1999-2011 Daniel Diaz Verbatim copying and distribution of this entire article is permitted in any medium, provided this notice is preserved. More about the copyright