# IEEE problem

• 09-19-2007
Lind
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
• 09-19-2007
matsp
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
• 09-19-2007
hk_mp5kpdw
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.
• 09-19-2007
matsp
Quote:

Originally Posted by hk_mp5kpdw
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