Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Not getting exact output

Gaurav_K_2
Beginner
365 Views

 

Hi,

I am Gaurav & just started using fortran. I tried to run following code for which output should be 10.5 but instead I am getting 16.5. Why is it so? please tell

PROGRAM try

    REAL (KIND=8):: x,y,z
    x=0.5;y=0.25434678E9;z=0.25434677E9
    w=x+y-z
    PRINT*,w
    END PROGRAM try

Regards,

Gaurav K.

0 Kudos
1 Solution
Arjen_Markus
Honored Contributor I
365 Views

This has nothing to do with Fortran, but rather with the finite precision of floating-point numbers on computers. You are simply exceeding the limits of the internal representation Compare it to working with 0.33333 and 1/3 in mathematics with paper and pencil. The literal numbers are in _single precision_. That is a rather crucial part of Fortran: the right-hand side is not influenced by the left-hand side. You probably want to use double precision numbers instead:

PROGRAM try

    REAL (KIND=8):: x,y,z
    x=0.5;y=0.25434678E9_8;z=0.25434677E9_8
    w=x+y-z
    PRINT*,w
    END PROGRAM try

Note the "_8" which is an example of specifying the kind to a literal number. You can also use "d9" instead of "e9" to get double precision.

Regards,

Arjen

 

View solution in original post

0 Kudos
4 Replies
Arjen_Markus
Honored Contributor I
366 Views

This has nothing to do with Fortran, but rather with the finite precision of floating-point numbers on computers. You are simply exceeding the limits of the internal representation Compare it to working with 0.33333 and 1/3 in mathematics with paper and pencil. The literal numbers are in _single precision_. That is a rather crucial part of Fortran: the right-hand side is not influenced by the left-hand side. You probably want to use double precision numbers instead:

PROGRAM try

    REAL (KIND=8):: x,y,z
    x=0.5;y=0.25434678E9_8;z=0.25434677E9_8
    w=x+y-z
    PRINT*,w
    END PROGRAM try

Note the "_8" which is an example of specifying the kind to a literal number. You can also use "d9" instead of "e9" to get double precision.

Regards,

Arjen

 

0 Kudos
TimP
Honored Contributor III
365 Views

ifort and gfortran also work with the more modern real64:

PROGRAM try
    use iso_fortran_env
    REAL (KIND=real64):: x,y,z
    x=0.5;y=0.25434678E9_real64;z=0.25434677E9_real64
    w=x+y-z
    PRINT*,w
  END PROGRAM try

If you intend to play with the precision limits, you need parentheses:

w=x+(y-z)

and, in principle, you need to tell ifort to adhere to Fortran standard:

ifort -standard-semantics gk.f90

0 Kudos
Gaurav_K_2
Beginner
365 Views

Thank you

0 Kudos
andrew_4619
Honored Contributor II
365 Views

and as W is not specified it would be a default real which unless you have set some specific compiler options will be kind=4. It is best to always have IMPLICIT NONE command in the code after the PROGRAM and any USE statements. This forces you to declare everything which avoids some unexpected things and will find typing/spelling errors in variable names.

 

0 Kudos
Reply