Gladir.com - Pascal - Finance - IPaymt/Interet de Lotus 1-2-3 et de Quattro Pro


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 IPaymt en anglais ou Interet en français. A l'aide du code source Pascal suivant, vous trouverez la réponse que vous souhaitez:
Program IPaymt;

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 IPAYMT(Rate,Per,NPer,PV,FV,PType:Real):Real;Begin
 IPayMt:=Rate*FVal(Rate,Per-PType-1,PayMt(Rate,NPer,PV,FV,PType),PV,PType);
End;

BEGIN
 WriteLn('Prêt hypothécaire de 30 ans à 15% de 200 000$:');
 WriteLn(IPAYMT(0.15/12.0,2*12,30*12,200000,2,0):4:2);
END.
on obtiendra le résultat suivant:
Prêt hypothécaire de 30 ans à 15% de 200 000$:
-2490.45$


Dernière mise à jour: Samedi, le 17 mars 2007