I have unsigned 64bit number, representing mantissa, or fraction (which represent range from [0..1)
, where 0.0
maps to 0
and 0xffffff..
maps to a number "just before 1.0")
Now i want to split this range into equal buckets
- and to answer - given random number key
, to which part of the range it will fall to?
Its easier to get from following code:
func BucketIndex(key, buckets uint64) uint64 {
return uint64(float64(key) / ((math.Pow(2, 64) / float64(buckets)))
}
My attempt to "hack this over" - was to split 2^64 to two, like if I will reduce range to 32bit, and operate in 64bit in order to conduct math:
// ~=key / ((1 << 64) / buckets)
return ((key >> 32) * buckets) >> 32
but ranges stopped to be equal..
eg one third (buckets==3
) will be at 0x5555555600000000
, instead of being at 0x5555555555555556
thats sad story, so im asking do you know of a better methods of finding (1 << 64) / buckets
?