2
$\begingroup$

For a rational number 1/3 below is the floating point representation(64 bit) of decimal expansion 0.3333333....

enter image description here

As per the above bit structure, I would like to interpret the value of exponent(11 bits) and value of fraction(52 bits).

exponent has enter image description here 11 bits. How do I interpret exponent value?

fraction has enter image description here 52 bits. Here 0101 is decimal 5. Am not sure if we captured fractional part 33333333... of decimal expansion? To capture fractional part am I suppose to expect 0011 pattern instead of 0101. How do I interpret fractional part?

$\endgroup$
3
  • $\begingroup$ Floating point represents a number in binary but not by coding the individual decimal digits in binary. The same happens with integers: the decimal number 23 is coded as $0001\,0111$ ($2^4+2^2+2^1+2^0$), not $0010\,0011$ ("two and then three"). $\endgroup$ Commented Feb 25, 2015 at 7:49
  • $\begingroup$ That image is all but indecipherable. Please get a better image and, in any case, retype the actual representation. $\endgroup$
    – Raphael
    Commented Feb 25, 2015 at 8:14
  • $\begingroup$ Mantissa is 1 (implicit) + 0*(1/2)+1*(1/4)+0*(1/8)+1*(1/16) ... = 1.33333. Exponent is coded with a bias equal to 011 1111 1111. So here your exponent is 2^(-2)=1/4. 1.3333/4 = 0.3333 $\endgroup$
    – Grabul
    Commented Feb 26, 2015 at 1:53

1 Answer 1

4
$\begingroup$

The fractional part is in binary: $(0.01010101\ldots)_2 = (0.333333\ldots)_{10}$. To convince yourself of this, try multiplying the binary $0.0101010101\ldots$ by $3$: you will get $0.111111\ldots = 1$. In a similar way, the decimal $0.0101010101\ldots$ equals $1/99$.

The mantissa (fractional part) in fact has an implicit $1.$ in front, so it actually equals $(1.01010101\ldots)_2 \approx 4/3$. The exponent should therefore be $-2$, since $1/3 = 4/3 \cdot 2^{-2}$. The exponent itself is stored with an offset of $1024$ (or more generally, $2^{w-1}$, where $w$ is the width of the exponent field), that is, instead of $-2$ we actually store $1022$, which in binary reads $01111111101$.

You should note that the floating point number doesn't represent $1/3$ exactly, rather a number pretty close to $1/3$. The only numbers representable exactly in floating point (given enough precision) are dyadic numbers, which are numbers of the form $A/2^B$ (for integer $A,B$).

$\endgroup$
6
  • $\begingroup$ In python, >>> 1/3 == 0.333333333333333 gives False. >>> 1/3 == 0.3333333333333333 gives True. How do I understand this? $\endgroup$ Commented Mar 10, 2015 at 11:55
  • $\begingroup$ In other words, If 52 bits are 0101010101010101010101010101010101010101010101010101 as mentioned in the above query, then how many exact number of 3's come after the decimal? $\endgroup$ Commented Mar 10, 2015 at 12:41
  • 1
    $\begingroup$ There is no exact number of 3's after the decimal. For example, $(.01)_2 = 0.25$, $(.0101)_2 = 0.3125$, $(.010101)_2 = 0.328125$, $(.01010101)_2 = 0.33203125$, and so on. Those numbers approach $1/3$, but not via the decimals $0.3,0.33,0.333,\ldots$. $\endgroup$ Commented Mar 10, 2015 at 14:18
  • 2
    $\begingroup$ Re your other question, in Python 1/3 equals zero, since that's how integer division works. But even if you try 1./3, which does give you an approximation of one third, it shouldn't necessarily agree with any 0.333333333333333333333333, since the two numbers could have different representations (you might have got lucky in your case). When comparing two floating point numbers for equality, you should really test that they are very close rather than that they are exactly equal. Another reason for this is that some errors are incurred during computation. $\endgroup$ Commented Mar 10, 2015 at 14:21
  • $\begingroup$ If you want to understand these things further, I suggest you read up lecture notes on numerical analysis. $\endgroup$ Commented Mar 10, 2015 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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