Author: Francesco Di Tolla
Date: 02:57:06 11/01/03
Go up one level in this thread
>Assume the "numbers" in question are unsigned and fall in the range 0 to 255 as >an example. Now what we want to do is to map the numbers onto 0 to 100: > > find largest > k = (100<<17)/largest > foreach number > number = (number*k)>>17 > >Where this might fail is if the original range is very much larger than 0 to >255, so that the last statement would either create an overflow problem or an >inaccurate result depending on the selected shift value. This can be remedied by >resorting to 64 bit arithmetic, but I find that inelegant. > >With a little tweaking the above should be able to be made exactly equivalent to >the OP's original algorithm, though perhaps the OP may find that is not >necessary. I don't get the point of using shifts: find largest double k = 100.0/largest foreach number number = some_function(number*k) where you decide if you want somefunction to be round, ceil or floor or no function which means simply cast to int.
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.