PDA

View Full Version : Checking for precison loss


Mike Prager
01-28-2005, 08:21 PM
This is to request that the Lahey compiler warn about cases where precision may be lost by assigning a lower-precision literal constant to a higher-precision variable. Warning about more complex cases of this would be helpful, as well. FTNCHK does this for Fortran 77, but of course that doesn't help users of Fortran 95.

An example:

program foo
double precision F
F = 1.0
end program foo

This post is a result of the issue's being raised on comp.lang.fortran and my finding that LF95 7.1 does not warn about such usage.

RNMcLean
05-02-2006, 03:21 AM
Since 1.0 is represented exactly in floating-point, there should be no precision loss. However, decimal fractions are usually recurring in binary, so
10.15 is 1010.00010011001100110011, in 24 bits. Clearly, further bits are not all zero (because 0.1 is 0.00011001100110011001100110011...,) and writing such a value with format F6.1 might yield 10.1 or 10.2 depending on the precision used to store it. As a real*8 variable, 10.15 is printed as 10.1499996185303 in free-format.