#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

    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
  2. #2
  3. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    Originally Posted by michelema
    Are perhaps CPU/ALU registers used to compute expressions longer than 64 bit?
    The x86 FPU has 80-bit internal registers, so that is entirely possible; by avoiding copying of intermediate values to a 64-bit 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 80-bit FPU registers however.

    Incidentally in Visual C++, the long double type is 80-bits, but in gcc/g++ it is I believe the same as double.
    Last edited by clifford; September 23rd, 2012 at 03:22 PM.

IMN logo majestic logo threadwatch logo seochat tools logo