Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

best way to convert a Fortran implied do loop to C++

41 views
Skip to first unread message

Lynn McGuire

unread,
Jan 7, 2023, 12:00:52 AM1/7/23
to
So what is the best way to convert a Fortran implied do loop to C++ ?

WRITE (2,2) (N (I), I = 1, 80)
2 FORMAT(' ',80A1)

Thanks,
Lynn

Ben Bacarisse

unread,
Jan 7, 2023, 7:55:36 AM1/7/23
to
Lynn McGuire <lynnmc...@gmail.com> writes:

> So what is the best way to convert a Fortran implied do loop to C++ ?
>
> WRITE (2,2) (N (I), I = 1, 80)
> 2 FORMAT(' ',80A1)

Presumably the bounds are not always literals, and you probably also
want to general solution the works for multiple implied loops, some of
which might be nested? So not much!

My first question would be how you translate WRITE/FORMAT combinations
with no loops as that's going to form the basis of a solution.

(I presume you know Fortran well enough to know that this is not the
same as putting a loop round the write.)

--
Ben.

Richard Damon

unread,
Jan 7, 2023, 11:07:48 AM1/7/23
to
Althogh in C++ you COULD put the loop around the write equivalent since
C++ doesn't automatically take the first character as carriage control
and add the new line to the end (if you use the right functions).

Ben Bacarisse

unread,
Jan 7, 2023, 4:48:37 PM1/7/23
to
Richard Damon <Ric...@Damon-Family.org> writes:

> On 1/7/23 7:55 AM, Ben Bacarisse wrote:
>> Lynn McGuire <lynnmc...@gmail.com> writes:
>>
>>> So what is the best way to convert a Fortran implied do loop to C++ ?
>>>
>>> WRITE (2,2) (N (I), I = 1, 80)
>>> 2 FORMAT(' ',80A1)
>> Presumably the bounds are not always literals, and you probably also
>> want to general solution the works for multiple implied loops, some of
>> which might be nested? So not much!
>> My first question would be how you translate WRITE/FORMAT combinations
>> with no loops as that's going to form the basis of a solution.
>> (I presume you know Fortran well enough to know that this is not the
>> same as putting a loop round the
>
> Althogh in C++ you COULD put the loop around the write equivalent
> since C++ doesn't automatically take the first character as carriage
> control and add the new line to the end (if you use the right
> functions).

I would expect that with a clean translation the carriage control
handling (if wanted) would just come out in the wash. Even in modern
Fortran (where the "carriage control" malarkey is obsolete) you can't
just turn an implied loop into an explicit one. Presumably the
translating software already has some mechanism to iterate through the
format and that's what has to be driven by the implied loop.

If, as I suppose is possible, the first character is still to be treated
specially, that should just work out correctly as the code iterates
through the format.

--
Ben.

Ben Bacarisse

unread,
Jan 7, 2023, 5:42:02 PM1/7/23
to
To add a bit more detail, I image that formats will be turned into
run-time objects containing a 'compiled' version of the format
mini-language. A statement like

WRITE(*, 2) A, X
2 FORMAT(I2, F5.2)

would turn into

format_2.start();
format_2.write(UNIT_STAR, A);
format_2.write(UNIT_STAR, X);
format_2.end();

where the write method is a templated method that advances an internal
pointer in the format "list" (it may, in fact, be a cyclic graph). And

WRITE(*,2) (N(I), X(I), I = 1, 6)

would turn into

format_2.start();
for (int i = 1; i <= 6; i++) {
format_2.write(STAR, N[i]);
format_2.write(STAR, X[i]);
}
format_2.end();

The member function 'end' should be idempotent, because the write
function will also call it when the last format specification is used.

I'm sure this is turn out to be way more complicated that it looks to me
right now, but this feel like the gist of what's needed.

--
Ben.

Ben Bacarisse

unread,
Jan 7, 2023, 5:46:45 PM1/7/23
to
Ben Bacarisse <ben.u...@bsb.me.uk> writes:

> To add a bit more detail, I image that formats will be turned into
> run-time objects containing a 'compiled' version of the format
> mini-language. A statement like
>
> WRITE(*, 2) A, X
> 2 FORMAT(I2, F5.2)

It might have made more sense to use a format that could be used by an
external loop like

DO 10 I = 1, 6
WRITE(*, 2) A(I), X(I)
10 END DO

but which while still being sensible would give a different result. For
example, I might have used

2 FORMAT(3(I2, F5.2))

The rest remains the same.

Lynn McGuire

unread,
Jan 7, 2023, 10:30:06 PM1/7/23
to
This is close to the way that F2C mimics the implied loop.

I have been using a std::string as an accumulator to maintain type safety.

Thanks,
Lynn

Lynn McGuire

unread,
Jan 11, 2023, 3:16:49 PM1/11/23
to
BTW, the code I ended up using is:

char *n_input;
...
std::string msg = n_input;
outwri (msg);

Thanks,
Lynn

0 new messages