Converting IEEE 754 Float to MIL-STD-1750A Float

2019-06-28 03:31发布

问题:

I am trying to convert a IEEE 754 32 bit single precision floating point value (standard c float variable) to an unsigned long variable in the format of MIL-STD-1750A. I have included the specification for both IEEE 754 and MIL-STD-1750A at the bottom of the post. Right now, I am having issues in my code with converting the exponent. I also see issues with converting the mantissa, but I haven't gotten to fixing those yet. I am using the examples listed in Table 3 in the link above to confirm if my program is converting properly. Some of those examples do not make sense to me.

  1. How can these two examples have the same exponent?

    .5 x 2^0 (0100 0000 0000 0000 0000 0000 0000 0000) 
    -1 x 2^0 (1000 0000 0000 0000 0000 0000 0000 0000)
    

    .5 x 2^0 has one decimal place, and -1 has no decimal places, so the value for .5 x 2^0 should be

    .5 x 2^0 (0100 0000 0000 0000 0000 0000 0000 0010)
    

    right? (0010 instead of 0001, because 1750A uses plus 1 bias)

  2. How can the last example use all 32 bits and the first bit be 1, indicating a negative value?

    0.7500001x2^4 (1001 1111 1111 1111 1111 1111 0000 0100)
    
  3. I can see that a value with a 127 exponent should be 7F (0111 1111) but what about a value with a negative 127 exponent? Would it be 81 (1000 0001)? If so, is it because that is the two's complement +1 of 127?

Thank you

回答1:

1) How can these two examples have the same exponent?

As I understand it, the sign and mantissa effectively define a 2's-complement value in the range [-1.0,1.0).

Of course, this leads to redundant representations (0.125*21 = 0.25*20, etc.) So a canonical normalized representation is chosen, by disallowing mantissa values in the range [-0.5,0.5).

So in your two examples, both -1.0 and 0.5 fall into the "allowed" mantissa range, so they both share the same exponent value.

2) How can the last example use all 32 bits and the first bit be 1, indicating a negative value?

That doesn't look right to me; how did you obtain that representation?

3) What about a value with a negative 127 exponent? Would it be 81 (1000 0001)?

I believe so.



回答2:

  1. Remember the fraction is a "signed fraction". The signed values are stored in 2's complement format. So think of the zeros as ones.
    Thus the number can be written as -0.111111111111111111111 (base 2) x 2^0 , which is close to one (converges to 1.0 if my math is correct)

  2. On the last example, there is a negative sign in the original document (-0.7500001x2^4)