Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to identify and distinguish types using Fortran's Select Type?

Tags:

select

fortran

Can Select Type distinguish between Integer(Int8), Integer(Int16), Integer(Int32) and Integer(Int64)?

Also, can Select Type identify an integer type irrespective of the number of bits it uses?

like image 767
Zeus Avatar asked Sep 02 '25 16:09

Zeus


2 Answers

Yes, you can write something like the following. Here I'm using real kind constants from the intrinsic module iso_fortran_env.

SELECT TYPE(areal)
TYPE is (REAL(real32))
   WRITE(*,*) '... real32'
TYPE is (REAL(real64))
   WRITE(*,*) '... real64'
CLASS default
   WRITE(*,*) '... default'
END SELECT

Note, though, that you can't write

SELECT TYPE(areal)
TYPE is (REAL(real32))
   WRITE(*,*) '... real32'
TYPE is (REAL(real64))
   WRITE(*,*) '... real64'
TYPE is (REAL)
    WRITE(*,*) '... real'
CLASS default
   WRITE(*,*) '... default'
END SELECT

In this case a real of default kind (probably real32 for most current compilers) will match two of the type guard statements, and that's an error the compiler should pick up.

like image 163
High Performance Mark Avatar answered Sep 05 '25 16:09

High Performance Mark


Different integer kinds constitute different intrinsic types. So yes, select type distinguishes integers of different kinds.

I am not aware of any possibility to disregard the kind (i.e. not just the number of bytes, theoretically). You must use a distinct type is section for each kind.

like image 32
Vladimir F Героям слава Avatar answered Sep 05 '25 16:09

Vladimir F Героям слава