comp.lang.ada
 help / color / mirror / Atom feed
* does modern Fortran have membership test similar to this Ada 2012 feature?
@ 2012-06-19 12:37 Nasser M. Abbasi
  2012-06-19 16:34 ` Ron Shepard
  0 siblings, 1 reply; 2+ messages in thread
From: Nasser M. Abbasi @ 2012-06-19 12:37 UTC (permalink / raw)



Fortran experts:

I do not know Fortran well. I did google search, but too many
false hits due to the nature of the search.

I thought I ask here, since someone who is an expert in the language
would know right away the answer to this question without any effort.

Does Fortran have set membership test? Similar to this
description below, I copied from 2012 Ada introduction by
John Barnes.

-- start quote ------

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.

----- end quote ----------------

Any thing like the above build-in Fortran 90, 2003, etc...

ofocurse one can write a function to do the above, but
wanted to check if it is build in. Syntax is not important,
just the concept.

thanks,
--Nasser
ps. I added comp.lang.ada, just FYI, and may be someone there
might know also.



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: does modern Fortran have membership test similar to this Ada 2012 feature?
  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
  0 siblings, 0 replies; 2+ messages in thread
From: Ron Shepard @ 2012-06-19 16:34 UTC (permalink / raw)


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



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2012-06-19 16:34 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox