Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

a c++ program returns different results in two IDE

I write the following c++ program in CodeBlocks, and the result was 9183. again I write it in Eclipse and after run, it returned 9220. Both use MinGW. The correct result is 9183. What's wrong with this code? Thanks. source code:

#include <iostream>
#include <set>
#include <cmath>

int main()
{
   using namespace std;
   set<double> set_1;
   for(int a = 2; a <= 100; a++)
   {
       for(int b = 2; b <= 100; b++)
       {
           set_1.insert(pow(double(a), b));
       }
   }
    cout << set_1.size();

return 0;
}
like image 697
Sam Avatar asked Nov 29 '12 22:11

Sam


1 Answers

You are probably seeing precision errors due to CodeBlocks compiling in 32-bit mode and Eclipse compiling in 64-bit mode:

$ g++ -m32 test.cpp
$ ./a.out
9183
$ g++ -m64 test.cpp
$ ./a.out
9220
like image 186
Justin ᚅᚔᚈᚄᚒᚔ Avatar answered Oct 03 '22 02:10

Justin ᚅᚔᚈᚄᚒᚔ