bin根据指数浮点精度数字 [英] bin float precision numbers according to exponent

查看:151
本文介绍了bin根据指数浮点精度数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是一个很好的方法来浮动精度数字?
这样的东西,但也许更有效率?

  x = 1; 
for i = 0,size-1 {//循环遍历数目b $ b $ if(value> x){
++ bin [i];返回;
}
x * = 0.1;
}
++ bin [size-1]; //增加最后的分数

我想直接得到指数, frexp ,并使用它。
是否值得?

解决方案

我会回答这个问题,我想这可能对您有帮助:



给定一个数字x,当用科学记数法写x时指数是多少,例如,如果x是.007,指数是-3(7x10 ^ -3) b
$ b因此,x = a * 10 ^ b,其中1< = a< 10,我们解决b。



让我们来看双方的日志(基数10)吧!

$ ul $ b

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

    现在b是一个整数,并且0 <= log(a)< 1,所以log(a)实际上只是log(x)的小数部分。所以,我们可以通过舍弃log(x)来减小小数部分。


    • b = floor(log(x) li>


    当x = .007时,b = floor(-2.15)= -3如预期的那样

    我认为C代码是 int b =(int)floor(log10(x))

    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?

    解决方案

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

    这篇关于bin根据指数浮点精度数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