Search code examples
assemblyx86denormalized

IEEE 754 Denormalized Decimal Converting to Half-Point Binary


I am trying to convert 0.0000211 to binary. Here is what I understand so far:

E = -bias + 1. bias = 15, E = -14

Sign bit and exponent = 0.

So I have:

0 00000 ??????????

With the half-point format being 1 sign bit, 5 exponent bits, and 10 fraction bits.

My question is how can I find the fraction of this denormalized number? What does E, and the bias mean in this context? Any help would be appreciated

Note: I need to be able to do this manually for my final.


Solution

  • The mantissa (OPs ? bits) of a half, float or double is normalized to remove the leading zeros. Usually this is done until the number is, 1.0 <= number < 2.0. But in this case the number is in the sub-normals range (The exponent is 00000 as you've already established. Which means the original number was less than the minimum normal of 6.10352 × 10^−5, ie when you're trying to shift to make the number 1.0 <= number < 2.0, you hit the exponents minimum limit), in this case they shift 15 times, ie multiply by 2^15 and store as many bits after the point as possible (for half floats this is 10bits). Doing this means they can store very small numbers, because for the sub-normal range they have an implicit 0. in front of the mantissa when restoring the number and they allow leading zeros on the mantissa.

    So 0.0000211 = b'0.000000000000000101100001111111111100111...

    2^15 * 0.0000211 = 0.6914048 = b'0.101100001111111111100111...

    We store 1011000011 because the sub normal range removes the implicit 0. (ie for 0.XXXXXXXXXX we only store the Xs)

    So in this case the mantissa (OPs ? bits) are 1011000011

    sign   exp      mantissa
    0      00000    1011000011
    

    This can be checked with python using numpy and struct

    >>> import numpy as np
    >>> import struct
    >>> a=struct.pack("H",int("0000001101010000",2))
    >>> np.frombuffer(a, dtype =np.float16)[0]
    2.116e-05
    

    So for your final... At the very least you're going to need to learn how to turn a decimal less than 1.0 into a binary, and remember a few rules. You seem to be on top of calculating the exponent.

    Have a look at...

    https://math.stackexchange.com/questions/1128204/how-to-convert-from-floating-point-binary-to-decimal-in-half-precision16-bits

    One of the answers to this question has python code for the whole conversion. Which may be useful for learning.