- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi folks
I can't seem to find anything on this issue, which kind of surprises me... I've looked around pretty thoroughly though.
I'd simply like to write to a file with a format that specifies the number of characters in the number, and the minimum number of dps. For instance, I need to write a line with two integers (2I5) followed by 6 reals (currently 6F8.4). The reals range from 1.something to 100.something so while this format permits the latter case the maximum number of dps it also limits the smaller numbers to only four dps, when more could clearly fit. I know we have some flexibility in this when dealing with reads, but things seem very rigid when it comes to writes and I can figure out no workaround.
I cannot just use list-directed because that uses up too many spaces per write and I can figure out no way to control that without using formatting, which gets me back to the original issue.
Any suggestions would be much appreciated.
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
An alternative would be to use code like this:
character(len=1000) :: string
character(len=20) :: number
string = ' '
do i = 1,size(array)
write( number, '(f10.4)' ) array(i)
string = trim(string) // ' ' // triml(number)
enddo
(Not very efficient, I admit, because of the string length of the variable string, but that was easier than mucking about with a substring)
Regards,
Arjen
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Make that:
string = trim(string) // ' ' // adjustl(number)
Regards,
Arjen
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
What I would like to see is a "significant figures" format where you specify the field width and the number is written such that is uses the best format to display the most significant figures while taking into account the sign, decimal point and any exponent format requirements. Is this what G0.d will do?
Currently I have a routine to do this but it slows down the writing process as it uses the log function and lots of string manipulation just to write outthe number how I need it. We need to retain as much accuracy as possible when creating data files for third party programs that require fixed size (8 character) fields for input and numbers can range between -xxxxx.x to xxxxx.x floating point. I also remove the E from exponent as well as any superfluous zeros.
eg
1.23456E+09 would become 1.2346+9 (This is accepted by the program reading the data files)
-1.100000 would become -1.1
and so on.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If you are concerned about getting the most significant figures in 8 characters, then 0.1234-3 has 4 significant figures, 1.2345-4 has 5 significant figures, but 123456-9 has 6 significant figures, as does 123456+9.So You do(may) not need the decimal point if you use the exponent form.
Consider 99999999. This can be written 999999+2 and loses2 significant figures producing 99999900 when converted.
1234.567 uses all 8 characters,and has7 sig figs, but 123456-2 loses a sig. fig.
It is asking a lot of a simple format control to produce the following:
number sig figs
99999+10 5
999999+9 6
999999+8 6
999999+7 6
999999+6 6
999999+5 6
999999+4 6
999999+3 6
99999999 8
999999.9 7
99999.99 7
9999.999 7
999.9999 7
99.99999 7
9.999999 7
.9999999 7} same number
999999-6 6}
999999-7 6
999999-8 6
999999-9 6
99999-10 5
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Unfortunately the G format doesn't work for me because it seems to add exponent formatting in some cases, which blows the length of my output beyond the specified 8 characters (or forces me to reduce the number of dps) and as Anthony just pointed out, steals my sigfigs.
I am writing a LOT of output so I'm not sure how efficient your string manipulations will be Arjen, but I have used that solution for some smaller edits and it worked great - thanks!
I'm still rather new to Fortran in the grand scheme of things, but I have to admit to being surprised that such a computationally explicit and potent language does not allow you to more simply control the number of significant figures when writing to output.
I hope that G0.d is, indeed, exactly what I am looking for. And that I still need it by then ;-)
Thanks again everyone
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
How would this example deal with one more significant figure? Would it utilise the digit before the decimal point? One of the issues I've raised in the past is that the G format does not allow the additional control available in the E format whereby you can specify EN (or ES to form the scientific notation).
i.e. GN could be used to format all numbers requiringexponent notation to use the first digit. (Could this be put into the G0.d (if it is not already covered)?
Another enhancement would be to be able to omit the E and any unecessary exponent zeroesaltogether from the output string. This would free up two additional spaces for numbers in the range -9.999e09 to 9.999e09.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The G0.d behavior is fixed by the Fortran 2008 standard (draft). It's not going to change. However, if one used 1PG0.5, you get "1.23450E-07"
You can say something like 1PG11.5 today and get " 1.23450E-7". Note that the 1P doesn't change the behavior if the value is in the "F format" range.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
demonstrate a method of restricting the format to a given character length and converting a floating point number to fill it whilst maximising the sig.figs.
The code takes the form of a subroutine that
lets you specify a floating point number and the number of characters to
be used to format the number and converts a floating point number to fit within that length,
returning a string of the required length, while maximising the number of sig.figs.
If digits are lost because of truncation to fit within the format, some rounding up is done first.
In the test program provided, the converted string is read back using a Fn.0 format in order
to regenerate the number so that it can be compared with the original. :-)

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page