Author: Ricardo Gibert
Date: 13:05:43 10/31/03
Go up one level in this thread
On October 31, 2003 at 11:03:03, Francesco Di Tolla wrote:
>On October 31, 2003 at 10:06:53, Ricardo Gibert wrote:
>
>>On October 31, 2003 at 09:04:41, Charles Roberson wrote:
>>
>>>
>>> I have a list of numbers that I wanted to normalize to the range of 0-100.
>>> The numbers are integers so the obvious thought is
>>> find largest
>>> foreach number
>>> number = number*100/largest
>>
>>Since the "/largest" does not change within the loop, it can be moved outside
>>the loop. In this way, only 1 division is performed for all the numbers of the
>>set. Acceptable if the set is large. Making this idea work requires some
>>elaboration, but is quite workable.
>
>also 100 does not change in the loop but then you have
>
>number=number
>
>as stament, not very useful
Here is an example of what I had in mind:
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.
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.