home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / SHDK_2.ZIP / TESTFIN.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-06-16  |  15.8 KB  |  436 lines

  1. {$I SHDEFINE.INC}
  2.  
  3. {$I SHUNITSW.INC}
  4.  
  5. {$V-}
  6.  
  7. unit TestFin;
  8. {This unit tests the ShFinanc unit over a range of values. Both Ordinary
  9.  Annuities and Annuities Due are tested.}
  10.  
  11. interface
  12.  
  13. uses
  14.   TpCrt,
  15.   TpDos,
  16.   TpString,
  17.   shFinanc,
  18.   shErrMsg;
  19.  
  20. procedure TestFinance;
  21.  
  22. implementation
  23.  
  24. {$IFOPT N-}
  25. type
  26.   extended = real;
  27. {$ENDIF}
  28.  
  29. const
  30.   TestDesc  : array[1..6] of string[65] =
  31.       ('In the following, for each set of I(nterest rate), N(umber of',
  32.        'payments), and T(ype of annuity, an expected result for the',
  33.        'calculation being performed will be displayed, followed by the',
  34.        'actual result. The expected results are taken from the appro-',
  35.        'priate tables in Smail, L.L., "Mathematics of Finance",',
  36.        'McGraw-Hill, 1953.');
  37.  
  38.   IntTstDsc : array[1..10] of string[65] =
  39.        ('In the following interactive test, you will be asked for three',
  40.         'values; Interest (if percentage suffix with a "%"), Number of Pay-',
  41.         'ment Periods, and Type of the annuity ((O)rdinary (i.e., payments',
  42.         'are made at the END of the conversion period), and (D)ue (the',
  43.         'payments are made at the BEGINNING of the conversion period)).',
  44.         'Following input of the annuity type, calculated results will be',
  45.         'displayed.',
  46.         '',
  47.         'The sequence will be repeated as often as desired. Use an inter-',
  48.         'est rate of 0 to exit the program.');
  49.  
  50.   NumI  = 4;
  51.   NumN  = 4;
  52.   NumT  = 4;
  53.   NumTst= 6;
  54.   TestTtl   : array[1..NumTst] of string[25] =
  55.                ('Compound Amount',
  56.                 'Compound Present Value',
  57.                 'Annuity Amount',
  58.                 'Annuity Present Value',
  59.                 '''N'' from Ann. Pres. Val.',
  60.                 '''I'' from Ann. Pres. Val.');
  61.   TestsI    : array[1..NumI] of extended  =
  62.                 (0.005, 0.030, 0.050, 0.080);
  63.   TestsN    : array[1..NumN] of integer   =
  64.                 (  1, 33, 70, 100);
  65.   TestsT    : array[AnnType] of string[18] =
  66.                 ('Ordinary Annuity', 'Annuity Due');
  67.   TestsE    : array[1..NumI, 1..NumN, AnnType, 1..NumT] of extended =
  68.        (
  69.        {I = 0.005}
  70.          (
  71.          {N = 1}
  72.            (
  73.            {T = Ordinary}
  74.              {Comp. Amt}   {Pres. Val}  {Ann.  Amt}  {Ann Pres Val}
  75.              (   1.0050000, 0.9950249,     1.0000000,  0.9950249   ),
  76.            {T = Due}
  77.              {Comp. Amt}   {Pres. Val}  {Ann.  Amt}  {Ann Pres Val}
  78.              (   1.0050000, 0.9950249,     1.0050000,  1.0000000   )
  79.                                                    {End of N =   1}),
  80.  
  81.          {N = 33}
  82.            (
  83.            {T = Ordinary}
  84.              {Comp. Amt}   {Pres. Val}  {Ann.  Amt}  {Ann Pres Val}
  85.              (   1.1789083, 0.8482424,    35.7816669, 30.3515259   ),
  86.            {T = Due}
  87.              {Comp. Amt}   {Pres. Val}  {Ann.  Amt}  {Ann Pres Val}
  88.              (   1.1789083, 0.8482424,    35.9605752, 30.5032835   )
  89.                                                    {End of N =  33}),
  90.  
  91.          {N = 70}
  92.            (
  93.            {T = Ordinary}
  94.              {Comp. Amt}   {Pres. Val}  {Ann.  Amt}  {Ann Pres Val}
  95.              (   1.4178305, 0.7053029,    83.5661055, 58.9394176   ),
  96.            {T = Due}
  97.              {Comp. Amt}   {Pres. Val}  {Ann.  Amt}  {Ann Pres Val}
  98.              (   1.4178305, 0.7053029,    83.9839360, 59.2341147   )
  99.                                                    {End of N =  70}),
  100.  
  101.          {N = 100}
  102.            (
  103.            {T = Ordinary}
  104.              {Comp. Amt}   {Pres. Val}  {Ann.  Amt}  {Ann Pres Val}
  105.              (   1.6466685, 0.6072868,   129.3336984, 78.5426448   ),
  106.            {T = Due}
  107.              {Comp. Amt}   {Pres. Val}  {Ann.  Amt}  {Ann Pres Val}
  108.              (   1.6466685, 0.6072868,   129.9803669, 78.9353580   )
  109.                                                    {End of N = 100})
  110.                                                  {End of I = 0.005}),
  111.  
  112.        {I = 0.030}
  113.          (
  114.          {N = 1}
  115.            (
  116.            {T = Ordinary}
  117.              {Comp. Amt}   {Pres. Val}  {Ann.  Amt}  {Ann Pres Val}
  118.              (   1.0300000, 0.9708738,     1.0000000,  0.9708738   ),
  119.            {T = Due}
  120.              {Comp. Amt}   {Pres. Val}  {Ann.  Amt}  {Ann Pres Val}
  121.              (   1.0300000, 0.9708738,     1.0300000,  1.0000000   )
  122.                                                    {End of N =   1}),
  123.  
  124.          {N = 33}
  125.            (
  126.            {T = Ordinary}
  127.              {Comp. Amt}   {Pres. Val}  {Ann.  Amt}  {Ann Pres Val}
  128.              (   2.6523352, 0.3770262,    55.0778413, 20.7657918   ),
  129.            {T = Due}
  130.              {Comp. Amt}   {Pres. Val}  {Ann.  Amt}  {Ann Pres Val}
  131.              (   2.6523352, 0.3770262,    56.7301765, 21.3887656   )
  132.                                                    {End of N =  33}),
  133.  
  134.          {N = 70}
  135.            (
  136.            {T = Ordinary}
  137.              {Comp. Amt}   {Pres. Val}  {Ann.  Amt}  {Ann Pres Val}
  138.              (   7.9178219, 0.1262974,   230.5940637, 29.1234214   ),
  139.            {T = Due}
  140.              {Comp. Amt}   {Pres. Val}  {Ann.  Amt}  {Ann Pres Val}
  141.              (   7.9178219, 0.1262974,   237.5118856, 29.9971240   )
  142.                                                    {End of N =  70}),
  143.  
  144.          {N = 100}
  145.            (
  146.            {T = Ordinary}
  147.              {Comp. Amt}   {Pres. Val}  {Ann.  Amt}  {Ann Pres Val}
  148.              (  19.2186320, 0.0520328,   607.2877327, 31.5989053   ),
  149.            {T = Due}
  150.              {Comp. Amt}   {Pres. Val}  {Ann.  Amt}  {Ann Pres Val}
  151.              (  19.2186320, 0.0520328,   625.5063647, 32.5468725   )
  152.                                                    {End of N = 100})
  153.                                                  {End of I = 0.030}),
  154.  
  155.        {I = 0.050}
  156.          (
  157.          {N = 1}
  158.            (
  159.            {T = Ordinary}
  160.              {Comp. Amt}   {Pres. Val}  {Ann.  Amt}  {Ann Pres Val}
  161.              (   1.0500000, 0.9523810,     1.0000000,  0.9523810   ),
  162.            {T = Due}
  163.              {Comp. Amt}   {Pres. Val}  {Ann.  Amt}  {Ann Pres Val}
  164.              (   1.0500000, 0.9523810,     1.0500000,  1.0000000   )
  165.                                                    {End of N =   1}),
  166.  
  167.          {N = 33}
  168.            (
  169.            {T = Ordinary}
  170.              {Comp. Amt}   {Pres. Val}  {Ann.  Amt}  {Ann Pres Val}
  171.              (   5.0031885, 0.1998725,    80.0637708, 16.0025492   ),
  172.            {T = Due}
  173.              {Comp. Amt}   {Pres. Val}  {Ann.  Amt}  {Ann Pres Val}
  174.              (   5.0031885, 0.1998725,    84.0669593, 16.8026767   )
  175.                                                    {End of N =  33}),
  176.  
  177.          {N = 70}
  178.            (
  179.            {T = Ordinary}
  180.              {Comp. Amt}   {Pres. Val}  {Ann.  Amt}  {Ann Pres Val}
  181.              (  30.4264255, 0.0328662,   588.5285107, 19.3426766   ),
  182.            {T = Due}
  183.              {Comp. Amt}   {Pres. Val}  {Ann.  Amt}  {Ann Pres Val}
  184.              (  30.4264255, 0.0328662,   617.9549362, 20.3098104   )
  185.                                                    {End of N =  70}),
  186.  
  187.          {N = 100}
  188.            (
  189.            {T = Ordinary}
  190.              {Comp. Amt}   {Pres. Val}  {Ann.  Amt}  {Ann Pres Val}
  191.              ( 131.5012578, 0.0076045,  2610.0251569, 19.8479102   ),
  192.            {T = Due}
  193.              {Comp. Amt}   {Pres. Val}  {Ann.  Amt}  {Ann Pres Val}
  194.              ( 131.5012578, 0.0076045,  2740.5264147, 20.8403057   )
  195.                                                    {En of N = 100})
  196.                                                  {End of I = 0.050}                  ),
  197.  
  198.        {I = 0.080}
  199.          (
  200.          {N = 1}
  201.            (
  202.            {T = Ordinary}
  203.              {Comp. Amt}   {Pres. Val}  {Ann.  Amt}  {Ann Pres Val}
  204.              (   1.0800000, 0.9259259,   1.0000000,    0.9259259   ),
  205.            {T = Due}
  206.              {Comp. Amt}   {Pres. Val}  {Ann.  Amt}  {Ann Pres Val}
  207.              (   1.0800000, 0.9259259,   1.0800000,    1.0000000  )
  208.                                                   {End of N =   1}),
  209.  
  210.          {N = 33}
  211.            (
  212.            {T = Ordinary}
  213.              {Comp. Amt}   {Pres. Val}  {Ann.  Amt}  {Ann Pres Val}
  214.              (  12.6760496, 0.0788889,   145.9506204, 11.5138884   ),
  215.            {T = Due}
  216.              {Comp. Amt}   {Pres. Val}  {Ann.  Amt}  {Ann Pres Val}
  217.              (  12.6760496, 0.0788889,   157.6266700, 12.4349995   )
  218.                                                    {End of N =  33}),
  219.  
  220.          {N = 70}
  221.            (
  222.            {T = Ordinary}
  223.              {Comp. Amt}   {Pres. Val}  {Ann.  Amt}  {Ann Pres Val}
  224.              ( 218.6064059, 0.0045744,  2720.0800738, 12.4428196   ),
  225.            {T = Due}
  226.              {Comp. Amt}   {Pres. Val}  {Ann.  Amt}  {Ann Pres Val}
  227.              ( 218.6064059, 0.0045744,  2937.6864797, 13.4382452   )
  228.                                                    {End of N =  70}),
  229.  
  230.          {N = 100}
  231.            (
  232.            {T = Ordinary}
  233.              {Comp. Amt}   {Pres. Val}  {Ann.  Amt}  {Ann Pres Val}
  234.              (2199.7612563, 0.0004546, 27484.5157043, 12.4943176   ),
  235.            {T = Due}
  236.              {Comp. Amt}   {Pres. Val}  {Ann.  Amt}  {Ann Pres Val}
  237.              (2199.7612563, 0.0004546, 29683.2769606, 13.4938630   )
  238.                                                    {End of N = 100})
  239.                                                  {End of I = 0.080})
  240.                                          {End of TestsE Definition});
  241.  
  242.   RW  : byte = 14;    {Field width for real-type reference values}
  243.   RP  : byte =  7;    {Decimal places for real-type reference values}
  244.   ISp =  5;           {Interest lead-in spacing}
  245.   NSp =  7;           {Num Pay  lead-in spacing}
  246.   TSp =  9;           {Type     lead-in spacing}
  247.   FSp = 11;           {Function lead-in spacing}
  248.   VSp = 13;           {Value    lead-in spacing}
  249.  
  250. procedure TestFinance;
  251.   var
  252.     ISt : string;
  253.     EC  : integer;
  254.     E1,
  255.     I,
  256.     APV,
  257.     IPV : extended;
  258.     N   : integer;
  259.     T   : AnnType;
  260.     TT  : string;
  261.     IxI,                            {Interest index}
  262.     IxN,                            {Payment periods index}
  263.     IxTT: ShortInt;                 {Tests index}
  264.     IxT : AnnType;                  {Annuity type index}
  265.     S1  : string;
  266.     O   : text;
  267.  
  268.   procedure AnyKey;
  269.     begin
  270.       if HandleIsConsole(1) then begin
  271.         Write('Any key to continue... ');
  272.         if ReadKey = #0 then ;
  273.         GoToXY(1, WhereY);
  274.         DelLine;
  275.         end;
  276.       end;
  277.  
  278.   procedure WriteValue(E : extended);
  279.     begin
  280.       S1 := finErrMsg(finErrCode);
  281.       if S1 = '' then
  282.         WriteLn(O, E:FW:DP)
  283.       else
  284.         WriteLn(O, S1);
  285.       end;
  286.  
  287.   procedure WriteIvalue(I : integer);
  288.     begin {WriteIvalue}
  289.       S1 := finErrMsg(finErrCode);
  290.       if S1 = '' then
  291.         WriteLn(O, I:IW)
  292.       else
  293.         WriteLn(O, S1);
  294.       end; {WriteIvalue}
  295.  
  296.   begin {TestFinance}
  297.     if OpenStdDev(O, 1) then ;
  298.     CheckOn;
  299.     finErrCheckOff;
  300.  
  301.     WriteLn(O);
  302.     for N := 1 to 6 do
  303.       WriteLn(O, '':5,TestDesc[N]);
  304.  
  305.     for IxI := 1 to NumI do begin
  306.       I := TestsI[IxI];
  307.       for IxN := 1 to NumN do begin
  308.         N := TestsN[IxN];
  309.         for IxT := Ordinary to Due do begin
  310.           for IxTT := 1 to NumTst do begin
  311.             case IxTT of
  312.               1 : begin
  313.                     WriteLn(O);
  314.                     AnyKey;
  315.                     WriteLn(O, '':ISp,'For I = ',(I*100.0):3:1,'%');
  316.                     WriteLn(O, '':NSp,'For N = ', N:3);
  317.                     WriteLn(O, '':TSp,'For Type = ',TestsT[IxT]);
  318.  
  319.                     WriteLn(O, '':FSp,'For Function = ',TestTtl[IxTT]);
  320.                     Write  (O, '':VSp,'Calculated = ');
  321.                     WriteValue(CompAmount(N, I));
  322.                     WriteLn(O, '':VSp,'Reference  = ',
  323.                                   TestsE[IxI, IxN, IxT, IxTT]:RW:RP);
  324.                     end;
  325.               2 : begin
  326.                     WriteLn(O, '':FSp,'For Function = ',TestTtl[IxTT]);
  327.                     Write  (O, '':VSp,'Calculated = ');
  328.                     WriteValue(CompPresVal(N, I));
  329.                     WriteLn(O, '':VSp,'Reference  = ',
  330.                                   TestsE[IxI, IxN, IxT, IxTT]:RW:RP);
  331.                     end;
  332.               3 : begin
  333.                     WriteLn(O, '':FSp,'For Function = ',TestTtl[IxTT]);
  334.                     Write  (O, '':VSp,'Calculated = ');
  335.                     WriteValue(AnnuityAmount(N, I, IxT));
  336.                     WriteLn(O, '':VSp,'Reference  = ',
  337.                                   TestsE[IxI, IxN, IxT, IxTT]:RW:RP);
  338.                     end;
  339.               4 : begin
  340.                     WriteLn(O, '':FSp,'For Function = ',TestTtl[IxTT]);
  341.                     Write  (O, '':VSp,'Calculated = ');
  342.                     APV := AnnuityPresVal(N, I, IxT);
  343.                     WriteValue(APV);
  344.                     WriteLn(O, '':VSp,'Reference  = ',
  345.                                   TestsE[IxI, IxN, IxT, IxTT]:RW:RP);
  346.                     end;
  347.               5 : begin
  348.                     WriteLn(O, '':FSp,'For Function = ',TestTtl[IxTT]);
  349.                     Write  (O, '':VSp,'Calculated = ');
  350.                     WriteIvalue(NumPay(APV, I, IxT));
  351.                     WriteLn(O, '':VSp,'Reference  = ',
  352.                                   N:IW);
  353.                     end;
  354.               6 : begin
  355.                     WriteLn(O, '':FSp,'For Function = ',TestTtl[IxTT]);
  356.                     Write  (O, '':VSp,'Calculated = ');
  357.                     WriteValue(IfromPresVal(APV, N, IxT, 1.0E-9));
  358.                     WriteLn(O, '':VSp,'Reference  = ',
  359.                                   I:RW:RP);
  360.                     end;
  361.               end; {case}
  362.             end; {for IxTT}
  363.  
  364.             if (IxT = Due)                  and
  365.               ((IxN < NumN)       or
  366.                (IxI < NumI))                and
  367.                (not (HandleIsConsole(1)))   then begin
  368.               Write(O, ^L);
  369.               end;
  370.  
  371.           end; {for IxT}
  372.         end; {for IxN}
  373.       end; {for IxI}
  374.  
  375.     WriteLn(O);
  376.     AnyKey;
  377.  
  378.     if not HandleIsConsole(1) then begin
  379.       WriteLn(O, '':TSp,
  380.               'Interactive mode testing not available under redirection.');
  381.       Close(O);
  382.       exit;
  383.       end;
  384.  
  385.     WriteLn;
  386.     for N := 1 to 10 do
  387.       WriteLn('':5,IntTstDsc[N]);
  388.     WriteLn;
  389.  
  390.     repeat
  391.       write('     ..Interest rate » '); readln(ISt);
  392.       Val(ISt, I, EC);
  393.       if (EC <> 0) and (ISt[EC] = '%') then begin
  394.         ISt[0] := char(pred(EC));
  395.         Val(ISt, I, EC);
  396.         if EC = 0 then begin
  397.           I := I / 100.0;
  398.           end
  399.         else begin
  400.           I := 0.0;
  401.           end;
  402.         end;
  403.       if I <= 0.0 then
  404.         exit;
  405.       write('Number of payments » '); readln(N);
  406.       write('   Type of annuity » '); readln(TT);
  407.       finErrCheckOff;
  408.       case Upcase(TT[1]) of
  409.         'O' : T := Ordinary;
  410.         'D' : T := Due;
  411.         end; {case}
  412.       APV := AnnuityPresVal(N, I, T);
  413.       writeln;
  414.       writeln('Annuity present value = ',APV:FW:DP);
  415.       write(finErrMsg(finErrCode)); if WhereX <> 1 then WriteLn;
  416.       writeln('Number of payments = ',NumPay(APV, I, T):IW);
  417.       write(finErrMsg(finErrCode)); if WhereX <> 1 then WriteLn;
  418.       writeln;
  419.       writeln('Ann. pres. val. as discounted ann. amt. = ',
  420.                (CompPresVal(N, I) * AnnuityAmount(N, I, T)):FW:DP);
  421.       write(finErrMsg(finErrCode)); if WhereX <> 1 then WriteLn;
  422.       writeln;
  423. {$IFDEF Gen87}
  424.       IPV := IfromPresVal(APV, N, T, 1.0E-15);
  425. {$ELSE}
  426.       IPV := IfromPresVal(APV, N, T, 1.0E-09);
  427. {$ENDIF}
  428.       write(finErrMsg(finErrCode)); if WhereX <> 1 then WriteLn;
  429.       writeln('Interest rate = ',IPV:FW:DP);
  430.       writeln;
  431.       writeln(Center('* * * * * * * * * * * * * * * * *', 72));
  432.       writeln;
  433.       until false;
  434.     end; {TestFinance}
  435.   end.
  436.