Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Question on edit desciptors (formatting reals into text strings)

0 views
Skip to first unread message

J. F. Cornwall

unread,
Jan 6, 2009, 12:45:12 PM1/6/09
to
Hi all. Working in our antique legacy system (currently running on
Solaris, compiled using Sun's f95 compiler, and trying to make a small
enhancement to a display screen.

The old code took 4 reals (single precision), and converted them into
strings based on a local scheme for rounding numbers to specified
precision & max decimal places. The output for this display was limited
to a max of 3 decimal places, so a valid entry of -0.0006512 is rounded
and displayed as "-.001" according to our local rules.

Users requested that the actual number be displayed instead. I have
been trying various schemes to do this, and I have gotten the full
numbers displayed, but still a few little glitches I'd like to correct
before committing the change. Using hte right edit descriptor, if there
is one for what I want, would be the easiest, so I thought I'd ask here
as a search of online docs has not yet given me an answer...

The display is intended to look like:

1.0
OUTPUT = -.0006512 * (INPUT) + -.25335

(in a monospaced font, the 1.0 is the exponent for the (INPUT) field)

Here's what my current code actually gives me:

1.00000
OUTPUT = -6.51200E-04 * (INPUT) + -0.253350

I abandonded the idea of trying to use our rounding system (this is not
the type of data it was "designed" for several decades ago), and I'm
trying to use an internal file WRITE to convert the reals into strings I
can use (code below). I have list-directed formatting at the moment,
but the compiler's choices are, as you can see, not what I'd like. The
numbers in the equation can vary widely in range.

Any thoughts and suggestions welcomed!

Jim Cornwall
(yep, it's for work, US Geological Survey Water Resources, I don't speak
for them in any official capacity, etc.....)

======================================================================

SUBROUTINE NW_FORMAT_STD_EQUATION(
i EQUATION_A_VA,
i EQUATION_B_VA,
i EQUATION_C_VA,
i EQUATION_D_VA,
o LINE1,
o LINE2)

C **********************************************************************
C * ARGUMENT DECLARATIONS
C **********************************************************************

REAL equation_a_va
REAL equation_b_va
REAL equation_c_va
REAL equation_d_va
CHARACTER*133 line1
CHARACTER*133 line2

...

C **********************************************************************
C * LOCAL VARIABLE DECLARATIONS
C **********************************************************************

CHARACTER*16 val_A, val_B, val_C, val_D ! A,B,C, D elements
INTEGER*4 len_A, len_B, len_C, len_D ! lengths of values
INTEGER len

C XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
C
C Format the standard equation elements

IF (EQUATION_A_VA .GT. NW_CR4) THEN
WRITE (val_A,*) EQUATION_A_VA
ENDIF

IF (EQUATION_B_VA .GT. NW_CR4) THEN
WRITE (val_B,*) EQUATION_B_VA
ELSE
val_B = 'INPUT'
len_B = 5
ENDIF

IF (EQUATION_C_VA .GT. NW_CR4) THEN
WRITE (val_C,*) EQUATION_C_VA
ENDIF

IF (EQUATION_D_VA .GT. NW_CR4) THEN
WRITE (val_D,*) EQUATION_D_VA
ENDIF

C Combine the display values into two lines output:

CALL S_JSTRLF (val_A, 16)
len_A = NWF_STRLEN (val_A)

CALL S_JSTRLF (val_B, 16)
len_B = NWF_STRLEN (val_B)

CALL S_JSTRLF (val_C, 16)
len_C = NWF_STRLEN (val_C)

CALL S_JSTRLF (val_D, 16)
len_D = NWF_STRLEN (val_D)

! do line2 first
line2 = ' OUTPUT = ' // val_A(1:len_A) // ' * (' //
& val_B(1:len_B) // ')'
len = NWF_STRLEN (line2)

! now do line1, placing the start of exponent at ')' + 1
line1 = ' ' ! nothing else goes into line1
line1(len+1:) = val_C ! put val_C into blanked line
len = NWF_STRLEN (line1) ! readjust len

! now add the remainder of line2:
line2 = line2(1:len) // ' + ' // val_D(1:len_D)

C return to caller (passing line1 & line2 back up)

RETURN
END

glen herrmannsfeldt

unread,
Jan 6, 2009, 12:55:47 PM1/6/09
to
J. F. Cornwall <JCor...@cox.net> wrote:

> Hi all. Working in our antique legacy system (currently running on
> Solaris, compiled using Sun's f95 compiler, and trying to make a small
> enhancement to a display screen.

> The old code took 4 reals (single precision), and converted them into
> strings based on a local scheme for rounding numbers to specified
> precision & max decimal places. The output for this display was limited
> to a max of 3 decimal places, so a valid entry of -0.0006512 is rounded
> and displayed as "-.001" according to our local rules.

> Users requested that the actual number be displayed instead. I have
> been trying various schemes to do this, and I have gotten the full
> numbers displayed, but still a few little glitches I'd like to correct
> before committing the change. Using hte right edit descriptor, if there
> is one for what I want, would be the easiest, so I thought I'd ask here
> as a search of online docs has not yet given me an answer...

You might look at the G edit descriptor. That allows for
either F form or E, depending on the exponenet
of ten needed to display the value. You can choose
the number of digits after the decimal point. A
scale factor of 1 (from 1P) is also nice with G.

G isn't perfect, but it usually works pretty well.

-- glen

J. F. Cornwall

unread,
Jan 6, 2009, 2:29:59 PM1/6/09
to
glen herrmannsfeldt wrote:

Thanks, Glen - I'll play with it a bit and see if I can get it looking
acceptable to the user.

Jim

dpb

unread,
Jan 6, 2009, 4:27:49 PM1/6/09
to
On Jan 6, 1:29 pm, "J. F. Cornwall" <JCornw...@cox.net> wrote:
> glen herrmannsfeldt wrote:
> > J. F. Cornwall <JCornw...@cox.net> wrote:
>
...

> >>Users requested that the actual number be displayed instead. I have
> >>been trying various schemes to do this, and I have gotten the full
> >>numbers displayed, but still a few little glitches I'd like to correct
> >>before committing the change. Using hte right edit descriptor, if there
> >>is one for what I want, would be the easiest, so I thought I'd ask here
> >>as a search of online docs has not yet given me an answer...
>
> > You might look at the G edit descriptor. That allows for
> > either F form or E, depending on the exponenet
> > of ten needed to display the value. You can choose
> > the number of digits after the decimal point. A
> > scale factor of 1 (from 1P) is also nice with G.
>
> > G isn't perfect, but it usually works pretty well.
>
> > -- glen
>
> Thanks, Glen - I'll play with it a bit and see if I can get it looking
> acceptable to the user.
...

If the G scaling doesn't suit you're pretty much back to the
evaluation of the magnitude of the values and writing explicit formats
as a function of those values (to simply make explicit something I'm
pretty sure you already knew :) ).

--

J. F. Cornwall

unread,
Jan 6, 2009, 6:01:19 PM1/6/09
to
dpb wrote:

Yep :-( The sort of thing I was hoping not to have to do. Oh well,
won't be the first time and I bet it's not the last time!

Jim

Dave Allured

unread,
Jan 7, 2009, 3:41:53 AM1/7/09
to

If I am not mistaken, this is about the same question that was asked on
2008-dec-19 with subject line "avoiding trailing zeros". Check that for
some possible ideas.

This subroutine might be of help. Follow with adjustl() and trim() as
needed, to control right and left blank padding:

http://indra.com/~dallured/f90/real_to_string.f90

--Dave

J.F. Cornwall

unread,
Jan 7, 2009, 6:54:09 PM1/7/09
to
Dave Allured wrote:
> J. F. Cornwall wrote:
>

(snip)

>
> If I am not mistaken, this is about the same question that was asked on
> 2008-dec-19 with subject line "avoiding trailing zeros". Check that for
> some possible ideas.
>
> This subroutine might be of help. Follow with adjustl() and trim() as
> needed, to control right and left blank padding:
>
> http://indra.com/~dallured/f90/real_to_string.f90
>
> --Dave

Huh, I searched the group's archives on my server and somehow managed to
miss that thread... :-( However, your subroutine saved me a pretty
fair chunk of time in rolling my own scheme, thanks! :-) I did have to
come up with a way to do it without len_trim and adjustl (because of our
archaic homegrown make system and its refusal to deal with modern
constructs - grrrrrrr).

Jim

0 new messages