How to approximate float function by integer numbers?
显示 更早的评论
I need to spare same space in 2 kB Flash MCU to finish with the program to control servos where x = 0.0° to 90.0°
How to approximate the function float y = x / 90 * 1250 + 3750 by an integer function, preferably using uint16_t
The integer divisor should be a power of 2.
采纳的回答
更多回答(1 个)
General exact process:
This can be done more compactly using bitand() and bitget() and similar, but sometimes it is easier to think in terms of streams of bits.
format long g
R = (1250/90)
U64 = typecast(double(R), 'uint64');
B64 = dec2bin(U64, 64);
numerator = int64(bin2dec(['1', B64(end-51:end)]))
denominator = 2^(510 + 52 - bin2dec(B64(2:11)))
sign = 2 * (B64(1)=='0') - 1
reconstructed = sign * double(numerator) / double(denominator)
R - reconstructed
Approximating with a 16 bit denominator would take more work. Or perhaps less...
1 个评论
format long g
R = (1250/90)
D = 15 - ceil(log2(abs(R)));
denominator = uint16(2^D)
sign = 1; if R < 0; sign = -1; end
numerator = sign * int16(floor(abs(R) * denominator))
reconstructed = double(numerator) / double(denominator)
R - reconstructed
When you look at those, at first it looks as if it would be plausible that you could gain another bit of accuracy by using a numerator one bit different from twice as large as the existing one, so 56888 +/- 1. But if you do that then you lose the room for the numerator to be negative.
This code will not work properly for input values less than 1.
类别
在 帮助中心 和 File Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
