Thread: IEEE problem

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    15

    IEEE problem

    With x = 0101 1111 1011 1110 0100 0000 0000 0000(two) and
    y = 0011 1111 1111 1000 0000 0000 0000 0000(two), and
    z = 1101 1111 1011 1110 0100 0000 0000 0000 0000(two)
    representing single precision IEE 754 floating-point numbers, perform, showing all work:

    a) x + y

    b) (result of part a) + z

    c) Why is this result counterintuitive?



    a) i got 2.74x10^10
    b)z is -2.74x10^10
    so the reult is 0
    c) i don't see why it's counterintuative

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Because I'm lousy at doing floating point math by hand, I wrote a little program:
    Code:
    // fpnum.cpp : Defines the entry point for the console application.
    //
    #include <iostream>
    #include <cmath>
    
    using std::cout;
    using std::endl;
    
    union floatint {
    	float f;
    	int   i;
    };
    
    const int xi = 0x5FBE4000;  // 0101 1111 1011 1110 0100 0000 0000 0000
    const int yi = 0x3FF80000;	// 0011 1111 1111 1000 0000 0000 0000 0000
    const int zi = 0xDFBE4000;  // 1101 1111 1011 1110 0100 0000 0000 0000
    
    int main(int argc, char* argv[])
    {
        floatint x, y, z;
    
    	x.i = xi;
    	y.i = yi;
    	z.i = zi;
    
    	cout << "x = " << x.f << endl;
    	cout << "y = " << y.f << endl;
    	cout << "z = " << z.f << endl;
    
    	float a = x.f + y.f;
    	cout << "a) x + y = " << a << endl;
    
    	float b = a + z.f;
    	cout << "b) a + z = " << b << endl;
    
    	return 0;
    }
    The output is:
    Code:
    x = 2.74179e+019
    y = 1.9375
    z = -2.74179e+019
    a) x + y = 2.74179e+019
    b) a + z = 0
    I guess the reason it may be counterintuitive is that adding a number to another number and then subtracting the first number (or adding a negative version of it, as in this case) should result in something other than zero - becasue b is non-zero, right?

    And the reason for this is as I stated earlier: The numbers are normalized, so the 1.9375 is shifted right to match up with 2.74179e+19, which makes it zero (because 0.00000....00019375e+19 is too many digits to fit in a 32-bit float.

    [It seems like you got the exponent wrong in your conversion - or you typed it wrong]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You can look at it this way,
    (x + y) + z = 0
    but...
    (x + z) + y = 1.9375
    Addition is (normally) an associative/commutative operation, that is, A + B + C = (A + B) + C = A + (B + C) = (A + C) + B, etc... But in this case, depending on the order of operations for the addition, you can get different results.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by hk_mp5kpdw View Post
    You can look at it this way,
    (x + y) + z = 0
    but...
    (x + z) + y = 1.9375
    Addition is (normally) an associative/commutative operation, that is, A + B + C = (A + B) + C = A + (B + C) = (A + C) + B, etc... But in this case, depending on the order of operations for the addition, you can get different results.
    Good explanation. Thanks for the clarification.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM