Does "gfortran -O0" truncate floating point numbers to their expected precision?

Tim Prince n8tm@aol.com
Wed Jul 3 12:06:00 GMT 2013


On 7/3/2013 7:22 AM, Guan Xin wrote:
> Hello,
>
> I have some Fortran 77 code like this (it's not me who wrote it) --
>
> c     f1 and f2 are of the same floating point type
> c     func() returns the same value on every call
>     10 f1 = func()
>     20 f2 = func()
> c     f1 and f2 are expected to have the same value
>     30 if (f1 - f2) 100,200,300
>
> When compiled without optimization, the "if" branches to line 200
> as expected.
>
> When compiled with optimization, at line 30 the value of f2 still
> remains in the register, which has extended precision, and f1 has
> already been truncated. Therefore, line 30 will branch to anywhere
> depending on the rounding of f1.
>
> My question is: Does "gfortran -O0" guarantee that both f1 and f2
> are truncated by line 30, so they compare equal?
>
> Thanks in advance!
>
> Guan
This doesn't look to me like F77, although it contains some F66.
If you are using 32-bit gfortran, the i486 code with x87 instructions is 
particularly prone to this kind of problem.  Did you try -march=native, 
or, if using an extremely old CPU, -ffloat-store?

-- 
Tim Prince



More information about the Gcc-help mailing list