0

I have a large expression (10k+ lines) which is implemented in fortran as a single instruction (just a huge formula with lots of line continuation symbols). The expression depends on x. For some values of x I would like to evaluate it in double precision, for other values of x I need to evaluate (also the whole expression) in quad precision, without duplicating the code of 10k+ lines.

I have tried to use an unlimited polymorphic variable for this declared as

class(*), allocatable :: x

and then after having allocated x with the correct precision I do

    select type( var )
      type is ( real(kind=16) )
        ! compute with 
      type is ( real(kind=8) )
        write( *, * ) "real with double precision:  ", var
    end select

But that would still leave me duplicating the in the two cases of the select type. The reason is that I cannot have a case for generic floating point number, nor can I have mulitple checks in the SELECT TYPE statement. Any suggestions?

Luca
  • 558
  • 4
  • 18
  • 3
    Include file is probably the way to go - two subroutines each with the appropriate precision and use an include file for the repeated source. But are you sure that just changing x will change the precision of the whole expression? e.g. will you need to also specify the appropriate kind for any constants in the expression? – Ian Bush May 17 '21 at 13:45
  • 5
    Also note Real(8) is not good practice - it is not guaranteed either to be double precison or even to be supported. Similarly real(16). See https://stackoverflow.com/questions/838310/fortran-90-kind-parameter – Ian Bush May 17 '21 at 13:46

0 Answers0