Real numbers not working in Prolog

I have the following code:

:-use_module(library(clpfd)).
afn(A,B,C):-
    C #= B*A.

It works correctly with integers, but not with decimal numbers:

43 ?- afn(20, 10, C).
C = 200.

44 ?- afn(20, -10, C).
C = -200.

45 ?- afn(20, -10.5, C).
ERROR: Domain error: `clpfd_expression' expected, found `-10.5'
46 ?- 

How can I work with decimals here? Thank.

Edit: I find the following works with decimal places:

afn(A,B,C):-
    C is B * A.

67 ?- afn(20.895, 40.5, C).
C = 846.2475.

Is it right ??

+4
source share
2 answers

You must distinguish

( "moded" ) . , : , , . , , /2 ( Prolog), , , .

?- A=3, B=1.5, C is B * A.
C = 4.5

?- A=3, C=4.5, C is B * A.
instantiation fault                 <<< input B not kown

. , . /. , , .

, (, ), .

ECLiPSe library (ic) (. 2, 3), :

?- A=3, C=4.5, C $= B * A.
B = 1.5__1.5                            <<< precise result

?- C $= B * A, C=1, A=10.
B = 0.099999999999999992__0.1           <<< imprecise but accurate result
There are 2 delayed goals.

?- ln(X) $>= sin(X).
X = X{0.36787944117144228 .. 1.0Inf}    <<< partially solved
There are 3 delayed goals.

, , , @mat, ( ).

+1

CLP (FD) .

, :

  • . . CLP (Q).
  • , CLP (FD).

, . (2) , CLP (FD) , , .

. :

?- afn(200, -105, C).
C = -21000.

. , .

, , . , , Prolog  .

+3

All Articles