Formatted output of IEEE exceptional values

Harald Anlauf anlauf@hep.tu-darmstadt.de
Wed Jul 13 20:53:00 GMT 2005


Hi,

since I cannot access bugzilla right now, I will describe the problem
here.  Somebody please file a PR.

Consider the following code:

program fmt_inf_nan
  implicit none
  integer :: i
  real    :: nan,neg_inf,pos_inf,zero,neg_nan
  character (len=6)  :: f1, f2
  character (len=80) :: fmt
  zero = 0.0
  pos_inf =  1.0/zero
  neg_inf = -1.0/zero
  nan = zero/zero
  neg_nan = - nan
  print *, 'FMT  |  neg inf  |  pos inf  |    nan    |  neg nan  |'
  do i = 1, 10
     write (f1, '("F",i0,".0")') i
     write (f2, '(i0,"X")') 11-i
     fmt = '(a6,4("|",' // trim (f2) // ',' // trim (f1) // '),"|")'
     !print *, fmt
     print fmt, f1, neg_inf, pos_inf, nan, neg_nan
  end do
end program fmt_inf_nan


gfortran prints:

 FMT  |  neg inf  |  pos inf  |    nan    |  neg nan  |
F1.0  |          *|          *|          *|          *|
F2.0  |         **|         **|         **|         **|
F3.0  |        Inf|        Inf|        NaN|        NaN|
F4.0  |       -Inf|       +Inf|        NaN|        NaN|
F5.0  |       -Inf|       +Inf|        NaN|        NaN|
F6.0  |       -Inf|       +Inf|        NaN|        NaN|
F7.0  |       -Inf|       +Inf|        NaN|        NaN|
F8.0  |   Infinity|   Infinity|        NaN|        NaN|
F9.0  |  -Infinity|  +Infinity|        NaN|        NaN|
F10.0 |  -Infinity|  +Infinity|        NaN|        NaN|


This is not too bad, but it exhibits some defects and/or
inconsistencies, in particular for F3.0 and F8.0, as well as the
superfluous "+" sign for positive infinity.

Formatted output of IEEE is BTW described in the F2003 draft standard.

-------------8<---------------> cut here <--------------8<--------------
10.6.1.2.1 F editing

The Fw.d edit descriptor indicates that the field occupies w
positions, the fractional part of which consists of d digits. When w
is zero, the processor selects the field width. [...]

For an internal value that is an IEEE infinity, the output field
consists of blanks, if necessary, followed by a minus sign for
negative infinity or an optional plus sign otherwise, followed by the
letters Inf or Infinity , right justified within the field. If w is
less than 3, the field is filled with asterisks, otherwise, if w is
less than 8, Inf is produced.

For an internal value that is an IEEE NaN, the output field consists
of blanks, if necessary, followed by the letters NaN and optionally
followed by up to w-5 nonblank processor-dependent characters enclosed
in parentheses, right justified within the field. If w is less than 3,
the field is filled with asterisks.

10.6.1.2.2 E and D editing

[...]

For an internal value that is an IEEE infinity or NaN, the form of the
output field is the same as for Fw.d.

(dto. for EN, ES, G)
-------------8<---------------> cut here <--------------8<--------------

As an example and for comparison, this is what xlf on AIX/Power does:
(xlf version 7.1, 8.1 and 9.1).

-------------8<---------------> cut here <--------------8<--------------
 FMT  |  neg inf  |  pos inf  |    nan    |  neg nan  |
F1.0  |          *|          *|          *|          *|
F2.0  |         **|         **|         **|         **|
F3.0  |        ***|        INF|        ***|        ***|
F4.0  |       -INF|        INF|       NaNQ|       ****|
F5.0  |       -INF|        INF|       NaNQ|      -NaNQ|
F6.0  |       -INF|        INF|       NaNQ|      -NaNQ|
F7.0  |       -INF|        INF|       NaNQ|      -NaNQ|
F8.0  |       -INF|        INF|       NaNQ|      -NaNQ|
F9.0  |       -INF|        INF|       NaNQ|      -NaNQ|
F10.0 |       -INF|        INF|       NaNQ|      -NaNQ|
-------------8<---------------> cut here <--------------8<--------------

I am not sure whether x86 supports signed NaNs and how important that
is.  (I am also not sure whether the output of xlf for NaN is entirely
consistent with the standard, but that is IBM's problem...)

Cheers,
-ha

P.S.: if you want to have some fun, run the above code thru the Intel
compiler ;-)



More information about the Fortran mailing list