@jpessin: It's probably not the most efficient option computationally, but perhaps the simplest solution would be to use the Arduino language's map() function.
This just goes to show that I really need to spend some time on the Arduino website looking to see what's there. I'm currently ver yhappy with the solution proposed by Javi (Garcia) earlier in these comments, but it's great to know that this (and other) functions are available.
It's probably not the most efficient option computationally, but perhaps the simplest solution would be to use the Arduino language's map() function. It seems to work going up and going down in color value, and ends on exactly the right value, in the right number of steps.
Your truncation problem would not be there if you stick with fractions.
Just keep 2 integers for each number numerator and denominator) and rewrite your algorithm using 2 numbers instead of one for all calcluations and do multiplications first and divisions last. It's pretty much the same as Fixed Point arithmetics .
Blog That A-Ha Moment Larry Desjardin 11 comments Have you ever had an a-ha moment? Sure, you have. The Merriam-Webster dictionary defines it as "a moment of sudden realization, inspiration, insight, recognition, or ...