0
votes

I have a fraction part of a float number (5 of -10.5, for instance). It is a character since I extracted it from the input. How can I convert that character into a binary fraction part of that float number? (I am building a float input -> HEX output in 32bit IEEE784, I have already extracted binary representations of the sign, exponent and integer part of mantissa.)

I have been thinking about implement the algorithm of multiplying the fraction by two and taking the remainder, then repeating till it fill the mantissa, but I am not allowed to use any floating point operations in an assignment.

Examples: User inputs -10.5. The program needs to take the fraction of the number (which is 5) and convert it to binary format (which is 1 (.1))

EDIT: I am limited by 16 bit register size thus I need a solution to operate on numbers > 2^16-1.

2
The question is not very clear. Please show some typical inputs, how they are obtained, and the required output in each case. And the code ("I have extracted...") - Weather Vane
At the moment, I have two 32 bit memory words, long type numbers, one is 1 (because of the sign is negative) and the other one is 10 in binary. I also have char array with character '5' as the first and only one element. It is the fraction of the number provided (it could be '05', '00001257852' and etc.). I just need to convert it to bits. - Lukas Valatka
... in the question, not as waffle please. - Weather Vane

2 Answers

5
votes

You basically have the right idea. Just double the decimal part and the carry becomes the number. Let's try converting .7 to binary as an example (.5 is too simple)

NUMBER REMAINDER CARRY
    .7       ---     .
   1.4        .4     1
    .8        .8     0
   1.6        .6     1
   1.2        .2     1
    .4        .4     0
    .8        .8     0
   1.6        .6     1
   1.2        .2     1
&ct.

So .7 in binary is .101100110.... 10.7 would just be 1010.101100110...

You can stop converting to binary once the remainder becomes 0, or once the total representation becomes 24 bits long due to the limits of IEEE float precision.

Note that the decimal itself is purely decorative, you aren't using floating point here.

NUMBER     %10    /10
     7     ---      .
    14       4      1
     8       8      0
    16       6      1
    12       2      1
     4       4      0
     8       8      0
    16       6      1
    12       2      1
&ct.

In the case of .07 you would represent the remainder and carry by %100 and /100 respectively. In the case of .007 you would use %1000 and /1000.

2
votes

In an IEEE-754 sense, there is no well-defined meaning for the fractional part of a floating-point number outside the context of the overall number. The bits that distinguish the value 10.5 from the value 10.0 are a smaller subset of the overall representation than are the bits that distinguish the value 0.0 from the value 0.5. When one talks about the "fraction" part of an IEEE-754 numeric representation, one normally is talking about all bits of the mantissa; for IEEE 754 single-precision binary format that's 24 bits, including one implied bit, regardless of the magnitude of the number.

UPDATE / REVISION:

Based on updates clarifying the question, I have replaced the bulk of my previous answer, which addressed the problem of programmatically extracting the bits of a float or double, as opposed to converting a decimal-format text representation to the bits of an IEEE-754 format representation. Parsing text is a tricky problem, as I also said in the original version of this answer.

I do nevertheless reiterate that I think it a mistake to separate the mantissa into separate pieces. It offers no particular advantage that I can see, but it introduces the problem of properly recombining the pieces later.

Here's a workable approach to the overall problem:

  1. Determine the sign of the input number based on the presence or absence of a negative sign.

  2. Form a bignum representation of the absolute value of the input number. For example, a base-1,000,000,000 representation, stored in an array of uint32_t. As long as the base is a power of 10, it is straightforward to parse your decimal-format input number into such a representation, and no rounding error is introduced by doing so.

  3. Scale the bignum representation by the appropriate power of two to obtain a scaled value between 223 and 224 - 1. This can be done with only integer arithmetic. Do note that it may be possible that you'll have to scale down instead of up. Either way, this scaling can be performed via integer arithmetic alone. Remember which power of two (s) was used as the scale factor (i.e. the base-2 logarithm of the scale factor, not so much the scale factor itself; this can come easily out of the algorithm with no computation of transcendental functions required).

  4. Round to unit precision.

You then have the sign bit from step 1, the bits of the mantissa / significand as the remaining significant bits of the bignum (remember that the most-significant one is implicit, not explicit, in the final IEEE format), and the exponent as 23 - s.

Note, too, that that approach can be extended very naturally to scientific notation.