2.96 Float f2i

★★★★

Problem:

Following the bit-level floating-point coding rules, implement the function with the following prototype:

/* 
 * Compute (int) f
 * If conversion casues overflow or f is NaN, return 0x80000000
 */
int float_f2i(float_bits f);

For floating-point number f, this function computes (int) f. Your function should round toward zero. If f cannot be represented as an integer, then the function should return 0x80000000.

Test your function by evaluating it for all 2^32 values of argument f and comparing the result to what would be obtained using your machine's floating-point operations.

Code:

#include <stdio.h>
#include <assert.h>

typedef unsigned float_bits;

float u2f(unsigned u) {
    return *(float *)&u;
}

int float_f2i(float_bits f) {
    unsigned sign = f >> 31;
    unsigned exp = f >> 23 & 0xFF;
    unsigned frac = f & 0x7FFFFF;
    unsigned bias = 0x7F;

    int result;
    unsigned E;
    unsigned M;

    if (exp < bias) {
        /* the float number is less than 1 */
        result = 0;
    } else if (exp >= 31 + bias) {
        /* overflow */
        result = 0x80000000;
    } else {
        /* normal */
        E = exp - bias;
        M = frac | 0x800000;
        if (E > 23) {
            result = M << (E - 23);
        } else {
            /* round to zero */
            result = M >> (23 - E);
        }
    }
    return sign ? -result : result;
}

int main() {
    assert(float_f2i(0x98765432) == (int)u2f(0x98765432));
    assert(float_f2i(0x19802385) == (int)u2f(0x19802385));
    assert(float_f2i(0x78000087) == 0x80000000);
    assert(float_f2i(0x8F088888) == (int)u2f(0x8F088888));
    return 0;
}

Last updated