From: Ron Shepard <ron-shepard@NOSPAM.comcast.net>
Subject: Re: does modern Fortran have membership test similar to this Ada 2012 feature?
Date: Tue, 19 Jun 2012 11:34:31 -0500
Date: 2012-06-19T11:34:31-05:00 [thread overview]
Message-ID: <ron-shepard-5ABD92.11343119062012@news60.forteinc.com> (raw)
In-Reply-To: jrprpv$d6q$1@speranza.aioe.org
In article <jrprpv$d6q$1@speranza.aioe.org>,
"Nasser M. Abbasi" <nma@12000.org> wrote:
> In previous versions of Ada, membership tests allowed one
> to see whether a value was in a range or in a subtype, thus
> we could write either of
>
> if D in 1 .. 30 then
>
> if D in Days_In_Month then
>
> but we could not write something like
>
> if D in 1 | 3 | 5 | 6 ..10 then
>
> This is now rectified and following "in" we can now have one
> or more of a value, a range, or a subtype or any
> combination separated by vertical bars. Moreover, they do
> not have to be static.
This is basic functionality that is available in essentially every
high level programming language. However, the syntax is different.
If D is a scalar, then the above would be done in fortran as
if ( (D>=1) .and. D(<=30) ) then
If Days_In_Month is an array, then you could do
if ( any(D==Days_in_Month) ) then
There are also lower-level constructs that could be used, including
looping over the elements of Days_In_Month(:) and testing each one
individually. Sometimes that is actually the best approach,
depending on what you need to do after the test. In other cases,
the best approach is something like
if ( D==1 .or. D==3 ) then
...
elseif ( D==2 ) then
...
endif
or if the cases really are static you can do things like
select case (D)
case(1,3,5,6:10)
...
case(2)
...
end select case
The string of if-then-else tests requires effort proportional to the
number of tests, so you generally want to order the most common
cases first in the sequence. The select case construct can be done
by setting up a branch table, so the effort does not depend on the
number of tests.
In fortran, the select-case construct requires static tests.
However, in many situations you can map a set of dynamic comparisons
to a fixed set of static cases, in which case you get the advantages
of the branch table algorithm for your dynamic case.
$.02 -Ron Shepard
prev parent reply other threads:[~2012-06-19 16:34 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-06-19 12:37 does modern Fortran have membership test similar to this Ada 2012 feature? Nasser M. Abbasi
2012-06-19 16:34 ` Ron Shepard [this message]
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox