Topic: Use Double Precision for All Reals checkbox

Jeff,
How it works exactly? Is it simply a compiler switch? Does it apply also for function arguments declared as real?
Thank you,
Carlos

Re: Use Double Precision for All Reals checkbox

Carlos,

Yes, that checkbox just implements a compiler switch, -fdefault-real-8 to be exact.  It will promote all REAL declarations that are not explicitly defined with a kind to be REAL(KIND=8).  You can read more about it at http://simplyfortran.com/docs/compiler/ … tions.html.

Jeff Armstrong
Approximatrix, LLC

Re: Use Double Precision for All Reals checkbox

Jeff,
I asked this question inspired by the discussion on fortram-lang forum. In Modern Fortran it is recommended to write
x=1.234d0 or x=1.234_dp,  if x is declared as double. This is indeed annoying and hurts readability. If I understand correctly,
with -fdefault-real-8 switch we can use x=1.234, however, it is still dangerous since then, 1.234d0 and other doubles are promoted to quad precision.

Re: Use Double Precision for All Reals checkbox

While you're right, the readability suffers somewhat, your simple example even shows issues.  If my program is:

program helloworld
implicit none

    real(kind=8)::x
    
    x = 1.234
    Print *, x
    
    x = 1.234d0
    Print *, x
    
end program helloworld

I get the following results:

   1.2339999675750732     
   1.2340000000000000  

So there is an advantage to using the 1.234d0 syntax if you're counting on accuracy in double precision. 

You're right, though, that using the -fdefault-real-8 does fix the issue illustrated above.  However, counting on a specific compiler's flag is not the best way to write portable code.  I would normally use that flag to handle older code that I (or someone else) had neglected to assign kind specifications where I wanted to try running in double precision.  There was also a time in the not-so-distant past that running in single precision was considerably faster, so you could work in single precision when developing and switch to double precision when you were confident that the long-running simulation was ready to go.

Jeff Armstrong
Approximatrix, LLC

Re: Use Double Precision for All Reals checkbox

Regarding Jeff's example, compare precision of_sp, _dp, and _qp.
Results are 'odd', go figure?

    !    Compare precision of sp, dp, and d0:
    !
    !    x=1.234_sp, 1.234_dp, 1.234d0
    !    1.23399997
    !    1.23399997
    !    1.23399997
    !
    !    y=1.234_sp, 1.234_dp, 1.234d0
    !    1.2339999675750732
    !    1.2340000000000000
    !    1.2340000000000000
    !
    !    z=1.234_sp, 1.234_dp, 1.234d0
    !    1.23399996757507324218750000000000000
    !    1.23399999999999998578914528479799628
    !    1.23399999999999998578914528479799628
    !
    !    x=1.2345_sp, 1.2345_dp, 1.2345d0
    !    1.23450005
    !    1.23450005
    !    1.23450005
    !
    !    y=1.2345_sp, 1.2345_dp, 1.2345d0
    !    1.2345000505447388
    !    1.2344999999999999
    !    1.2344999999999999
    !
    !    z=1.2345_sp, 1.2345_dp, 1.2345d0
    !    1.23450005054473876953125000000000000
    !    1.23449999999999993072208326339023188
    !    1.23449999999999993072208326339023188
    !
    !    x=1.23456_sp, 1.23456_dp, 1.23456d0
    !    1.23456001
    !    1.23456001
    !    1.23456001
    !
    !    y=1.23456_sp, 1.23456_dp, 1.23456d0
    !    1.2345600128173828
    !    1.2345600000000001
    !    1.2345600000000001
    !
    !    z=1.23456_sp, 1.23456_dp, 1.23456d0
    !    1.23456001281738281250000000000000000
    !    1.23456000000000010174971976084634662
    !    1.23456000000000010174971976084634662

