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

时间:2017-07-28 11:58:05

标签: c visual-studio floating-point precision type-conversion

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

1 个答案:

答案 0 :(得分:2)

对于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,则将移位设为零。这将优化输出的整体精度。