Sid,
If the number you are using is not exact in binary representation, then you will have problems, as round-off will change the result, as in the following example:
real*4 r1
real*8 d1, d2, numtol
integer*4 i
do i = 1,2
!
if (i==2) then
r1 = 1.3175
d1 = 1.3175d0
d2 = 1.3175
else
r1 = 1.5
d1 = 1.5d0
d2 = 1.5
end if
write (*,*) 'Using d2=',d2
!
if ( d1 == d2 ) write (*,*) 'd1 and d2 are the same'
if ( d1 /= d2 ) write (*,*) 'd1 and d2 are different'
write (*,*) d1-d2
!
d2 = r1
if ( d1 == d2 ) write (*,*) 'd1 and d2 are the same'
if ( d1 /= d2 ) write (*,*) 'd1 and d2 are different'
write (*,*) d1-d2
!
d2 = d1
if ( d1 == d2 ) write (*,*) 'd1 and d2 are the same'
if ( d1 /= d2 ) write (*,*) 'd1 and d2 are different'
write (*,*) d1-d2
end do
!
write (*,*) 'Estimation of accuracy of d1'
numtol = abs (d1) * epsilon (d1) ! works if d1 /= 0
write (*,*) 'number of digits in r1 =', precision (r1)
write (*,*) 'epsilon for real*4 =', epsilon (r1)
write (*,*) 'tiny for real*4 =', tiny (r1)
write (*,*) 'number of digits in d1 =', precision (d1)
write (*,*) 'epsilon for real*8 =', epsilon (d1)
write (*,*) 'tiny for real*8 =', tiny (d1)
!
write (*,*) 'accuracy of d1 =', numtol
write (*,*) ' useful NUMTOL test =', 2*numtol
end
However if you try r1 = 1.5 ; d1 = 1.5d0 ; d2 = 1.5 you will get a different result.
The correct way is how Eddie suggests, although it is more to code and you need to know what precision to apply to (real*8) NUMTOL.
Note: NUMTOL varies with the value of d1 (or d2 or the expected range of values for d1 or d2)
if d1 is zero, then you need a different approach for NUMTOL. Tiny(d1) is the smallest number near (not) zero, so you can see for small values of d1 you need an even smaller value for NUMTOL.
Effectively you need to say; How close to d1 is the same value ?
There is another case, where you initially give d2 the value of d1, then at the end of your routine you ask if d2 has changed. In this case if ( d1/= d2) will give the answer you want (and the compiler warnings)
John