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

BUG: ifort 12.0.4 Strange behavior for constans and double precision

Kamil_Kie
Beginner
519 Views
[fortran]program prec double precision :: a,b,x,y a=1./3. x=1. y=3. b=x/y print *,"kind=",kind(a), " val=",a print *,"kind=",kind(b), " val=",b if(a==b) then print *,"OK!" else print *,"Why 'a' and 'b' are different?!" end if end program[/fortran]
We compile this by:
ifort prec.f90
Result:
kind= 8 val= 0.333333343267441
kind= 8 val= 0.333333333333333
Why 'a' and 'b' are different?!
Why such a misleading behavior?
0 Kudos
1 Reply
mecej4
Honored Contributor III
519 Views
Please read a few Fortran/programming-related text books and manuals. There is nothing unusual or buggy here at all.

The right-hand sides of lines 5, 6 and 7 are single-precision expressions. In particular, the expression 1./3. is single precision, which implies that it is correct to about seven decimal digits. Assigning such values to double precision variables cannot restore lost precision.

The values assigned to x and y, however, happen to have values (small integers) that are exactly representable in single or double precision. Therefore, the expression x/y is calculated in double precision and the quotient is the best that double precision is capable of delivering.

0 Kudos
Reply