# How do I convert 1/3 to signed float 8 bit?

The formula is as follows:

(-1)^[signed bit] * (1.[significant bit]) * (2^[excess bit -3])

or

(-1)^[s] * (1.[f]) * (2^[x-3])

It is a question posed by my signals and systems instructor, hence the electrical tag.(electrical engineering tag is two letters and not available)

The signed bit (s) has a maximum of 1 total bits. This represents the sign of the decimal. The mantissa has a range of 1-2 and the significant bits has a decimal range of 0-1. There are a maximum 4 bits in f and 3 bits in x.

I can only assume that it is not possible to represent 1/3 exactly using 8 bits, given that 1.f > 1, x < 3. That is the only way to represent the final tally as < 1. The sign is positive, so the signed bit is 0. By tabulating -1> x < 3 with f = .125, .25, .5

• this looks like a math question or a programming question .... please explain why you used the electrical tag – jsotola Feb 3 at 19:38
• Not enough detail, your symbols aren't defined, and I don't see a mantissa in your $F(p)$ expression. Oh -- and it's clearly homework, so show how much work you've done so far, what you think you have, and why you think you're stuck. – TimWescott Feb 3 at 19:43
• @MachiAz: please edit your question with the above information. When you do that, also say how many bits in s, f, and x, and how they fit into 8 bits. And show your work so far -- this is not a "solve your homework for you" site. It's a "help you find your own solution" site. – TimWescott Feb 3 at 19:57
• @MachiAz, you are correct. 1/3 cannot be represented exactly in binary (nor in decimal: 0.333333...). Your 8 bit representation will only be an approximation. – The Photon Feb 3 at 20:03
• There are many, many numbers that can not be represented exactly using any number of binary bits...that is just a fact of life. You can't represent 0.1 exactly in binary fixed-point. – Elliot Alderson Feb 3 at 20:04

Your notation is pretty standard. The sign bit is obvious to anyone, so I won't address that further. The mantissa is in hidden-bit notation (the "1." is assumed, except in the unique case of an exact zero, which you didn't discuss.) The exponent is in the usual "excess" notation (some bias is added, so that a stored/packed exponent of zero represents the smallest allowable exponent.)

Start with the number you have as $$\v\$$ (this is a real number, not necessarily an integer) and the number of mantissa bits available as $$\m=4\$$ and the number of exponent bits available as $$\e=3\$$. Assuming that $$\v\ne 0\$$, apply the following logic:

1. set up a new variable as $$\p=0\$$.
2. if $$\v\$$ is positive, set new variable $$\s=0\$$ otherwise set $$\s=1\$$, set $$\v=\:\mid v \:\mid\$$.
3. while $$\v\lt 2^m\$$, set $$\p=p-1\$$ and $$\v=2\cdot v\$$.
4. while $$\v\ge 2^{m+1}\$$, set $$\p=p+1\$$ and $$\v=\frac{v}{2}\$$.
5. set up a new variable as $$\f=\lfloor v\rfloor\$$.
6. if $$\\left(v-f\right)\ge \frac{1}{2}\$$ set $$\f=f + 1\$$.
7. if $$\f = 2^{m+1}\$$ then set $$\p=p+1\$$, $$\f=\frac{f}2\$$.
8. set up a new variable as $$\x=p+m+2^{e-1}-1\$$.
9. if $$\x\lt 0\$$ or $$\x\ge 2^e\$$ then error.
10. it must be that $$\2^m\le f\lt 2^{m+1}\$$, so set $$\f=f-2^m\$$.

At this point, you find the result as $$\\left<[\;s\;]_1\quad[\;f\;]_m\quad[\;x\;]_e\right>\$$, with the subscripts indicating the number of bits for each field. (If $$\v=0\$$ then the entire result has all bits as zero.)

(Note that I've buried the exponent's excess value in the algorithm because it is almost always handled this way.)

Steps 1 and 2 handle the sign bit and prepare for the remaining steps; steps 3 and 4 perform value normalization; steps 5 and 6 handle value rounding; step 7 deals with the rare case where the rounding required a re-normalization step; step 8 computes the excess exponent notation and step 9 validates that result; and finally step 10 removes the hidden bit from the mantissa prior to packing the floating point notation word.

I just dashed this off quickly and I'll gladly correct any uncovered algorithm errors.