# Conversion to ReSyFloat if |x|<1

Hi :)

I tried to convert multible numbers from decimal to ReSyFloat. Unfortunaly the procedure given in the exercise lessons doesn't give me the right solution if |x|<1.

Let's look at 0.2 for example: I started to calculate the mantissa by saying the first digit is 0 and I have 3 digits left. Then I calulated 0.2*2^3=1.6. I thought the possible mantissas would be 0010 and 0001. But this isn't the case... I tried to understand the way of calculating it with help of the teaching tool but i wasn't able to figure it out.

Also I don't understand when to use denormalized numbers. Maybe someone can help me. Thank you in advance!!!
Could you add the specific format? Number of bits of exponent and mantissa. And if there is a hidden bit.
Oh yeah of course. Let's say m=4, e=3 and B=2. If poosible I would be interested in seeing both cases, with and without hidden bit. Thanks

+1 vote

Let's convert 0.2 to a floating point number with 4 bits for the mantissa and three bits for the exponent. First, let's consider what the teaching tool does.

To describe the input, we have to specify the number as a quotient of integers, e.g., 2/10. The tool first translates these two numbers to radix-2 numbers and reads it as <10>_2 / <1010>_2. It realizes that it needs to scale the division by a factor of 2^7, i.e., to compute +<100000000>_2 / <1010>_2 * 2^{-7} to have sufficiently many digits for the mantissa when doing an integer division. The result of this integer division is now

`+<1.1001...0/1...>_2 * 2^{-3}`

The meaning of this is as follows: the mantissa is normalized (as you can see it starts with a 1 left of the decimal point). The next three digits given in blue color will be our remaining bits for the mantissa. The next bit given in red color is called the rounding bit; it would be the next bit after our mantissa, and finally the 0/1 given in green color will tell us whether there are futher 1 digits after this one (this is known by the integer division +<100000000>_2 / <1010>_2 by checking the remainder). Sometimes people call the green part the sticky bit in that they provide a bit which is 1 if there are further 1 bits and 0 otherwise.

Now, we either have to choose one of the representable numbers +<1.100>_2 * 2^{-3} or +<1.101>_2 * 2^{-3} since our actual number 0.2 = +<1.1001...0/1...>_2 * 2^{-3} cannot be precisely represented. Which one of the two is chosen depends on the rounding mode, and there we have to consider three cases:

1. In case the rounding bit would be 0, then the number would be closer to +<1.100>_2 * 2{-3}.
2. However, as the rounding bit is 1, and there are further 1-bits after this (the green ones), our number is closer to +<1.101>_2 * 2{-3}.
3. There could also be the third case, where the rounding bit is 1, and there are no further green bits on the right. In that case, the number would be right in the middle of the representable numbers +<1.100>_2 * 2{-3} and +<1.101>_2 * 2{-3}.

The table that follows in the output of the teaching tool tells you the numbers chosen for the different rounding modes.

If we would like to use a hidden bit, we should specify one further bit for the teaching tool, and the omit the leading 1 of the mantissa as the hidden bit.

When are denormal numbers used? They are used when the exponent becomes zero. In that case, we cannot reduce the exponent further to scale the mantissa, and we have to live with a mantissa that is not normalized. Recall that normalizing means that the mantissa is between 1 and 2, i.e., in binary 1.xxxxx. If that cannot be achieved with the exponents that we have, you need to represent the number as a denormal number (or it is an overflow).

Looking at the slides 23-26, you need to compute it as follows: First, normalize the number, i.e., represent it as M*2^{E} such that 1≤M<2 holds. In our case, we get

`    0.2 = 0.4 * 2^{-1} = 0.8 * 2^{-2} = 1.6 * 2^{-3}`

So, we have M = 1.6 and E = -3. With 3 bits for the exponent, we obtain the offet beta .= (2^{3}-1)/2 = 3. Hence, E+beta = -3+3 = 0, and the exponent that we should use is 0, so we will have a denormalize number (indicated by exponent 0).

Now note that 1.6 = 1 + 1/2 + 1/16 + ... which gives us the same information as mentioned above, i.e.

`+<1.1001...0/1...>_2 * 2^{-3}`
The rest can be explained as before.

Does it help?

by (162k points)
edited by
Thank you for the detailed explanation. Most of those things in the middle I already understood but it helped to read it as a whole. The important thing I did not understand (but is possible not that hard) is how to determine the scale factor for our division at the beginning. How do I calculate that it is 7? And did I get it right that I have to do a normal radix 2 division afterwards?
There are formulas for the scale factor, but they are actually not worth remembering. Just check how I did the calculation in the second part: You double (or halven) the number as long as needed to bring in between 1 and 2. That determines the exponent. Having such a number you could then do a integer division to get the digits, but again, it is easier to write it as a sum of powers of 2 using negative exponents, i.e., as sum_{i=0}^n x_i 2^{-I} where n is large enough to get the required bits of the mantissa, the rounding bit and the sticky bit (knowing the green ones).
Thank you a lot. The last part was the one i needed to understand what is going on. I totally overlooked that i need to use a negative exponent at first to produce a numer which is larger than one. This makes a lot of sense!!!
Glad that I could help, and maybe I wrote way too much, but I wanted you to understand the output of the teaching tool that can greatly guide you by checking your examples.