A demonstration of how compilers give many different results for the same piece of code.
"g++" is GCC 4.1.2 on Linux. "icc" is the Intel C++ Compiler for Linux, 10.0.023. "cl" is Visual C++ 2008. All code compiled and run on 32-bit Core 2 CPU.
The code is:
T a = 1e20;
T b = arg;
T c = a*b;
T d = (a*b)/a;
T e = sqrt(a*a);
T f = (a*b)-(a*b+1);
T g = (a*a)-(a*a+1);
// then printf everything, using "%f" or "%Lf" as appropriate
where 'arg' is a variable (not a compile-time constant) equal to 1e20.
Output from various compiler configurations, with various T types: (Differences from the first row are highlighted in yellow)
Compiler | float | double | long double |
---|---|---|---|
g++ -O0 | [0]a=1e20: 100000002004087734272.000000 b=arg: 100000002004087734272.000000 a*b: inf (a*b)/a: 100000002004087734272.000000 sqrt(a*a): 100000002004087734272.000000 (a*b)-(a*b+1): 0.000000 (a*a)-(a*a+1): 0.000000 | [1]a=1e20: 100000000000000000000.000000 b=arg: 100000002004087734272.000000 a*b: 10000000200408773773888108121297313071104.000000 (a*b)/a: 100000002004087734272.000000 sqrt(a*a): 100000000000000000000.000000 (a*b)-(a*b+1): 0.000000 (a*a)-(a*a+1): 0.000000 | [2]a=1e20: 100000000000000000000.000000 b=arg: 100000002004087734272.000000 a*b: 10000000200408773427384467440737095516160.000000 (a*b)/a: 100000002004087734272.000000 sqrt(a*a): 100000000000000000000.000000 (a*b)-(a*b+1): 0.000000 (a*a)-(a*a+1): 0.000000 |
g++ -O1 | [3]a=1e20: 100000002004087734272.000000 b=arg: 100000002004087734272.000000 a*b: inf (a*b)/a: inf sqrt(a*a): 100000002004087734272.000000 (a*b)-(a*b+1): nan (a*a)-(a*a+1): nan | [same as 1] | [same as 2] |
g++ -O2 | [4]a=1e20: 100000002004087734272.000000
b=arg: 100000002004087734272.000000
a*b: 10000000400817550870767646659478483369984.000000
(a*b)/a: 100000002004087734272.000000
sqrt(a*a): 100000002004087734272.000000
(a*b)-(a*b+1): 0.000000
(a*a)-(a*a+1): 0.000000
| [same as 1] | [same as 2] |
g++ -O3 | [same as 4] | [same as 1] | [same as 2] |
g++ -O0 -ffast-math | [same as 0] | [same as 1] | [same as 2] |
g++ -O1 -ffast-math | [5]a=1e20: 100000002004087734272.000000 b=arg: 100000002004087734272.000000 a*b: 10000000400817550870767646659478483369984.000000 (a*b)/a: 100000000834727641088.000000 sqrt(a*a): 100000002004087734272.000000 (a*b)-(a*b+1): 0.000000 (a*a)-(a*a+1): 0.000000 | [same as 1] | [6]a=1e20: 100000000000000000000.000000
b=arg: 100000002004087734272.000000
a*b: 10000000200408773427384467440737095516160.000000
(a*b)/a: 100000002004087734280.000000
sqrt(a*a): 100000000000000000000.000000
(a*b)-(a*b+1): 0.000000
(a*a)-(a*a+1): 0.000000
|
g++ -O0 -ffloat-store | [7]a=1e20: 100000002004087734272.000000 b=arg: 100000002004087734272.000000 a*b: inf (a*b)/a: inf sqrt(a*a): inf (a*b)-(a*b+1): nan (a*a)-(a*a+1): nan | [same as 1] | [same as 2] |
g++ -O1 -ffloat-store | [same as 7] | [same as 1] | [same as 2] |
g++ -O2 -ffloat-store | [same as 7] | [same as 1] | [same as 2] |
g++ -O0 -frounding-math | [same as 0] | [same as 1] | [same as 2] |
g++ -O1 -frounding-math | [same as 3] | [same as 1] | [8]a=1e20: 100000000000000000000.000000
b=arg: 100000002004087734272.000000
a*b: 10000000200408773427384467440737095516160.000000
(a*b)/a: 100000002004087734272.000000
sqrt(a*a): 100000000000000001520.000000
(a*b)-(a*b+1): 0.000000
(a*a)-(a*a+1): 0.000000
|
g++ -O2 -frounding-math | [same as 4] | [same as 1] | [same as 8] |
g++ -O0 -frounding-math -ffloat-store | [same as 7] | [same as 1] | [same as 2] |
g++ -O1 -frounding-math -ffloat-store | [same as 7] | [same as 1] | [same as 2] |
g++ -O2 -frounding-math -ffloat-store | [same as 7] | [same as 1] | [same as 2] |
g++ -O1 -msse -mfpmath=sse | [same as 7] | [same as 1] | [same as 2] |
g++-3.3.6 -O2 | [9]a=1e20: 100000002004087734272.000000 b=arg: 100000002004087734272.000000 a*b: inf (a*b)/a: 100000002004087734272.000000 sqrt(a*a): inf (a*b)-(a*b+1): 0.000000 (a*a)-(a*a+1): nan | [same as 1] | [same as 8] |
g++-3.4.6 -O2 | [10]a=1e20: 100000002004087734272.000000 b=arg: 100000002004087734272.000000 a*b: 10000000400817550870767646659478483369984.000000 (a*b)/a: 100000002004087734272.000000 sqrt(a*a): inf (a*b)-(a*b+1): 0.000000 (a*a)-(a*a+1): nan | [same as 1] | [same as 2] |
icc -O0 | [same as 0] | [same as 1] | [same as 2] |
icc -O1 | [11]a=1e20: 100000002004087734272.000000 b=arg: 100000002004087734272.000000 a*b: inf (a*b)/a: 100000002004087734272.000000 sqrt(a*a): inf (a*b)-(a*b+1): nan (a*a)-(a*a+1): nan | [same as 1] | [same as 2] |
icc -O2 | [12]a=1e20: 100000002004087734272.000000 b=arg: 100000000000000000000.000000 a*b: 10000000200408773773888108121297313071104.000000 (a*b)/a: 100000000000000000000.000000 sqrt(a*a): inf (a*b)-(a*b+1): 0.000000 (a*a)-(a*a+1): nan | [13]a=1e20: 100000000000000000000.000000 b=arg: 100000000000000000000.000000 a*b: 10000000000000000303786028427003666890752.000000 (a*b)/a: 100000000000000000000.000000 sqrt(a*a): 100000000000000000000.000000 (a*b)-(a*b+1): 0.000000 (a*a)-(a*a+1): 0.000000 | [14]a=1e20: 100000000000000000000.000000 b=arg: 100000000000000000000.000000 a*b: 9999999999999999999783686092270256259072.000000 (a*b)/a: 100000000000000000000.000000 sqrt(a*a): 100000000000000000000.000000 (a*b)-(a*b+1): 0.000000 (a*a)-(a*a+1): -1.000000 |
icc -O3 | [same as 12] | [same as 13] | [same as 14] |
icc -O2 -fp-model double | [same as 0] | [same as 1] | [same as 2] |
icc -O2 -fp-model extended | [same as 0] | [same as 1] | [same as 2] |
icc -O2 -fp-model fast | [same as 12] | [same as 13] | [same as 14] |
icc -O2 -fp-model fast=1 | [same as 12] | [same as 13] | [same as 14] |
icc -O2 -fp-model fast=2 | [same as 12] | [same as 13] | [same as 14] |
icc -O2 -fp-model precise | [same as 0] | [same as 1] | [same as 2] |
icc -O2 -fp-model source | [same as 7] | [same as 1] | [same as 2] |
icc -O2 -fp-model strict | [same as 0] | [same as 1] | [same as 2] |
icc -O2 -mp | [same as 0] | [same as 1] | [same as 2] |
icc -O2 -mp1 | [same as 12] | [same as 13] | [same as 14] |
icc -O2 -prec-div | [same as 12] | [same as 13] | [same as 14] |
icc -O2 -prec-sqrt | [same as 12] | [same as 13] | [same as 14] |
icc -O2 -pc32 | [15]a=1e20: 100000002004087734272.000000 b=arg: 100000000000000000000.000000 a*b: 10000000288757980574615738795334859489280.000000 (a*b)/a: 100000002004087734272.000000 sqrt(a*a): inf (a*b)-(a*b+1): 0.000000 (a*a)-(a*a+1): nan | [16]a=1e20: 100000000000000000000.000000 b=arg: 100000000000000000000.000000 a*b: 10000000288757980574615738795334859489280.000000 (a*b)/a: 100000002004087734272.000000 sqrt(a*a): 100000000000000000000.000000 (a*b)-(a*b+1): 0.000000 (a*a)-(a*a+1): 0.000000 | [17]a=1e20: 100000000000000000000.000000 b=arg: 100000000000000000000.000000 a*b: 10000000288757980574615738795334859489280.000000 (a*b)/a: 100000002004087734272.000000 sqrt(a*a): 100000002004087734272.000000 (a*b)-(a*b+1): 0.000000 (a*a)-(a*a+1): -1.000000 |
icc -O2 -pc64 | [same as 12] | [same as 13] | [18]a=1e20: 100000000000000000000.000000 b=arg: 100000000000000000000.000000 a*b: 10000000000000000303786028427003666890752.000000 (a*b)/a: 100000000000000000000.000000 sqrt(a*a): 100000000000000000000.000000 (a*b)-(a*b+1): 0.000000 (a*a)-(a*a+1): -1.000000 |
icc -O2 -pc80 | [same as 12] | [same as 13] | [same as 14] |
icc -O2 -fbuiltin | [same as 12] | [same as 13] | [same as 14] |
icc -O2 -fno-builtin | [19]a=1e20: 100000002004087734272.000000 b=arg: 100000000000000000000.000000 a*b: inf (a*b)/a: 100000002004087734272.000000 sqrt(a*a): inf (a*b)-(a*b+1): nan (a*a)-(a*a+1): nan | [same as 13] | [same as 14] |
cl /Od | [20]a=1e20: 100000002004087730000.000000 b=arg: 100000002004087730000.000000 a*b: 1.#INF00 (a*b)/a: 100000002004087730000.000000 sqrt(a*a): 1.#INF00 (a*b)-(a*b+1): 0.000000 (a*a)-(a*a+1): 0.000000 | [21]a=1e20: 100000000000000000000.000000 b=arg: 100000002004087730000.000000 a*b: 10000000200408774000000000000000000000000.000000 (a*b)/a: 100000002004087730000.000000 sqrt(a*a): 100000000000000000000.000000 (a*b)-(a*b+1): 0.000000 (a*a)-(a*a+1): 0.000000 | [same as 21] |
cl /O1 | [same as 20] | [same as 21] | [same as 21] |
cl /O2 | [same as 20] | [same as 21] | [same as 21] |
cl /Ox | [same as 20] | [same as 21] | [same as 21] |
cl /O2 /fp:fast | [22]a=1e20: 100000002004087730000.000000 b=arg: 100000002004087730000.000000 a*b: 10000000400817551000000000000000000000000.000000 (a*b)/a: 100000000834727640000.000000 sqrt(a*a): 100000002004087730000.000000 (a*b)-(a*b+1): 0.000000 (a*a)-(a*a+1): 0.000000 | [same as 21] | [same as 21] |
cl /O2 /fp:precise | [same as 20] | [same as 21] | [same as 21] |
cl /O2 /fp:strict | [same as 20] | [same as 21] | [same as 21] |
cl /O2 /arch:SSE | [same as 20] | [same as 21] | [same as 21] |
cl /O2 /arch:SSE2 | [same as 20] | [same as 21] | [same as 21] |