next up previous
Next: Finite Domains (I) Up: Adding Computation Domains: CLP Previous: Programming with CLP() (I)

Programming with CLP($\Re$) (II)

  $\mbox{$\bullet$}$
$\frac{e}{4} = \frac{1}{1} - \frac{1}{2} + \frac{1}{3} -
\frac{1}{4} + \cdots $ (converges very slowly)
  $\mbox{$\bullet$}$
CLP($\Re$) program:
is_E(N, E4*4):- is_E(N, 1, 1, E4).

is_E(0, Mult, Sign, 0).
is_E(N, Mult, Sign, Sign/Mult + RestE):-
N > 0,
is_E(N-1, Mult+1, -Sign, RestE).
  $\mbox{$\bullet$}$
The series added up to the 11$^{\rm th}$ term:
?- is_E(11, E).
E = 2.94618
  $\mbox{$\bullet$}$
How many terms needed to achieve a given accuracy?
?- abs(E-E1)<0.01, is_E(N,E), is_E(N+1,E1).
N = 400, E1 = 2.77757, E = 2.76759
  $\mbox{$\bullet$}$
abs(E-E1) < 0.01 is delayed and not evaluated until E and E1 have a known value
  $\mbox{$\bullet$}$
Only an example: can be made much more efficiently



Last modification: Thu Oct 7 12:04:03 MEST 1999 <webmaster@clip.dia.fi.upm.es>