|
Il est très agaçant d'avoir des formules toutes préparés d'avance fonctionnant très bien dans des tableurs et
ne pas être capable d'effectuer les mêmes calculs et les mêmes réponses dans une situation anodine de la programmation.
Une de ces remarquables fonctions, est celle du Lotus 1-2-3 et de Quattro Pro, elle se nomme la fonction PPaymt en anglais ou Principal en français.
A l'aide du code source Pascal suivant, vous trouverez la réponse que vous souhaitez:
Program PPaymtSamples;
Function Abs(x:Real):Real;Begin
If x < 0 Then x := -x;
Abs := x;
End;
Function Exp(x:Real):Real;
Var
Inverse:Boolean;
n,i:Integer;
dl,q:Real;
Begin
Inverse := False;
n := 0;
dl := 1;
i := 1;
If x < 0 Then Begin
Inverse := True;
x := -x;
End;
While x >= 2 do Begin
x := x / 2;
n := n + 1;
End;
x := x / 16;
n := n + 4;
q := x;
While q > 1.0E-15 do Begin
dl := dl + q;
i := i + 1;
q := q * x / i;
End;
For i := 1 to n do dl := dl * dl;
If Inverse Then dl := 1 / dl;
Exp := dl;
End;
Function SquareRoot(X:Real):Real;
Var
A,B,M,XN:Real;
Begin
If X=0.0Then Begin
SquareRoot:=0.0;
End
Else
Begin
M:=1.0;
XN:=X;
While XN>=2.0 do Begin
XN:=0.25*XN;
M:=2.0*M;
End;
While XN<0.5 do Begin
XN:=4.0*XN;
M:=0.5*M;
End;
A:=XN;
B:=1.0-XN;
Repeat
A:=A*(1.0+0.5*B);
B:=0.25*(3.0+B)*B*B;
Until B<1.0E-15;
SquareRoot:=A*M;
End;
End;
Function Ln(x:Real):Real;
Var
negatif:Boolean;
fois,i:Integer;
ajout,savx,xp,quotient,dl:Real;
Begin
negatif := False;
fois := 1;
ajout := 0;
If x <= 0.0 Then Begin
Ln:=0;
Exit;
End;
If x < 1.0 Then Begin
negatif := True;
x := 1.0 / x;
End;
While x >= 10.0 do Begin
x := x / 10.0;
ajout := ajout + 2.302585092994046;
End;
While x >= 1.1 do Begin
x := SquareRoot(x);
fois := fois * 2;
End;
x := x - 1;
savx := x;
i := 2;
xp := x * x;
quotient := (xp / i);
dl := x - quotient;
While 1.0E-15 < quotient do Begin
i := i + 1;
xp := xp * x;
dl := dl + (xp / i);
i := i + 1;
xp := xp * x;
quotient := (xp / i);
dl := dl - quotient;
End;
dl := dl * fois;
dl := dl + ajout;
If(negatif)Then dl := - dl;
Ln:=dl;
End;
Function FVal(Rate,Nper,Pmt,PV,PType:Real):Real;Near;
Var
F:Real;
Begin
F:=Exp(NPer*Ln(1+Rate));
If Abs(Rate)<1E-6Then
FVal:=-Pmt*Nper*(1+(Nper-1)*Rate/2)*(1+Rate*PType)-PV*F
Else
FVal:=Pmt*(1-F)*(1/Rate+PType)-PV*F;
End;
Function Paymt(Rate,NPer,PV,FV,PType:Real):Real;Near;
Var
F:Real;
Begin
F:=Exp(Nper*Ln(1+Rate));
Paymt:=(FV+PV*F)*Rate/((1+Rate*PType)*(1-F));
End;
Function PPaymt(Rate,Per,NPer,PV,FV,PType:Real):Real;Near;
Var
F:Real;
Begin
F:=Paymt(Rate,NPer,PV,FV,PType);
PPaymt:=F-Rate*FVal(Rate,Per-PType-1,F,PV,PType);
End;
BEGIN
WriteLn('Exemple de versements trimestriels d''un prêt de 10 000$ à 15% par trimestre:');
WriteLn(PPaymt(0.15/4,24,40,10000,0,1):3:3,'$');
WriteLn('Exemple de versements d''un prêt de 100 000$ à 10% par mois:');
WriteLn(PPaymt(0.1/12,2*12,30*12,100000,0,0):3:3,'$');
END.
|
on obtiendra le résultat suivant:
Exemple de versements trimestriels d'un prêt de 10 000$ à 15% par trimestre:
-233.243
Exemple de versements d'un prêt de 100 000$ à 10% par mois:
-53.542
|
|