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

How do I generate compiler warnings for comparison of real variables?

pcshah
Beginner
675 Views
I have large inherited Fortran code that has comparion of real variables sprinked everywhere. When I switched to Intel Fortran compiler, the results are sometimes different. I need to spot all comparison of real varibales (single and double precision) in my code. Is there a compiler option that will help me with this?
Thanks,
P.C. Shah
0 Kudos
5 Replies
Steven_L_Intel1
Employee
675 Views
No, there isn't. Such comparisons are standard-conforming.
0 Kudos
emcnyc
Beginner
675 Views

You have to get something like one of the lint-like programs for Fortran. There are several out there: flint, forchek, ftnchek, etc.

Floating point comparisons for equality are perfectly legitimate (albeit not necessarily a good idea). Usually, comparison to 0 will be safe.

Message Edited by emcnyc on 06-12-200609:22 AM

0 Kudos
TimP
Honored Contributor III
675 Views
I have no idea whether this is relevant to the original posting, but comparison with 0 may be done in hopes of avoiding problems with divide by 0. In that case, the code may work as intended, if it is compiled to as to eliminate extra precision, and abrupt underflow is set (-Qftz). Otherwise, non-zero numbers are produced which are close enough to zero to exhibit the problems which the code author thought could be caught.
A common failure with aggressive optimization in ifort is like the following:
a=b/((c-d)+sqrt(tiny(a)))
This will compile correctly, if -fp:model precise is set. The intent, again, is to avoid a problem when c==d or nearly so. A simple comparison would take longer, and not accomplish the goal. It still looks like bad practice, particularly to those who like aggressive compiler optimizations.
0 Kudos
g_f_thomas
Beginner
675 Views
Alittle late but it's a timeless issue. As Steve remarks, comparing fpexp1 with fpexp2 is perfectly legit as far as Fortran and otherlanguages go; but, and without attribution to Steve, it might not bewise. If you are very careful it can be done without penalty, but for the rest of us, an appeal to Knuth is the ticket:
integer(4) function fpCompare (x, y, nulps)

implicit none
real(8), intent(in) :: x,y
integer(8), intent(in) :: nulps
!Local
real(8) difffp, deltafp
difffp = x-y
deltafp = nulps*2*spacing(max(abs(x),abs(y)))
if ( difffp > deltafp ) then
fpCompare = 1!x > y
elseif ( difffp < -deltafp ) then
fpCompare = -1!x < y
else
fpCompare = 0!x == y
endif
return
end function fpCompare
Useage is obvious, eg, to calculate (e^x-1)/x for small x
real(8) function expm1(x)
!
!Calculates (exp(x) -1)/x for /x/ << 1 using an approach due to Kahan
!
implicit none
real(8), intent(in) :: x

real(8), parameter :: one = 1.d0
real(8) :: y

y = exp(x)
if (fpCompare(y, one, 1000) == 0) then
expm1 = one
else
expm1 = (y - one)/log(y)
endif
return
end function expm1
HTH,
Gerry T.
0 Kudos
Steven_L_Intel1
Employee
675 Views
There is a whole class of things which are legal but unwise. I'd like to see the compiler optionally warn about some of these, and I'll agree that comparisons of reals and conversions between different kinds of reals, are candidates for this. I'd especially like to see a warning for conversion between logical and integer - something non-standard but supported by the compiler and which gets lots of people in trouble.
0 Kudos
Reply