1

I want to write a piece of code that can find all of the available integer kinds of my machine and print the range for all of them. Finding the kinds was not the hard part, using selected_int_kind I was able to iterate over all of the available kind number till I got a -1 value that indicated that the integer was not longer representable by the compiler.

This process resulted in an array of the available kind number for example (/ 1 2 4 8 16/) (this was the result for my gfortran compiler)

The next step is to use this array to define 5 different kinds of integers and ask for what the maximum representable integer is. My idea was to use an allocatable integer and allocate it every time with an other kind parameter, something like

program main
integer,allocatable :: i
integer::j
integer,dimension(:)::nb_kind
call give_me_the_kinds(nb_kind)
for j=1,size(nb_kind)
    allocate(i,kind=nb_kind(j))
    print *,huge(i)
    deallocate(i)
end program

But this did not work out for me. Has anybody has some experience with this? I think it could be done but I'm not sure how to do it.

francescalus
  • 30,576
  • 16
  • 61
  • 96
DoubleDouble
  • 49
  • 1
  • 4
  • possible duplicate of [Fortran: the largest and the smallest integer](http://stackoverflow.com/questions/9569756/fortran-the-largest-and-the-smallest-integer) – francescalus Mar 04 '15 at 14:49
  • 1
    And also potentially useful: http://stackoverflow.com/q/25534202/. – francescalus Mar 04 '15 at 14:53
  • My question is more about the possibility of defining a dynamical kind? Can you help me out with that? – DoubleDouble Mar 04 '15 at 14:53
  • 1
    You can't have a kind-type parameter being dynamic. Perhaps the hacky answer in my second link could be what you want (and therefore that is a better duplicate)?. – francescalus Mar 04 '15 at 14:55
  • 1
    As another comment, not worthy of an answer: also consider the array `integer_kinds` from the intrinsic module `iso_fortran_env`. – francescalus Mar 04 '15 at 15:01
  • Using `iso_fortran_env` you will get the correct kinds but you still do not know how many there will be? How do you handle that? – DoubleDouble Mar 04 '15 at 15:11
  • `size(integer_kinds)`. – francescalus Mar 04 '15 at 15:21
  • but you cant use a loop in the declaration of your variables, so you cant use the size function? – DoubleDouble Mar 04 '15 at 15:27
  • I'm losing track I think. Rather than your declaration of `nb_kind` and `call give_me_the_kinds` use `integer_kinds`. I didn't mean to suggest that that in itself solves your problem. – francescalus Mar 04 '15 at 15:30

1 Answers1

1

Dynamically-typed variables (going from a kind number to the variable directly) are not possible in Fortran. Kind numbers have to be compile-time constant. What you can do is this:

program main
  use iso_fortran_env, only : integer_kinds
  implicit none
  integer :: i
  open(20,file="mykinds.f90",status="unknown",action="write")
  write (20,'(A/A)') 'program main','  implicit none'
  do i=1, size(integer_kinds)
     write (20,'(A,I0,A,I0,A)') '  integer(kind=',integer_kinds(i),') :: i',i
  end do

  do i=1, size(integer_kinds)
     write (20,'(A,I0,A,I0,A)') '  write (*,"(A,I0)") "integer(kind=',&
          & integer_kinds(i),') huge = ", huge(i',i,')'
  end do

  write (20,'(A)') 'end'
  close(20)
  call execute_command_line ("gfortran mykinds.f90 && ./a.out")
end program main