Re: Formatting a real with an I edit descriptor.
- From: jfh <john.harper@xxxxxxxxx>
- Date: Wed, 22 Apr 2009 16:22:28 -0700 (PDT)
On Apr 23, 8:55 am, Ron Shepard <ron-shep...@xxxxxxxxxxxxxxxxxx>
wrote:
In article <49edcf37$0$2705$cc2e3...@xxxxxxxxxxxxxx>,
LR <lr...@xxxxxxxxxxxxx> wrote:
Another thread in another group aroused my curiosity.
I was wondering what this snippet should produce according to the
standard. (I've been reading a working draft from 2004, but whatever the
latest version says would be better.)
real f
f = 10.0
write(*,'(i20,3x,f20.2)') f,f
FWIW, my compiler produces:
1092616192 10.00
but I'm not sure this is correct. Should it be:
10 10.00
Others have commented about what "correct" might mean with a nonstandard
program. Nonetheless, it appears that what you *want* to happen is
really given by something like
write(*,'(i20,3x,f20.2)') nint(f),f
Maybe it should be int(f) rather than nint(f), but you didn't specify
enough in your post to make sure. Look also at the floor(), ceiling(),
and mod() intrinsics, they all do similar but different things. Anyway,
my point is that the conversion from floating point to integer is done
with an intrinsic function reference, not by the format descriptor.
What your compiler is doing (and this is common) is to take the string
of bits that define the floating point number and print out its value
"as if" that same string of bits occurred in an integer variable. You
can do this in a standard way with the transfer() intrinsic. You can
also do it in a different nonstandard (but also common) way using the
equivalence statement. (Well I think this is nonstandard. I've done it
so many times assuming it was nonstandard, I don't know for sure.)
real f
integer if
equivalence (f,if)
f = 10.0
write(*,'(i20,3x,f20.2)') if,f
$.02 -Ron Shepard
OK - I'll ask the dumb question: what's nonstandard about Ron's code?
Equivalencing default real and default integer is explicitly allowed
in both the f95 and f2003 standards section 5.5.1, and I see no
failure to obey the 9 constraints in f95 or the 11 constraints in
f2003.
John Harper
.
- Follow-Ups:
- Re: Formatting a real with an I edit descriptor.
- From: Richard Maine
- Re: Formatting a real with an I edit descriptor.
- From: ken . fairfield
- Re: Formatting a real with an I edit descriptor.
- References:
- Formatting a real with an I edit descriptor.
- From: LR
- Re: Formatting a real with an I edit descriptor.
- From: Ron Shepard
- Formatting a real with an I edit descriptor.
- Prev by Date: Monster Energy Co. New Promote: White Monster Energy Cap Monster Energy New Era Cap Monster Energy Fitted Cap Monster Energy Cap Monster Energy New Era Hat Link: http://sites.google.com/site/monsterenergyhats/
- Next by Date: Re: misunderstanding about counting in Fortran
- Previous by thread: Re: Formatting a real with an I edit descriptor.
- Next by thread: Re: Formatting a real with an I edit descriptor.
- Index(es):
Relevant Pages
|