Olin Lathrop wrote: > A simplifying trick would be to arrange the result value to be fixed point > with 8 fraction bits. Then you don't need to use floating point. Just > multiply each value by 256 times more than in your example, then ignore the > low byte of the result. Using the same 273 --> 87 example: > > SCALE = (115 / 360) * 256 = .3194 * 256 = 81.7778 > > RESULT = HUNDREDS*(SCALE*100) + TENS*(SCALE*10) + UNITS*SCALE > = 2*(8177) + 7*(817) + 3*81 Good approach, but it's better to round the scaling constants appropriately. Furthermore, to get a correctly rounded result, it's also a good idea to add in a constant equal to 1/2 LSB of the result. = 2*(8178) + 7*(818) + 3*82 + 127 = 16356 + 5726 + 246 + 127 = 22456 22456 >> 8 = 87 Note that you would normally use 128 as the rounding offset, but a quick simulation shows that this gives incorrect results in two cases (input values 277 and 349). Note that all three scaling constants are rounded up. Fudging the constant downward slightly gives correct results for all input values. Simulation Perl script: for (0..360) { my $answer = int ($_*115/360 + 0.5); my $u = $_ % 10; $_ = int ($_/10); my $t = $_ % 10; $_ = int ($_/10); my $h = $_ % 10; my $x = int(($h*8178 + $t*818 + $u*82 + 127)/256); if ($x != $answer) { print "$answer => $x\n"; } } -- Dave Tweed -- http://www.piclist.com hint: To leave the PICList mailto:piclist-unsubscribe-request@mitvma.mit.edu