From: | torbenm@diku.dk (Torben Ęgidius Mogensen) |
Newsgroups: | comp.compilers |
Date: | Mon, 28 Mar 2011 11:25:32 +0200 |
Organization: | SunSITE.dk - Supporting Open source |
References: | 11-03-054 |
Keywords: | C++, debug |
Posted-Date: | 29 Mar 2011 11:52:53 EDT |
"rhoads@cs.rutgers.edu" <rhoads@cs.rutgers.edu> writes:
> I have an C++ application program that is producing incorrect results
> but the error goes away when I run it through the debugger.
>
> When I used the -ggdb compiler flag so I can run it under the gdb
> debugger, the error goes away. It also runs fine if I do both of the
> following; extract a few of the input instances which generate
> incorrect results and put them in a separate input file.
> Use this input file and recompile the program with no optimization
> flags (I am otherwise using the -O3 optimization flag).
I have had a similar experience: My program was giving strange
results, so I inserted some print statements to write out intermediate
results. And promptly the problem disappeared. I traced the problem
to the compiler re-arranging some FP computations which caused a
change in the last bit, which made some values be unequal that would
otherwise be equal.
The moral is, of course, that I should not have used equality tests
for FP numbers, but instead used an "within epsilon" test. In
general, you should not expect FP calculations to give exactly the
same result after optimisation in C or C++, as the order of evaluation
is not fully specified and because the compiler might optimise a
multiplication followed by an addition to a multiply-and-add, which
can change rounding behaviour.
Torben
Return to the
comp.compilers page.
Search the
comp.compilers archives again.