3 ms·
To me, this screams “use a lookup table”. We’re talking, maybe, 100 different “Celsius” values. If there’s no memory for that, do multiplication by 9/5 the way
by Someone 6y ago
To me, this screams “use a lookup table”. We’re talking, maybe, 100 different “Celsius” values.
If there’s no memory for that, do multiplication by 9/5 the way it’s done in Bresenham’s line algorithm (https://en.wikipedia.org/wiki/Bresenham's_line_algorithm) https://en.wikipedia.org/wiki/Bresenham's_line_algorithm). If you start at zero Celsius = 32 Fahrenheit, that’s 50 steps, at most. Might not be (much) smaller than a lookup table, though.
Precomputing the steps for each degree Celsius difference and storing each in a single bit (a one degree change in Celsius will increase degrees Fahrenheit by either 1 or 2) might be the most memory-efficient approach. For a 100 degree Celsius range that’s a 50 bits table (starting at the center of the range). Edit: even that may be too large. That table will repeat every 9 bits, so one byte almost is enough. It might be a struggle to make that a net gain, given the more complex conversion code, though.
- noexani 6y agoIt depends on the application. In this case a lookup table would consume quite a bit more program memory (there isn't any spare), and the division routine is needed for other tasks, so it makes sense to use it for that conversion too. Speed is not an issue.
- noexani 6y agoI just calculated, there are 1800 possible Celsius values. in this application. That would be quite some lookup table.
- dvdkhlng 6y agoYou can very likely compress the table and/or interpolate between entries. It's not like that those entries are totally random.
- AstralStorm 6y agoYou can actually do that as well with the division itself. But you need a kind of fancy interpolation, I forget the name, used for rational functions. Polynomials won't do. Edit: I think this is either Restrictive Pade's approximation or barycentric rational interpolation. Or both combined. Normally instead division is approximated with Newton-Raphson iteration. I think 6 steps is enough for 16 bit.
- fuzzfactor 6y agoIt's a simple linear function, and the delta Y has enough digits to overcome significant rounding error in comparison to measurement error. In this application no rounding is needed either since the difference between rounding and truncation is also not significant. You don't need the computer to do any of your multiplication or store any table. You can start from a sufficiently low reference point, then add delta Y as you increment X. Xref + deltaX = Yref + deltaY