如何在 C 中存储 10 位浮点值

How to store 10 bit float value in C

我有一个雷达装置。来自设备的各种数据,如检测到的物体速度、加速度距离等。但数据是 10 位和 13 位浮点值。如何打印 10 位和 13 位浮点值以及如何存储这些值?作为具有 32 位值的浮点数。我试图将它直接存储在 float 变量中,但它给出了错误的值。

对于 10 位的情况,输入包括: 第 15 位:符号 位 10-14:指数(指数 =(位 10-14)+ 15) 位 0-9:重要 (1.(位 0-9))

像这样的东西应该可以工作,但请注意我还没有测试过它:

int halfFloatToInt16(unsigned int input, int shift)
{

    int exp, output;

    // Load precision: (1.(precision bits 0-9))
    output  = (int)(0x03FF & input);
    output += 0x0400;

    // Apply sign (bit 15)
    output  = (0x8000 & input)?(-output):output;

    // Calculate exponent (bits 10 - 14)
    // Adjustment -25 = -15 for exponent and -10 for significand
    exp  = (int)((0x001F) & (input >> 10));
    exp -= 25;

    // Apply shift to acheive desired fixed point precision
    exp += shift;

    // Shift output
    if(exp > 0)
    {
        return(output << exp);
    }
    else
    {
        return(output >> exp);
    }
}

这里的输入将是 16 位浮点值,如上所述,转换为 unsigned int。班次标识已应用于输出的班次。因此函数的输出将是值乘以 shift 指定的幂的两倍。例如,如果预期的最大输出值为 1,您将使用 14 的移位。因此 16384 代表一。如果输出的最大预期值为 20000,则将位移设为零。这将优化输出的整体精度。