fortran - Floating point numbers equality -


i using gfortran in mingw under windows 7 (32bit) compile fortran 77 code. here minimal code contained in testequal.f file:

  program testequal   real*8 a1, a2    a1 = 0.3d0   a2 = 0.7d0    write(*,*) 1.d0   write(*,*) a1+a2   write(*,*) a1+a2.eq.1.0   write(*,*) a1+a2.eq.1.d0   end 

compile with

gfortran testequal.f -std=legacy 

the output is:

1.0000000000000000 1.0000000000000000 f f 

but expect 2 booleans both t. problem here?

with rare exceptions, don't compare floating point numbers exact equality. rules of finite-precision floating point arithmetic not same rules of real number arithmetic. compare numbers tolerance, e.g.,

sum = a1 + a2 if ( abs (sum - 1.0) < 1.0d-5 ) ... 

Comments

Popular posts from this blog

Load Balancing in Bluemix using custom domain and DNS SRV records -

oracle - pls-00402 alias required in select list of cursor to avoid duplicate column names -

python - Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>] error -