views:

25

answers:

1

hello.

What is a good way to bin float precision numbers? something like this, but perhaps more efficient?

x = 1;
for i = 0,size-1 {  // loop over number of bins
    if (value > x) {
        ++bin[i]; return;
    }
    x *= 0.1;
}
++bin[size-1]; // increment last bins

I have thought of getting exponent directly, frexp, and using that. Is it worthwhile?

+1  A: 

I'll answer this question, I think it might help you:

"Given a number x, what is the exponent when x is written in scientific notation. For example, if x is .007, the exponent is -3 (7x10^-3)."

So, x = a * 10^b, with 1 <= a < 10. We solve for b.

Let's take the log (base 10) of both sides

  • log(x) = log(a * 10^b)
  • log(x) = log(a) + log(10^b)
  • log(x) = log(a) + b
  • b = log(x) - log(a)

Now b is an integer, and 0 <= log(a) < 1, so log(a) is really just the fractional part of log(x). So, we can just drop the fractional part by rounding log(x) down.

  • b = floor(log(x))

When x = .007, b = floor(-2.15) = -3 as expected

I think the C code would be int b = (int) floor( log10( x ) )

Tom Sirgedas
this is so obvious, I feel somewhat stupid for even asking
aaa