program helloworld
    implicit none

    integer, parameter :: p6  = selected_real_kind(6)   !---  4 bytes
    integer, parameter :: p15 = selected_real_kind(15)  !---  8 bytes
    integer, parameter :: p20 = selected_real_kind(20)  !--- 16 bytes

    !--- Symbolic names for kind types of reals:
    INTEGER, PARAMETER :: SP = KIND(1.0_p6)      !---  4 bytes
    INTEGER, PARAMETER :: DP = KIND(1.0_p15)     !---  8 bytes
    INTEGER, PARAMETER :: QP = KIND(1.0_p20)     !--- 16 bytes

    real(kind=SP):: x
    real(kind=DP):: y
    real(kind=QP):: z

    !---
    !--- Compare x_sp, x_dp, and xd0
    !---
    Print *, 'Compare precision of sp, dp, and d0:'
    !---
    !--- x=1.234
    !---
    Print *
    Print *, 'x=1.234_sp, 1.234_dp, 1.234d0'
    x = 1.234_sp
    Print *, x
    x = 1.234_dp
    Print *, x
    x = 1.234d0
    Print *, x
    Print *
    Print *, 'y=1.234_sp, 1.234_dp, 1.234d0'
    y = 1.234_sp
    Print *, y
    y = 1.234_dp
    Print *, y
    y = 1.234d0
    Print *, y
    Print *
    Print *, 'z=1.234_sp, 1.234_dp, 1.234d0'
    z = 1.234_sp
    Print *, z
    z = 1.234_dp
    Print *, z
    z = 1.234d0
    Print *, z
    !---
    !--- x=1.2345
    !---
    Print *
    Print *, 'x=1.2345_sp, 1.2345_dp, 1.2345d0'
    x = 1.2345_sp
    Print *, x
    x = 1.2345_dp
    Print *, x
    x = 1.2345d0
    Print *, x
    Print *
    Print *, 'y=1.2345_sp, 1.2345_dp, 1.2345d0'
    y = 1.2345_sp
    Print *, y
    y = 1.2345_dp
    Print *, y
    y = 1.2345d0
    Print *, y
    Print *
    Print *, 'z=1.2345_sp, 1.2345_dp, 1.2345d0'
    z = 1.2345_sp
    Print *, z
    z = 1.2345_dp
    Print *, z
    z = 1.2345d0
    Print *, z

    !---
    !--- x=1.23456
    !---
    Print *
    Print *, 'x=1.23456_sp, 1.23456_dp, 1.23456d0'
    x = 1.23456_sp
    Print *, x
    x = 1.23456_dp
    Print *, x
    x = 1.23456d0
    Print *, x
    Print *
    Print *, 'y=1.23456_sp, 1.23456_dp, 1.23456d0'
    y = 1.23456_sp
    Print *, y
    y = 1.23456_dp
    Print *, y
    y = 1.23456d0
    Print *, y
    Print *
    Print *, 'z=1.23456_sp, 1.23456_dp, 1.23456d0'
    z = 1.23456_sp
    Print *, z
    z = 1.23456_dp
    Print *, z
    z = 1.23456d0
    Print *, z
end program helloworld

Re: Use Double Precision for All Reals checkbox

Addendum to my previous post by replacing x.d0 with x._qp

        Compare precision of sp, dp, and qp:

        x=1.234_sp, 1.234_dp, 1.234_qp
        1.23399997
        1.23399997
        1.23399997

        y=1.234_sp, 1.234_dp, 1.234_qp
        1.2339999675750732
        1.2340000000000000
        1.2340000000000000

        z=1.234_sp, 1.234_dp, 1.234_qp
        1.23399996757507324218750000000000000
        1.23399999999999998578914528479799628
        1.23399999999999999999999999999999991

        x=1.2345_sp, 1.2345_dp, 1.2345_qp
        1.23450005
        1.23450005
        1.23450005

        y=1.2345_sp, 1.2345_dp, 1.2345_qp
        1.2345000505447388
        1.2344999999999999
        1.2344999999999999

        z=1.2345_sp, 1.2345_dp, 1.2345_qp
        1.23450005054473876953125000000000000
        1.23449999999999993072208326339023188
        1.23450000000000000000000000000000009

        x=1.23456_sp, 1.23456_dp, 1.23456_qp
        1.23456001
        1.23456001
        1.23456001

        y=1.23456_sp, 1.23456_dp, 1.23456_qp
        1.2345600128173828
        1.2345600000000001
        1.2345600000000001

        z=1.23456_sp, 1.23456_dp, 1.23456_qp
        1.23456001281738281250000000000000000
        1.23456000000000010174971976084634662
        1.23456000000000000000000000000000005