September 23rd, 2012, 03:09 PM

Issue with double precision
Hi all,
using g++ on Ubuntu, processor Intel Core 2 Duo, I found this strange behavior:
Executing the code:
Code:
double x, y, a;
x = pow(10.0,20.0);
y = x;
a = 1000.0;
x += a;
printf(" x + y = %f \n", x + y);
I correctly get the result: x + y = 0.000000, because 1000 added to 10^20 is equal to 10^20 in IEEE 754 double precision 64 bit format.
However, if I execute the equivalent code:
Code:
double x, y, a;
x = pow(10.0,20.0);
y = x;
a = 1000.0;
printf(" x + y = %f \n", (a + x) + y);
I get: x + y = 1000.000000
I cannot understand why. Are perhaps CPU/ALU registers used to compute expressions longer than 64 bit?
If I set a = 1, I get x+y = 0; with a = 10 I get x+y = 8; with a = 100 I get x+y = 96.
Is there somebody who knows why?
TIA
Michele
September 23rd, 2012, 04:19 PM

Originally Posted by michelema
Are perhaps CPU/ALU registers used to compute expressions longer than 64 bit?
The x86 FPU has 80bit internal registers, so that is entirely possible; by avoiding copying of intermediate values to a 64bit double, additional precision may be retained. To see how it works you could look at the assembler code generated or step the code at assembler level in GDB. I am not sure if GDB will show the 80bit FPU registers however.
Incidentally in Visual C++, the long double type is 80bits, but in gcc/g++ it is I believe the same as double.
Last edited by clifford; September 23rd, 2012 at 04:22 PM.