-2

I just started learning floating point and get to know the SME stuff. I'm still very confused about the mantissa... Can somebody explain to me how can I get the exp part of the float. I am sorry if that's a super stupid and basic question but I am having a hard time understanding it...

Also how do I implement the following function... clearly my implementation is wrong. But how do I do it?

// Extract the 8-bit exponent field of single precision
// floating point number f and return it as an unsigned byte
unsigned char get_exponent_field(float f)
{
// TODO: Your code here.
int bias = 127;
int expp = (int)f;
unsigned char E = expp-bias;

return E;
}
Hanming Zeng
  • 337
  • 1
  • 4
  • 12
  • 2
    Did you search? Any of the "Related" links (right hand side) help? Such as [How to get the sign, mantissa and exponent of a floating point number](https://stackoverflow.com/questions/15685181/how-to-get-the-sign-mantissa-and-exponent-of-a-floating-point-number). – kaylum Feb 26 '16 at 03:52
  • The exponent bits in the a `float` are bit 1 to 8 (0-th bit is for sign), Use bit-shift and bit mask to get it. – Ian Feb 26 '16 at 03:56

2 Answers2

1

If you want to extract the IEEE-754 single precision exponent from a float value (in excess 127 notation), you can use the float functions, or you can use a simple union with a shift and mask to do the same:

unsigned float_getexp (float f)
{
    union {
        unsigned u;
        float f;
    } uf;
    uf.f = f;
    return (uf.u >> 23) & 0xff;
}

If you want the actual exponent bias (i.e. the number of places the mantissa decimal is shifted during normalization prior to hidden bit removal), just subtract 127 from the value returned, or if you want that value returned, subtract it before the return.

Give it a try and let me know if you have questions. (note: the type should be unsigned for your exponent, instead of the int you have).

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • Hi can you explain how does this uf.u get value assigned and what does the & 0xff means? – Steve Deng Zishi Feb 28 '17 at 01:49
  • Sure. All you are looking at is a function `float_getexp`. Inside the function the first variable is a `union` named `uf` (with an `unsigned` and `float` -- both 32-bits on most x86 hardware). Immediately after the `union` named `uf` is declared, the union is assigned the value (passed as a parameter to the function) in `uf.f = f;` (that's standard *dot operator* `struct/union` member referencing, e.g. `uf.f` references the float member of the union). Then the `unsigned` member `uf.u` is right-shifted 23 bits and the 2-bytes that represent the exponent are returned. – David C. Rankin Feb 28 '17 at 14:16
0

First, get your floating-point number and calculate its binary form by converting both the integral and fractional parts separately. Once you've got that, say you've got 11010.101(base-2). Normalize the binary string: 1.1010101 x 2^4. Next, add your excess value, say excess 15, to the exponent of the sci. not. value, which would give you 19(base-ten). Convert this to base-two; this will be your exponent.

This is just the structure of the operation, plug in your own bias, etc.

Adrian M.
  • 360
  • 7
  • 17