Next: , Previous: , Up: CLPFD Example Programs   [Contents][Index]


10.9.15.5 Mortgage Calculator

This small example, adapted from CLPQR Projection, illustrates the use of arithmetic relations over reals. It defines a simple mortgage calculator as a relation over:

mortgage(1, P, Interest, B, MP) :- !,
        B + MP $= P * (1.0 + Interest).
mortgage(T, P, Interest, B, MP) :-
        P1 + MP $= P * (1.0 + Interest),
        T1 is T - 1,
        mortgage(T1, P1, Interest, B, MP).

| ?- % if I deposit 1000.0 at 10% interest,
     % what is the balance if I withdraw 250.0 per year four times?
     mortgage(4, 1000.0, 0.10, B, 250.0), indomain(B).
B = 303.8500000000002

| ?- % if I deposit 1000.0 at 10% interest,
     % how much should I redraw to empty the account in four years?
     mortgage(4, 1000.0, 0.10, 0.0, MP), MP $>= 0.0, indomain(MP).
MP = 315.47080370609797

| ?- % At 10% interest, how much should I deposit
     % if I want the balance to be 2000.0 in four years?
     mortgage(4, P, 0.10, 2000.0, 0.0), indomain(P).
P = 1366.026910730141

Send feedback on this subject.