For Programmers: Free Programming Magazines  


Home > Archive > Fortran > May 2005 > Maximum Precision Format Descriptor









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Maximum Precision Format Descriptor
Ron Shepard

2005-05-13, 4:10 pm

The other threads about formats reminded me of something that I've
wanted included in the language specification for a long time. I
would like to specify the field width of a floating point number,
and have always the maximum possible number of digits printed within
that field. This means that positive numbers would not have a +
sign or a space, and positive exponents would not have a + sign.
The number would be printed either like an fw.n or an ew.n,
depending on which would result in the most digits printed. If an
ew.n kind of format is used, then the width of the exponent field
would be minimized. The decimal point would not be printed unless
it is necessary.

I have written code to do this in the past, but it requires that the
output fields are created as character variables and placed into a
character output buffer. This works alright in some cases, but in
other cases you really want to just specify it in the normal way
with a format descriptor. I think something like "em", would be a
good way to denote this (analogous to "es" in f90).

Here are some examples with an "em6" field to show what I'm talking
about:

123.45678 would be printed as "123.46"

-123.45678 would be printed as "-123.5"

1.2345678e+07 would be printed as "1235e4"

1.2345678e-07 would be printed as "123e-9"

-1.2345678e-07 would be printed as "-12e-8"

1.2345678e+20 would be printed as "123e18"

1.2345678e+200 would be printed as "12e199"

-1.2345678e+200 would be printed as "-1e200"

I guess there should be a way to specify that the "e" could be
dropped when there is a negative exponent. Maybe "em6e0" or "em6.0"
or something like that. In any case, the "em" output forms should
be consistent with "e" and "f" input formats and with list-directed
i/o.

$.02 -Ron Shepard
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com