help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: g++ Bug? Why w/ and w/o "O3" options gives different result(g++3.3.3


From: Ulrich Eckhardt
Subject: Re: g++ Bug? Why w/ and w/o "O3" options gives different result(g++3.3.3) for this program?
Date: Sat, 04 Dec 2004 16:59:07 +0100
User-agent: KNode/0.7.7

One thing up front: http://got.to/quote

Peng Yu wrote:
> The following minimum program shows that if you uncomment the
> commented line, with and w/o -O3 option will give different results.
> 
> However, if you commented that line, with and w/o -O3 option will give
> the same results.
> 
> If you make g_range_down an integer and uncomment the first cout
> statement, the results will also be the same.
> 
> I wonder why the first cout statement and the declaration of
> g_range_down as an array will affect the compile result.
[...]
> int main(int argc, char *argv[])
> {
>   double g_range_down[3];
>   g_range_down[2] = - 0.5;
> //  cout << g_range_down[2] << endl;
>   cout << ceil(g_range_down[2] / 0.02) << endl;
> }

0.02 can't be represented exactly in binary and the number that is really
used is either larger or smaller than 0.02. Depending on the precision,
the most exact representation is either smaller or larger, and therefore
the result also varies.
Now, a smart optimizer might merge the three lines of your program
partially, and keep the value in a register, until it is passed to ceil().
These registers might be 80 bit wide, so the computation takes place with
a higher precision and the above mentioned differences happen.
If the value needs to be passed to a function in between, the compiler
probably wont keep it in a register all the time, so the results vary.
Just calling a function (even without touching g_range_down) might have
the same effect.

There is nothing wrong with GCC. If at all, it is your program that fails
to take these things into account.

Uli

-- 
http://gcc.gnu.org/faq.html
http://parashift.com/c++-faq-lite/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]