For Programmers: Free Programming Magazines  


Home > Archive > Fortran > May 2005 > [F77] Format statement problem









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 [F77] Format statement problem
RR

2005-05-13, 4:10 pm

Hello,

I'm using Fortran 77.

I would like to write double precision values to a file
using the following code :

DO i=1,4
write(20,'(G15.8)') x(i)
ENDDO

but here is what I get

0.23977611-100
0.51974370-100
0.11097246E-99
0.23353172E-99

What is the command for getting the correct

0.23977611E-100
0.51974370E-100
0.11097246E-099
0.23353172E-099

Thanks in advance

RR

Herman D. Knoble

2005-05-13, 4:10 pm

Use the format: E16.8E3

Skip Knoble

On Thu, 12 May 2005 15:02:24 +0000 (UTC), rr@mesne.fr (RR) wrote:

-|Hello,
-|
-|I'm using Fortran 77.
-|
-|I would like to write double precision values to a file
-|using the following code :
-|
-| DO i=1,4
-| write(20,'(G15.8)') x(i)
-| ENDDO
-|
-|but here is what I get
-|
-| 0.23977611-100
-| 0.51974370-100
-| 0.11097246E-99
-| 0.23353172E-99
-|
-|What is the command for getting the correct
-|
-| 0.23977611E-100
-| 0.51974370E-100
-| 0.11097246E-099
-| 0.23353172E-099
-|
-|Thanks in advance
-|
-|RR

Ron Shepard

2005-05-13, 4:10 pm

In article <d5vr60$9hn$1@arcturus.ciril.fr>, rr@mesne.fr (RR)
wrote:

> Hello,
>
> I'm using Fortran 77.
>
> I would like to write double precision values to a file
> using the following code :
>
> DO i=1,4
> write(20,'(G15.8)') x(i)
> ENDDO
>
> but here is what I get
>
> 0.23977611-100
> 0.51974370-100
> 0.11097246E-99
> 0.23353172E-99
>
> What is the command for getting the correct
>
> 0.23977611E-100
> 0.51974370E-100
> 0.11097246E-099
> 0.23353172E-099


What you claim is "correct" is obviously not correct since the field
widths are 16 characters, and the format you specified is only 15
characters wide (don't forget about the sign character). Your
problem is that you are specifying implicitly an exponent width of 4
(the "e", the sign, and two numeric digits), but you want to print
3-digit exponents. The only way to achieve that without losing
essential information is to drop the "e", which the compiler
(correctly) does for you. Here are some options (written in f90
rather than f77):

integer, parameter :: wp=selected_real_kind(14)
real(wp) :: x(4) = (/ 0.23977611E-100_wp, 0.51974370E-100_wp, &
& 0.11097246E-099_wp, 0.23353172E-099_wp/)

write(*,'(g15.8)') x
write(*,*)
write(*,'(g16.8e3)') x
write(*,*)
write(*,'(1p,e15.7e3)') x
write(*,*)
write(*,'(es15.7e3)') x

end

Notice that the second example gives you what you appear to want.
However, both the first and second formats waste a character in the
format (the leading zero), which irks me more than losing the "e" in
the output. The last two formats solve both problems, allowing
3-digit exponents and without wasting the position because of the
leading zero. The third one is standard f77, the last one is f90
although it may be supported as an extension in your f77 compiler.

$.02 -Ron Shepard
Michael Metcalf

2005-05-13, 4:10 pm


"RR" <rr@mesne.fr> wrote in message news:d5vr60$9hn$1@arcturus.ciril.fr...
> Hello,
>

write(20,'(e16.8e3)') x(i)

is what you want. That specifies also the width of the exponent field
explicitly.

Regards,

Mike Metcalf


RR

2005-05-13, 4:10 pm

Thank you very much to you all for
your help and concise explanations.

Regards,

RR

Sponsored Links







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

Copyright 2009 codecomments.com