Sun Studio 12: Fortran Programming Guide

7.8.1 Results Are Close, but Not Close Enough

Try the following:


      real*4 x,y
      x=99999990e+29
      y=99999996e+29
      write (*,10) x, x
 10   format(’99,999,990 x 10^29 = ’, e14.8, ’ = ’, z8)
      write(*,20) y, y
 20   format(’99,999,996 x 10^29 = ’, e14.8, ’ = ’, z8)
      end

The output is:


99,999,990 x 10^29 = 0.99999993E+37 = 7CF0BDC1
99,999,996 x 10^29 = 0.99999993E+37 = 7CF0BDC1

In this example, the difference is 6 x 1029. The reason for this indistinguishable, wide gap is that in IEEE single-precision arithmetic, you are guaranteed only six decimal digits for any one decimal-to-binary conversion. You may be able to convert seven or eight digits correctly, but it depends on the number.