Fixed-Point-Math - Is the conversion from a safe float to always give the same result?

I use libfixmath for a simulation that must run on two devices (iOS / Android) at the same time and be absolutely accurate.

Modeling should be accompanied by some initial parameters of the float. I am wondering if it is safe to use float and then convert them to fix16_t as follows (function from the library) or do I need to load the simulation with fix16_t values ​​even more?

Therefore, is it possible that two different devices calculate different results with the same input for the function below, due to inaccuracy with a floating point?

typedef int32_t fix16_t;
static const fix16_t fix16_one = 0x00010000; /*!< fix16_t value of 1 */

static inline fix16_t fix16_from_float(float a)
{
    float temp = a * fix16_one; 

    // rounding
    temp += (temp >= 0) ? 0.5f : -0.5f;
    return (fix16_t)temp;
}
+4
1

, :

  • IEEE-754 float

  • a ""

, , , , a 0,5 & times; 2 & minus; 16.

() 2 ( 2 16) , ( 2). .

++ ​​ 0, .

0,5 temp .

temp .

temp 2 23 , , , .

temp 1,0, , . , . , 0, 1; temp 0,5, 1,0, 0. temp 0,5, 1,0, 1.

temp 0,5, . , temp 0,5 & plusmn; 2 & 25 . 1.0 & minus; 2 & minus; 25 float IEEE-754. , ULP. , , .

IEEE-754 " " , , 0 . 0,5 & minus; 2 & minus; 25 + 0.5 1.0, 1. , , , , std::fesetround.

.

+2

Source: https://habr.com/ru/post/1624139/


All Articles