- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Link Copied
5 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
No, there isn't. Such comparisons are standard-conforming.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
implicit none
real(8), intent(in) :: x,y
integer(8), intent(in) :: nulps
!Local
real(8) difffp, deltafp
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
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
!
!
!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)
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
expm1 = one
else
expm1 = (y - one)/log(y)
endif
return
end function expm1
HTH,
Gerry T.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page