Re: Constraints: Bug? X*X*X #= 0 fails

Daniel Diaz <[email protected]>
Newsgroups gmane.comp.gnu.prolog.general
Message-ID <[email protected]>
Hello,
> In gnu prolog 1.3.0 no value for X is found for the constraint X*X*X 
> #= 0 and additionally, gprolog states there is no solution:
>
> | ?- X*X*X #= 0.
>
> no
>

The problem comes from integer overflow (X*X*X computes the upper bound 
as the max of X ** 3, which overflows the integer encoding). Generally 
it is recommended to first define the domain of the variable (e.g. using 
fd_domain) and then to add constraints. Example:

| ?- fd_domain(X,0,1000), X*X*X #= 0.

X = _#3(0..1000)

Note: in this example X=0 is not detected until labeling, but the 
solution is obtained with a labeling:

| ?- fd_domain(X,0,1000), X*X*X#=0, fd_labeling(X).

X = 0 ? ;
no

It is better to use the ** operator (power) for this:

| ?- X**3 #= 0.

X = 0

> On the other hand, the (correct) solution X #= 1 is found for X*X*X #= 1:
>
> | ?- X*X*X #= 1.
>
> X = 1
>
> yes
>
> and X #= 2 is found for X*X*X = 8.
>
> GNU Prolog gives no solutions for X*X*X #= 27 (X #= 3), X*X*X #= 64 (X 
> #= 4), and even states there are none. Is this behaviour intentional?
>
| ?- fd_domain(X,0,1000), X*X*X#=27, fd_labeling(X).

X = 3

or better

| ?- X**3 #= 27.

X = 3
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.