Search code examples
fortranprecisiongfortran

Is it possible to force Fortran compiler to use single precision?


I have a large Fortran code in which most of the real variable is declared to have double precision by using

double precision
real*8

And I wonder if it is possible to force the compiler to use single-precision for those variables.


Solution

  • This depends on the compiler that you use. GNU Fortran supports the option -freal-8-real-4, which transforms all 8-byte reals to 4-byte ones. For instance, the following program

    program p
    
        use iso_fortran_env, only: real64
    
        real(kind=real64) :: x
        double precision :: y
        real*8 :: z
    
        print *, sizeof(x), sizeof(y), sizeof(z)
    
    end program p
    

    prints

                        8                    8                    8
    

    but when compiled with -freal-8-real-4, the outputs is

                        4                    4                    4
    

    Other compilers may have different options (or none of such kind).