问题
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;
}
回答1:
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
回答2:
If I cast both arguments to double I get what you would expect:
pow(static_cast<double>(a), static_cast<double>(b))
回答3:
The difference appears to be due to whether the floating point operations are using 53-bit precision or 64-bit precision. If you add the following two lines in front of the loop (assuming Intel architecture), it will use 53-bit precision and give the 9220
result when compiled as a 32-bit application:
uint16_t precision = 0x27f;
asm("fldcw %0" : : "m" (*&precision));
It is bits 8 and 9 of the FPU that control this precision. The above sets those two bits to 10
. Setting them to 11
results in 64-bit precision. And, just for completeness, if you set the bits to 00
(value 0x7f), the size is printed as 9230
.
回答4:
Actually you're not really supposed to rely on ==
(or technically, x <= y && y <= x
) for doubles anyway. So this code produces implementation-dependent results (not strictly speaking UB, per comments, but what I meant :) )
来源:https://stackoverflow.com/questions/13635546/a-c-program-returns-different-results-in-two-ide