4

This is probably a very basic question but I can't seem to find the answer on google or through searching though this forum (most likely due to poor phraseology of the problem).

I am looking for an intrinsic Fortran function to give me the number of positions used by an integer. What I mean by this is, if an integer is 12543 then the number of positions used is 5 (it will always be positive). The reason I want this is so that when using a WRITE statement with formatted output I can specify the "w" in "Iw" and use the minimum spacing possible.

More specifically:

WRITE(*,"(/ A,Iw,1X,A,Iw)") "All particles within radius ",radius, "of particle index number", p_o_i

Where radius and p_o_i are specified by the user earlier on.

I can write something to achieve this easily enough but I thought that there must be an intrinsic function which can achieve this.

I have been going through the functions here but without much luck. Thanks for the help.

1QuickQuestion
  • 417
  • 6
  • 16
  • Are you searching for specific function in Fortran or do you want to implement it by yourself? (Very easy implementation). – matan7890 Aug 18 '14 at 15:00
  • 4
    Either use the `log10` function, or write to a string variable with fixed large width and then print the strings with the help of `adjustl` and `trim`. – steabert Aug 18 '14 at 15:10
  • 7
    One can use the `I0` and `G0` edit descriptors. Often these result in the smallest field. – francescalus Aug 18 '14 at 15:16
  • 1
    Thanks for the replies. What I was previously going to do was copy the integer to a temp and then continuously divide the temp by 10 until it was less than 1 whilst increasing a counter. I thought their might be a specific intrinsic function out there though! – 1QuickQuestion Aug 18 '14 at 15:17
  • It's like using `log10` – matan7890 Aug 18 '14 at 15:27
  • 4
    Do what @francescalus says, Fortran provides the `i0` edit descriptor for integer fields which will, at run time, have the exact width for the integer to be printed. You certainly don't need to mess around with logarithms to figure out how many characters to allow when printing an integer. – High Performance Mark Aug 18 '14 at 15:36
  • @HighPerformanceMark If it is being calculated on runtime, doesn't it mean that it won't work for user input? – matan7890 Aug 18 '14 at 15:40
  • If you're only concerned with printing an integer use `i0`, the compiler will take care of making the output field wide enough and no wider. Write some code, find out what happens, it's a great way to learn how to program. – High Performance Mark Aug 18 '14 at 15:45

1 Answers1

2

Thanks for the responses! I admit that the question was misleading in that it specifically requested an intrinsic Fortran function to give the number of positions used by an integer when this was not strictly required in the first place. Furthermore, the general consensus seems to be that this does not exist.

As advised by francescalus and High Performance Mark, in order to ensure that the minimum spacing possible is used for an integer, the "I0" edit description should be used. So for the previous example this would be:

WRITE(*,"(/ 1X, A, I0, A, I0)") "All particles within radius ",radius, " of particle index number ", p_o_i

Should you actually need to find the number of "characters" in an integer (for some other reason) the best proposed response seems to be the use of the log10 function, as advised by steabert.

Community
  • 1
  • 1
1QuickQuestion
  • 417
  • 6
  • 16