c++ - 温度将两个 int 数字转换为 float

标签 c++ c floating-point avr avr-gcc

如何将表示 float 的数字和小数部分的两个无符号整数转换为一个 float 。

我知道有几种方法可以做到这一点,比如将小数部分转换为 float 并将其相乘得到小数并将其加到数字上,但这似乎不是最佳选择。

我正在寻找执行此操作的最佳方法。

/*
 * Get Current Temp in Celecius.
 */
void  GetTemp(){


    int8_t digit = 0;      // Digit Part of Temp
    uint16_t decimal = 0;  // Decimal Part of Temp

    // define variable that will hold temperature digit and decimal part
    therm_read_temperature(&temperature, &decimal); //Gets the current temp and sets the variables to the value

}

我想获取数字和小数部分并将它们转换为浮点类型,使其看起来像 digit.decimal 。

最终可能看起来像这样,但我想找到最优化的解决方案。

/*
 * Get Current Temp in Celecius.
 */
float GetTemp(){


    int8_t digit = 0;      // Digit Part of Temp
    uint16_t decimal = 0;  // Decimal Part of Temp

    // define variable that will hold temperature digit and decimal part
    therm_read_temperature(&temperature, &decimal); //Gets the current temp and sets the variables to the value

    float temp = SomeFunction(digit, decimal);  //This could be a expression also. 

    return temp;

}

////更新///- 7 月 5 日

我能够获得源代码,而不仅仅是利用库。我张贴在这个 GIST DS12B20.c .

    temperature[0]=therm_read_byte();
    temperature[1]=therm_read_byte();
    therm_reset();

    //Store temperature integer digits and decimal digits
    digit=temperature[0]>>4;
    digit|=(temperature[1]&0x7)<<4;
    //Store decimal digits
    decimal=temperature[0]&0xf;
    decimal*=THERM_DECIMAL_STEPS_12BIT;

    *digit_part = digit;
    *decimal_part = decimal;

虽然该函数不会强制我们返回单独的部分作为数字和小数,但从温度传感器读取数据似乎需要这样做(除非我遗漏了一些东西并且它可以作为 float 检索)。

我认为最初的问题仍然是使用这两个部分将其变成 C 中的 float 的最佳方法是什么(这是与 AVR 和 8 位微处理器一起使用,制作优化 key )或能够检索它直接作为一个 float 。

最佳答案

您真正遇到的是使用定点数。这些可以用两种方式表示:作为具有已知幅度或乘数的单个整数(即“十分之一”、“百分之一”、“千分之一”等;示例:来自数字刻度的值的千分之十一克,用 32 位整数保存——除以 10000 得到克),或者作为两个整数,一个保存“累积”或“整数”值,另一个保存“小数”值。

查看 header 。这声明了类型和函数来保存这些定点数,并用它们执行数学运算。例如,当添加小数部分时,您必须担心滚动到下一个整数值,然后您需要增加结果的累加器。通过使用标准函数,您可以利用内置处理器的定点数学功能,例如 AVR、PIC 和 MPS430 微 Controller 中的功能。非常适合温度传感器、GPS 接收器、秤(天平)和其他具有有理数但只有整数寄存器或算术的传感器。

这是一篇关于它的文章:“C 编程语言的定点扩展”,https://sestevenson.wordpress.com/2009/09/10/fixed-point-extensions-to-the-c-programming-language/

引用那篇文章的一部分:

I don’t think the extensions simplify the use of fixed types very much. The programmer still needs to know how many bits are allocated to integer and fractional parts, and how the number and positions of bits may change (during multiplication for example). What the extensions do provide is a way to access the saturation and rounding modes of the processor without writing assembly code. With this level of access, it is possible to write much more efficient C code to handle these operations.

斯科特·霍尔 美国北卡罗来纳州罗利市

关于c++ - 温度将两个 int 数字转换为 float,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10575453/

相关文章:

c++ - 如何创建一个系统来存储和加载数据库中的任何结构?

c++ - float fmodf(float_x, float_y) 函数,math.h,c++

c++ - 除了消除 nullptr 参数的歧义外, std::nullptr_t 还有什么用处?

c - GNU libc.so 如何既是共享对象又是独立可执行文件?

c++ - 为什么这个结构的大小是 24?

c - 什么是 "stack thrash"?

python - int(ceil(x)) 是否表现良好?

C++ 双除法并返回

c++ - 使用 tbb 并行化视频转换程序

c++ - 如何将 char 数组放入 std::string