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

Is increment operator available in fortran?

10,225 views
Skip to first unread message

Peng Yu

unread,
Jun 13, 2015, 11:50:56 AM6/13/15
to
C/C++ has +=. Is there something similar to it in fortran?

Regards,
Peng

edmondo.g...@gmail.com

unread,
Jun 13, 2015, 12:12:21 PM6/13/15
to
No, you should do:

i = i + 1

(1 or whatever you want to sum to the variable "i")

Keep in mind that Fortran has array expressions and assignment so given an array (as example):

real :: a(20), b(20)

The expression:

a = a + b

sums each element of "b" to each element of "a" without an explicit loop.
:)

glen herrmannsfeldt

unread,
Jun 13, 2015, 6:00:36 PM6/13/15
to
edmondo.g...@gmail.com wrote:
> Il giorno sabato 13 giugno 2015 17:50:56 UTC+2, Peng Yu ha scritto:
>> C/C++ has +=. Is there something similar to it in fortran?

(snip)
> No, you should do:

> i = i + 1

Now, consider:

X(complicated subscript expression)=X(complicated subscript expression)+1

especially if the expression involves function calls.

-- glen

mec...@gmail.com

unread,
Jun 13, 2015, 6:23:46 PM6/13/15
to
Yes; please read about ASSOCIATE...END ASSOCIATE

Richard Maine

unread,
Jun 13, 2015, 7:25:06 PM6/13/15
to
glen herrmannsfeldt <g...@ugcs.caltech.edu> wrote:

> edmondo.g...@gmail.com wrote:
> > Il giorno sabato 13 giugno 2015 17:50:56 UTC+2, Peng Yu ha scritto:
> >> C/C++ has +=. Is there something similar to it in fortran?
> > No, you should do:
> > i = i + 1
> Now, consider:
>
> X(complicated subscript expression)=X(complicated subscript
> expression)+1
>
> especially if the expression involves function calls.

As mece notes, see ASSOCIATE if you really have need for that.

Or for your particular example there is always
i = complicated subscript expression
x(i) = x(i) + 1

One doesn't actually need a whole new language feature, which comes with
a large number of complications and caveats for a special case as simply
handled as that. If you are looking to avoid rewriting the same
complicated expression multiple times - an admirable goal for many
reasons - might as well handle much more general cases instead of the
particular one of incrementation. See ASSOCIATE.

--
Richard Maine
email: last name at domain . net
dimnain: summer-triangle

glen herrmannsfeldt

unread,
Jun 13, 2015, 7:39:46 PM6/13/15
to
Richard Maine <nos...@see.signature> wrote:
(snip, I wrote)

>> X(complicated subscript expression)=X(complicated subscript
>> expression)+1

>> especially if the expression involves function calls.

> As mece notes, see ASSOCIATE if you really have need for that.

> Or for your particular example there is always
> i = complicated subscript expression
> x(i) = x(i) + 1

This does remind me of those places in Fortran 66 where variables
are allowed, but not expressions. DO statements and the I/O list
in WRITE statements were the cause for many temporary variables.

I don't know ASSOCIATE, will have to look that one up.

> One doesn't actually need a whole new language feature, which comes with
> a large number of complications and caveats for a special case as simply
> handled as that. If you are looking to avoid rewriting the same
> complicated expression multiple times - an admirable goal for many
> reasons - might as well handle much more general cases instead of the
> particular one of incrementation. See ASSOCIATE.

-- glen

robin....@gmail.com

unread,
Jun 13, 2015, 8:44:33 PM6/13/15
to
On Sunday, June 14, 2015 at 8:00:36 AM UTC+10, glen herrmannsfeldt wrote:
Now consider:
k = complicated subscript expression
X(k) = X(k) + 1
Message has been deleted

erik.t...@gmail.com

unread,
Feb 24, 2017, 2:00:36 AM2/24/17
to
Consider

elemental subroutine Inc(target,add)
real,intent(inout) :: target
real,intent(in) :: add
target = target+add
end subroutine Inc

septc...@gmail.com

unread,
Feb 24, 2017, 7:19:42 AM2/24/17
to
2015年6月14日日曜日 0時50分56秒 UTC+9 Peng Yu:
> C/C++ has +=. Is there something similar to it in fortran?
>
> Regards,
> Peng

I use the following macro for this (while always using *.F90):

#define _(x) x = x

so that I can write

_( foo( bla, bla2 ) ) * 2

etc :)

robin....@gmail.com

unread,
Feb 24, 2017, 9:19:39 AM2/24/17
to
Does it really need a subroutine to add 1 to a variable?

How fast do you want it to go?

herrman...@gmail.com

unread,
Feb 24, 2017, 2:56:06 PM2/24/17
to
On Friday, February 24, 2017 at 6:19:39 AM UTC-8, robin....@gmail.com wrote:
> On Friday, February 24, 2017 at 6:00:36 PM UTC+11, erik.t...@gmail.com wrote:

(snip)

> > elemental subroutine Inc(target,add)
> > real,intent(inout) :: target
> > real,intent(in) :: add
> > target = target+add
> > end subroutine Inc

> Does it really need a subroutine to add 1 to a variable?

> How fast do you want it to go?

There are systems now that can inline some subroutines or funcions,
such that the run time isn't affected. That leaves the question of
readability.

Note that there are also the *=, /=, <<=, >>=, and bitwise
logical operators in C and Java.

There is no Fortran equivalent to the C or Java for loop:

for(i=1; i<=n; i*=2)

(which doesn't require the *= operator)

michael siehl

unread,
Feb 24, 2017, 6:11:58 PM2/24/17
to
| Does it really need a subroutine to add 1 to a variable?

For atomic variables and coarray programming, the answer is yes. Fortran 2015 does add the atomic_add atomic subroutine to the language. OpenCoarrays/GFortran does already support this :

call atomic_add (atomic_variable, 1) ! add 1 atomically to atomic_variable

I already use this and find it really helpful.

cheers
0 new messages