home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / TURBOPAS / TPWRN.ZIP / TPWRN.PAS
Encoding:
Pascal/Delphi Source File  |  1985-12-28  |  6.2 KB  |  126 lines

  1. program TPWRN;
  2.      {TPWRN.PAS #1.02 85-06-20 TEST POWERN.PLB CALCULATION OF POWERS
  3.  
  4.           V01 L02 update on 85-06-20 by DEH to exhibit precision-maintenance
  5.                   difficulties with (1/7)**(-i) and exp(i*ln(7)) variations.
  6.               L01 update on 85-06-09 by DEH to compute error deviation of
  7.                   exp(ln) competitor a bit more carefully.
  8.               L00 created on 85-06-05 by Dennis E. Hamilton, just for
  9.                   demonstration of generally-correct overall operation of
  10.                   the algorithm in Turbo Pascal module POWERN.PLB.}
  11.  
  12. {$I POWERN.PLB } {Vintage 1.00}
  13.  
  14.  
  15. var    i: integer {counter of trial exponents};
  16.       p7: real {intermediate power of 7 to be checked};
  17.      ln7: real {logarithm of 7 used in comparison with exp(ln) method};
  18.      rv7: real {value of 1/7 used in showing precision loss};
  19.  
  20.      out: text {file variable used for direction of output as needed};
  21.  
  22. BEGIN {Testing the basic features of POWERN.PLB}
  23.  
  24. assign(out, 'CON:');
  25.    {V01 L02 change to a disk file when you want to capture the report in
  26.             a file for comparison, uploading, etc.}
  27. rewrite(out);
  28.  
  29. writeln(out, 'TPWRN> #1.02 85-06-20 TEST OF POWERN FUNCTION RESULTS');
  30. writeln(out);
  31.  
  32. writeln(out, '       I      (-1)**I      p = 7**I       (1/7)**-I - p',
  33.              '     exp(I*ln(7)) - p');
  34. writeln(out);
  35.  
  36. ln7 := ln(7.0);
  37. rv7 := 1.0/7.0;
  38. for i := 0 to 17
  39.  do begin
  40.     write(out, '      ', i:2);
  41.     write(out, PowerN(-1,i) :11:0);
  42.     p7 := PowerN(7.0,i);
  43.     write(out, p7 :16:0);
  44.        {It is important to use a number that is prime to the base.  Although
  45.         5 is easier to follow, it isn't so hot when Turbo-BCD is used.}
  46.     write(out, PowerN(rv7, -i) - p7 :20:13);
  47.        {V01 L02 use the inexact reciprocal to show how errors magnify}
  48.     writeln(out, exp(i*ln7) - p7 :21:13);
  49.        {V01 L02 show deviation of exp(i*ln(7)) on the same basis}
  50.     end;
  51.  
  52. close(out);
  53. END.
  54. {
  55. TPWRN> #1.02 85-06-20 TEST OF POWERN FUNCTION RESULTS
  56.  
  57.        I      (-1)**I      p = 7**I       (1/7)**-I - p     exp(I*ln(7)) - p
  58.  
  59.        0          1               1     0.0000000000000      0.0000000000000
  60.        1         -1               7     0.0000000000000     -0.0000000000146
  61.        2          1              49     0.0000000000582     -0.0000000003492
  62.        3         -1             343     0.0000000009313     -0.0000000032596
  63.        4          1            2401     0.0000000074506     -0.0000000298023
  64.        5         -1           16807     0.0000000596046     -0.0000003874302
  65.        6          1          117649     0.0000008344650     -0.0000026226044
  66.        7         -1          823543     0.0000057220459     -0.0000362396240
  67.        8          1         5764801     0.0000457763672     -0.0001296997070
  68.        9         -1        40353607     0.0003662109375     -0.0009155273438
  69.       10          1       282475249     0.0029296875000     -0.0126953125000
  70.       11         -1      1977326743     0.0234375000000     -0.0859375000000
  71.       12          1     13841287201     0.1875000000000     -0.6250000000000
  72.       13         -1     96889010407     1.3750000000000     -4.2500000000000
  73.       14          1    678223072850    11.0000000000000    -58.0000000000000
  74.       15         -1   4747561509900    80.0000000000000   -400.0000000000000
  75.       16          1  33232930570000   640.0000000000000  -1472.0000000000000
  76.       17         -1 232630513990000  4864.0000000000000 -20224.0000000000000
  77.  
  78. These results were obtained by assign(out, 'TPWRN.PRN') and compiling with
  79. POWERN.PLB #1.03.  CP/M-80 Turbo Pascal version 3.00A was used.
  80.  
  81.    Similar results should be obtained using MS-DOS, CP/M-86 and IBM PC 
  82. versions of Turbo Pascal.  Turbo-8087 should obtain better results in all
  83. columns because of the greater precision maintained internally.  Turbo-BCD
  84. should also show improvements, with decreased speed, after the last column
  85. is either dropped or library procedures for BCD ln() and exp() are obtained.
  86. (For Turbo-BCD and Turbo-8087 both, it is instructive to increase the number
  87. of decimal positions in the last two columns in order to see what error there
  88. is, however much smaller it turns out to be.)
  89.  
  90.    Note that, in this test, the 7**I powers are produced quite rapidly and
  91. exactly with I<14.  Thereafter, the 39-bit effective precision is inadequate
  92. for maintenance of an exact result.
  93.  
  94.    On the other hand, the use of (1/7)**-I, although MATHEMATICALLY the
  95. same as 7**I, deteriorates much more quickly.  That is because 1/7 cannot
  96. be carried exactly, and this error is quickly magnified in the taking of
  97. powers.  By I=14, the error has grown to 10, just as minimum error shows
  98. in the lowest digit of 7**I.  (Handling of the 1/7-th column may also be
  99. affected by rounding characteristics of this compiler's floating-point
  100. implementation.  We are less concerned, here, with placing blame than with
  101. showing the problem to be inevitable unless other steps are taken.)
  102.  
  103.    The final column shows how much more quickly the calculus textbook approach,
  104. using exp(I*ln(7)), breaks down.  Since this method is NOTICEABLY slower as
  105. well, there is nothing to commend it.
  106.  
  107.    In making use of these results, keep in mind that the PowerN vintage 1.xx
  108. Algorithms are the best available in terms of direct solution at minimum cost.
  109. Nevertheless, working in the same precision as the input data dooms us to some
  110. sort of error.  This may be tolerable, but it is well to know of its presence.
  111.  
  112.    If taking powers still seems the best approach in the problem at hand, but
  113. maximum precision must be obtained, more complex techniques using expanded-
  114. precision internal values must be substituted in the implementation of PowerN.
  115. The techniques are not directly realizable in Pascal, however.  It is necessary
  116. to "get under the hood" in order to expand the precision of calculation.  That
  117. is what makes finding an alternative problem approach all the more attractive.
  118. (For further background on these techniques, consult the bibliographical notes
  119. appended to the POWERN.PLB module.)
  120.  
  121.                                        -- Dennis E. Hamilton
  122.                                           1985 June 20                  }
  123.  
  124.  
  125.                        (* end of TPWRN.PAS *)
  126.