0
$\begingroup$

This isn't for any class or homework. As part of my personal study, I'm trying to better understand the IEEE754 representation of decimal floating-point numbers in binary. I'd like to add two numbers: $1.111$ and $2.222$, then compare the result by converting the IEEE754 representation of the sum back to decimal.

Per this online tool:

  • $1.111 = 00111111100011100011010100111111$
  • $2.222 = 01000000000011100011010100111111$

Summing these two together using signed binary addition, I get:

$0111 1111 1001 1100 0110 1010 0111 1110$

In hexadecimal, this is:

$7F9C6A7E$

And according to this other version of the tool, that corresponds to $NaN$.

What's going on here?

$\endgroup$
  • $\begingroup$ You can't expect doing integer addition on floating-point representations to give meaningful results. $\endgroup$ – hmakholm left over Monica Nov 25 '18 at 1:01
  • $\begingroup$ How would I go about trying to do what I want to do here? $\endgroup$ – AleksandrH Nov 25 '18 at 1:06
  • $\begingroup$ I have no idea what it is you want to do. Use floating-point addition rather than integer? $\endgroup$ – hmakholm left over Monica Nov 25 '18 at 1:07
  • $\begingroup$ Yes, I was under the impression that once I have the two floating-point numbers represented as binary strings, I could simply add them together bit by bit and then translate the resulting 32-bit string to decimal floating point. The IEEE754 standard defines conversions in both directions (binary to decimal and decimal to binary). $\endgroup$ – AleksandrH Nov 25 '18 at 1:12
  • $\begingroup$ You have to adjust them so they have the same mantissa before you add them. You ought to read about what the IEEE754 representation is actually constructed. $\endgroup$ – saulspatz Nov 25 '18 at 1:12
2
$\begingroup$

You cannot expect to use integer binary addition on two floating-point representations and get a meaningful result.

First, $1.111$ cannot be represented exactly in binary floating point. Your 00111111100011100011010100111111 is actually the IEEE-754 single precision representation of the number $$ 1.11099994182586669921875 $$ which is the closest representable number to $1.111$. This breaks up as

  0      01111111        00011100011010100111111
sign  biased exponent  fractional part of mantissa

and stands for the number $$ 1.00011100011010100111111_2 \times 2^{127-127} $$

The representation of $2.222$ is twice that, with the same mantissa but the exponent one higher. When we add them we must position the mantissas correctly with respect to each other:

   1.00011100011010100111111
+ 10.0011100011010100111111
----------------------------
= 11.01010101001111110111101
  11.0101010100111111011110   <-- rounded to 1+23 bits mantissa using round-to-even

 0    10000000   10101010100111111011110
sign biased exp    fractional mantissa

And the representation 01000000010101010100111111011110 corresponds to the number $$ 3.332999706268310546875 $$ Note that this is not the closest representable number to $3.333$, which would be the next one, $$ 3.33329999446868896484375 $$ but the round-to-even rule led to rounding down the full result of the addition, which compounded the error inherent in the two inputs each being slightly smaller than $1.111$ and $2.222$.

$\endgroup$
  • $\begingroup$ I followed this well until we got to the $10.00...$ part. Why did the decimal point move one place to the right? $\endgroup$ – AleksandrH Nov 25 '18 at 1:43
  • $\begingroup$ @AleksandrH: Because the second addend has a biased exponent of 10000000, so it represents the number $1.\langle\mathit{mantissa}\rangle_2 \times 2^{128-127}$ -- in other words the binary points is shifted one position to the right. $\endgroup$ – hmakholm left over Monica Nov 25 '18 at 1:46
  • $\begingroup$ Yeah, I don't understand. Sorry for wasting your time. $\endgroup$ – AleksandrH Nov 25 '18 at 14:06
  • $\begingroup$ @AleksandrH: The job of the exponent is to encode where the binary point is. That's what makes the representation "floating point" -- you can move the point! In the $2.22$ representation the exponent is $1$ (after we subtract the fixed bias), meaning that the point is after one of the explicitly represented mantissa bits. $\endgroup$ – hmakholm left over Monica Nov 25 '18 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.