home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / fortran / 3000 < prev    next >
Encoding:
Internet Message Format  |  1992-08-12  |  6.3 KB

  1. Path: sparky!uunet!usc!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!mp.cs.niu.edu!uxa.ecn.bgu.edu!psuvax1!psuvm!hdk
  2. Organization: Penn State University
  3. Date: Wed, 12 Aug 1992 15:00:39 EDT
  4. From: H. D. Knoble <HDK@psuvm.psu.edu>
  5. Message-ID: <92225.150039HDK@psuvm.psu.edu>
  6. Newsgroups: comp.lang.fortran
  7. Subject: Re: SUMMARY: precision of real numbers
  8. References:  <1992Aug11.193749.17659@colorado.edu>
  9. Lines: 109
  10.  
  11. Two excellent (less formal than ACM, but well qualified) practical papers
  12. dealing with precision of real numbers are published in the SHARE 75
  13. PROCEEDINGS, Volume 1, 14 August 1990, are:
  14.  
  15. 1) John R. Ehrman, "Floating Point Numbers and Arithmetic, a Tutorial".
  16.    This gives algebra of the Real Field of numbers that often do not hold in
  17.    the Floating Point approximations; then the IBM 370 and IEEE Floating Point
  18.    models are compared and many practical examples and consequences given
  19.    along with some very interesting questions about how to make the IEEE
  20.    model better.
  21.  
  22. 2) Herman D. Knoble, "Reality of REAL Arithmetic's Really Wrong Results".
  23.    This gives several examples and analyses of some catastrophic cases.
  24.    Lively teaching examples. Results shown for both IBM 370 and IEEE
  25.    Floating Point Models. Show these to the decision makers who believe
  26.    everything on paper with holes coming down the sides!
  27.  
  28. Example 1 and 3 in the second paper follow here. They may be useful
  29. if for no other benchmark then one in awareness that program results should
  30. be thoroughly checked before standing behind or believing any of them.
  31.  
  32. Example 1: For an apparently simple dollars and cents example whose result
  33.            algebraically is 1.0, the result computed using the IBM Floating
  34.            Floating Point model with the suggested input values of A=100.,
  35.            X=0.01 is not 1.0 but rather -39.06... ; using the IBM PC IEEE
  36.            Model the result is: 9.76... .  Please note both results are
  37.            "really wrong."  This is better than close or subtly wrong!
  38.            Try reversing the suggested input values for A and X and look
  39.            at the output, P; the result is P=0.999999...  and 1.00000...
  40.            respectively. Also try A=100. and X=0.0078125; on the IBM 370
  41.            model the result P=0.999999... ; using the IBM PC IEEE model
  42.            the result is P=0.000000... . This program is also useful as a
  43.            teaching tool to wake Computer Science College Seniors up just
  44.            before they graduate! Ask what one would do to prevent and to
  45.            detect problems like this as part of that exercise.
  46.  
  47. C==== Example 1: Dollars and Cents numeric breakdown; Try A=100., X=0.01.
  48. C     NOTE: P is algebraically 1.0 for all A and X<>0.
  49. C  Penn State University Center for Academic Computing
  50. C  H. D. Knoble, August 1979.
  51.        DO 1 I=1,99999
  52.          WRITE(6,*) 'Please enter A and X (or press Enter to Quit):'
  53.          WRITE(6,*) '(Try values 100., 0.01, then 0.01, 100.)'
  54.          READ(5,*,END=99) A,X
  55.          P=((A+X)**2 - A**2 - 2.*A*X)/X**2
  56.          WRITE(6,100) P,A,X
  57. 100      FORMAT(' P=',F14.7,' A=',F14.7,' X=',F14.7)
  58. 1      CONTINUE
  59. 99     WRITE(6,*) '** Example 1 Ended **'
  60.        STOP
  61.        END
  62.  
  63. Example 3. Results may be right (or wrong) seemingly by chance, even in the
  64.            same program. Using the IBM 370 Floating Point Model, the result,
  65.            "Swartz's Inequality holds" is output for X=Y=1 to X=Y=2901 by 1.
  66.            Then it "holds" and "fails" rather sporadically (but in a pattern)
  67.            from X=Y=2902. to 8190. by 1. Then it "holds" again from X=Y=8191
  68.            to X=Y=12743 by 1. It then appears to fail sporadically (but with
  69.            a changing pattern) above X=Y=12743. Using Double Precision only
  70.            changes where the "holding" and "failing" begin and end. Using
  71.            the IEEE Floating Point Model on the IBM PC, the result is "holds"
  72.            indefinitely, even well beyond where the limits where the
  73.            associative law of algebra breaks down for the arithmetic of this
  74.            model. So seemingly correct results can be obtained by a very
  75.            shaky program by chance alone; and floating point arithmetic
  76.            number systems are not closed with respect to addition and
  77.            multiplication. That is, there are X's and Y's in every
  78.            computer's floating point system such that X+Y, X-Y, X*Y,
  79.            and/or X/Y do not belong to this system. For example X=90010.
  80.            and Y=20.03125 (0.03125 is 1/32nd) both belong to the IBM 370
  81.            single precision floating point system; but neither their
  82.            sum, difference, product, or ratio do. Banks and (S & L's!)
  83.            do run into this problem trying to divide money into units
  84.            that don't exist; ever wonder in whose favor the results are
  85.            adjusted? Swartz's Inequality failing to hold shows that
  86.            floating point numbers do not obey both the associative and
  87.            distributive laws of algebra. That is, for all computers there
  88.            there are many X, Y, and Z's belonging to the system such that
  89.            (X+Y)+Z .NE. X+(Y+Z) and (X*Y)*Z .NE. X*(Y*Z) and (X+Y)*Z .NE.
  90.            X*Z+Y*Z. This too is a good example for students of numerical
  91.            computations to study; have them count and come up with a
  92.            simple ... notation to enumerate the floating point numbers
  93.            for a specific platform and model and then approximate what
  94.            proportion of that set obey some simple rules of algebra.
  95. C
  96. C==== Example 3, Swartz's Inequality.
  97. C  Example shows while it is true mathematically doesn't necessarily
  98. C  hold in straight-forward Fortran.
  99. C  Penn State University Center for Academic Computing
  100. C  H. D. Knoble, August 1979.
  101.       REAL X,Y, FROM,TO
  102.       WRITE(6,*)
  103.      * 'Please give 2 loop limits to run Swartz''s Inequality thru:'
  104.       WRITE(6,*) '(Try limits like: 1., 10.  2900., 2910.  8190., 8210.'
  105.       READ(5,*,END=99) FROM,TO
  106.       DO 1 X=FROM,TO,1.
  107.         Y=X
  108.         IF (2.*(X**2+Y**2) .GE. (X+Y)**2) THEN
  109.           WRITE(6,100) X,Y
  110. 100       FORMAT(' Swartz''s Inequality holds for X=',F6.1,' Y=',F6.1)
  111.         ELSE
  112.           WRITE(6,200) X,Y
  113. 200       FORMAT(' Swartz''s Inequality fails for X=',F6.1,' Y=',F6.1)
  114.         ENDIF
  115. 1     CONTINUE
  116. 99    WRITE(6,*) '** Example 3 Ended **'
  117.       STOP
  118.       END
  119. ---------------------------------------------------------End of examples
  120.