bin float precision numbers according to exponent

前端 未结 1 2012
刺人心
刺人心 2021-01-28 06:00

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         


        
1条回答
  •  北荒
    北荒 (楼主)
    2021-01-28 06:46

    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 ) )

    0 讨论(0)
提交回复
热议问题