Re: Print statement within If-Then block changes output!!!????



watsteel wrote:
I have a pair of If-then blocks to store a minimum residual.
While debugging, I noticed that a 'Print' statement placed within the
second block would change the program's output. I've tried 'Print *',
Write(both to * and to unit=2, which was opened.), and I've changed
the location of the 'Print' statement within the block, but all to no
avail. If the 'Print' statement is in the block, the output changes.
There are two sets of output, depending on whether the 'Print'
statement exists in the block or not.

The program is only 195 lines of code. The input file if 15 lines of
code.
I will happily e-mail them to anyone who can spare the time to look
this over--I'm stumped.

I'm using GNU G77, on a Dell laptop, running XP Home.

And my forehead is getting bruised from banging it on my desk, so any
help would be greatly appreciated.

Presuming that Colin's conclusion that this isn't the usual sort of out-of-bounds error causing that problem, and that it is in fact a compiler bug, I think I've got a fair guess which exactly compiler bug it is, because I've run into it in exactly this situation: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=323.

The short version is of the bug this: The Intel floating-point system has 80-bit floating-point registers, but has only 64-bit floating-point storage. Thus, there are cases where, if a value is stored in memory, its value will be slightly different (due to removing the last 16 bits) than if its kept in a register.

How this hit me is that I had two numbers where A was supposed to be greater than or equal to B, but when the comparison was done, one of them had been stored in memory and the other was still in the register where it had been calculated, and as a result A.GE.B was false. Printing out something in the middle of this caused all of the variables to be shunted out of registers into 64-bit memory slots, and thus made the bug go away.

One recommended fix for this in GCC/g77/gfortran is the -ffloat-store option, which forces all floating-point values to go through a memory store before being used, thereby assuring that they're consistently truncated to 64 bits. It's worth a try to see if using that flag causes your problem to disappear. (It does slow down the program a bit, though.)

- Brooks


--
The "bmoses-nospam" address is valid; no unmunging needed.
.



Relevant Pages

  • Re: floating-point operations on modern hardware
    ... I have tried coding my own floating-point numbers using fixed-point ... The pentium 4 hardware has registers internally for floating point ... another back from the memory to the register. ...
    (sci.math.num-analysis)
  • Re: ICFP Contest
    ... This the BALANCE machine puzzle concocted by Y. Yang. ... Each of the source and destination registers is an indirect register. ... stored in the memory location indicated by the current contents of D. ... industry's leading programmer certification programs. ...
    (comp.lang.forth)
  • Re: ICFP Contest
    ... This the BALANCE machine puzzle concocted by Y. Yang. ... Each of the source and destination registers is an indirect register. ... stored in the memory location indicated by the current contents of D. ... industry's leading programmer certification programs. ...
    (comp.lang.forth)
  • Re: The variable bit cpu
    ... Surely you don't mean a bit at a time; memory ... > The CPU could act upon "virtual registers". ... There is nothing in the instruction set of any processor that I have ever ...
    (sci.electronics.design)
  • Re: speed it up
    ... can load many registers at once from memory and put many instructions ... the inner loop is unrolled ... The above loop tells the compiler that 4 registers ...
    (comp.lang.cpp)