How to split fixed point number by small floating point number?

I need to split the 5U11 number set by 6.02and prefer to do it without casting for float and back.

5U11 means a 16-bit unsigned number, with the 11 least significant bits representing the fractional part.

How do I imagine 6.02and what will be the top error connection of one calculation?

+3
source share
2 answers

Simply scaling to 100 is enough.

uns16_t x_5U11;
uns32_t acc;
acc = x_5U11;
acc *= 100;
acc += 301; // for round to nearest rather than truncation.
acc /= 602;

Error: 1/2 LSbit in x_5U11.

-

If speed is more important than doing multiple and division (by switching), as @alastai suggests, this is the way to go. With proper rounding, the answer should be within +/- 1 LSBit.

, +/- 1/2 LSbit ( ).

[Edit] @Ingo Leonhardt, , .

+2

- 6.02 16- ; .. (2 ^ 16/6.02) = 0x2a86. , , ; (2 ^ 18/6.02) = 0xaa1a.

5U11 16x16 32- , ( ) 18 , , 5U11.

:

14.3562 * (2^18 / 6.02) = 625148.122 / 2^18 = 2.384
0x72d9  * 0xaa1a        = 0x4c4fc40a >> 18  = 0x1313

, - , (. . ).

, , 2 ^ 18, .


, d/2, d - ( 2 ^ 18, 2 ^ 17 0x20000.

, , . , 0x20000, x = 0xfa19:

0xfa19 * 0xaa1a + 0x20000 = 0xa62e008a >> 18 = 0x298c

31.2622 / 6.02 = 5.193058

0x298c * 2^-11 = 5.193359

0,000302 0,62 .

, ; , , ( 0xaa1a) . , -, 0x1c200, 0,56 LSB.

+2

All Articles