Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Is it a Bug in g++ compiler`s -O flag?? (http://www.go4expert.com/forums/bug-g-compilers-o-flag-t1649/)

nikhil_dokania 17Oct2006 01:20

Is it a Bug in g++ compiler`s -O flag??
 
The output is different for the following two codes when compiled as : "g++ -O foo.cpp"
1st COde :
Code:

#include<iostream.h>
#include<math.h>
int main(){
        double d=log(8)/log(2);
        cout<<"d="<<d<<"\t floor(d)="<<floor(d);        //<<"\t"<<floor(10.0)<<"\n";
        return 0;
}

------------------------------------------------------------------------------------------------------------------
2nd COde :
Code:

#include<iostream.h>
#include<math.h>
int main(){
        double d=log(8)/log(2);
        cout<<"d="<<d<<"\t floor(d)="<<floor(d)<<"\t"<<floor(10.0)<<"\n";
        return 0;
}

---------------------------------------------------------------------------------------------------------------

Expected output : d=3 floor(d)=3 10
But The First code give : d=3 floor(d)=2 10
while the second gives the right output.
Could nyone plz explain whats going wrong!!

Nikhil

shabbir 17Oct2006 10:18

Re: Is it a Bug in g++ compiler`s -O flag??
 
Quote:

Originally Posted by nikhil_dokania
Expected output : d=3 floor(d)=3 10

Nope. Its should be d=3 floor(d)=2 10 because log(8)/log(2) = 2.999999..... and so floor should be 2.

I guess the difference in output should be a bug as far as I think.

nikhil_dokania 19Oct2006 23:12

Re: Is it a Bug in g++ compiler`s -O flag??
 
i think u r right. and thanx for replying. But the difference in output is not recognized as a Compiler bug. I jus learned it from this GCC website. Check it out: http://gcc.gnu.org/bugs.html#nonbugs_general


All times are GMT +5.5. The time now is 14:45.