- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Can anyone help me, please?
I am trying to produce formatted output from a Fortran program that matches existing output from a a C++ program. In particular I have a set of real numbers, as follows:
real Fx, Fy, Fz, x, y
Fx = 1000.0
Fy = 0.0
Fz = 120.5
The output I want is spaced-delimited, as follows:
1000 0 120.5
Using the following Fortran statement:
write(file, '(3(f0.1, x)') Fx, Fy, Fz
the output I get is:
1000.0 .0 120.5
My questions are:
- How do I add the leading zero in front of the value for Fy?
- How do I suppress the training zero from Fx (and ideally the decimal point - although I could live with that if I have to)?
Note that the values of Fx, Fy, and Fz are unknown until run time.
As a C++ programmer most of the time, I am struggling to do this in Fortran.
Any help would be much appreciated.
Thanks in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@Bond__Andrew:
As a C++ programmer most of the time, I am struggling to do this in Fortran.
Any help would be much appreciated.
What you are looking for are some of the additional fine-grained controls that have not quite made it into the language due to a number of reasons.
The one with leading zeroes is something the next language revision (Fortran 202X) is slated to provide. But the one with trailing zeroes is missing, I'm afraid.
If this's of interest, a workaround you can consider is to write an extern "C" function in C++, given your background, and use it for your needs. You can see here for an example for integer to string that you can adapt as per your needs:
By the way, since your inquiry is general Fortran, you may want to also ask at this Fortran Discourse site for additional viewpoints.
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Fortran offers a lot of control over the output of numbers, but I am quite sure it does not give you control over the appearance of a leading zero or the suppression of trailing zeroes or decimal points.
If you really need this, then the way forward would be to:
- Write the number to a string using a suitable format (G0 for instance)
- Manipulate the appearance in the resulting string
- Write out the string
Something along these lines:
write(*,*) formcpp(fx), formcpp(fy), formcpp(fz)
where the function formcpp uses an internal write to convert the value to a string. Whether it is worth the extra work, is up to you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I'll comment that the next revision of the Fortran standard is likely to contain a new feature to give you control over leading zeros in formatted output. It doesn't help today, but it will come.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
"... the next revision of the Fortran standard is likely to ... give you control over leading zeros in formatted output. It doesn't help today, but it will come."
Thanks for this, Steve.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Markus
Thanks for the quick reply. I feared as much.
Unfortunately I need to reproduce the exact format to allow automated comparison of results produced by two separate programs (one written in Fortran, one in C++).
However, your proposed solution looks like it could be made to work ... thanks for suggesting it.
Andrew
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@Bond__Andrew:
As a C++ programmer most of the time, I am struggling to do this in Fortran.
Any help would be much appreciated.
What you are looking for are some of the additional fine-grained controls that have not quite made it into the language due to a number of reasons.
The one with leading zeroes is something the next language revision (Fortran 202X) is slated to provide. But the one with trailing zeroes is missing, I'm afraid.
If this's of interest, a workaround you can consider is to write an extern "C" function in C++, given your background, and use it for your needs. You can see here for an example for integer to string that you can adapt as per your needs:
By the way, since your inquiry is general Fortran, you may want to also ask at this Fortran Discourse site for additional viewpoints.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks to all those who have replied to my post. This has been very helpful.
The solution I am pursuing at the moment is something like this:
call FormatReal(Fx, w, d)
write(file, '("Value of Fx = ", f<w>.<d>)') Fx
The subroutine FormatReal uses the value of Fx to determine appropriate values for w and d, whioch then control the width of the f format.
The allows me to add a leading 0, removed trailing zeroes, but not, unfortunately, get rid of the decimal point when appropriate.
This is close enough for my current purposes.
And I can see a full solution by adding a callback function from C/C++.
Thanks to all!
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page