Weird fortran numeric behavior when comparing to a constant

92 Views Asked by At

So I have this code in Fortran:

REAL*8 DELTA,XI,SO,S
SO=0.273333328465621
S=0.323333333556851
XI=0.01
DELTA =SO-S                ! DELTA = -0.0500000050912297
IF(DELTA.GE.0.0)XI=XI/10

This code with those values always end up evaluating the IF as true and executes the XI division (i.e. XI=0.001 after. I think this is a weird behavior, but my job is to replicate that behavior in C#.

Compiled with intel fortran, no optimizations and and full debug information as part of a 32 bit DLL

Any ideas why this happens?

1

There are 1 best solutions below

2
M. S. B. On

The following doesn't execute the IF statement. Both with gfortran and ifort.

program test_delta

double precision DELTA
DELTA = -0.0500001
IF (DELTA .GE. 0.0) then
    write (*, *) "IF-statement executed"
ENDIF

end program test_delta

One change is that I added the expected "then" to the IF statement. Otherwise both compilers issued error messages.