Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在研究一个问题具有自然对数刻度的程序。
到目前为止,我使用的是 base 2,并在此处找到unsigned int log2(uint64_t)了(在 C/C++ 中)的一个很好的实现。
unsigned int log2(uint64_t)
但是,我发现基数 2 对我的问题来说太多了:我需要使用分数基数,例如 3/2。
有人知道这种操作的实现吗?
我目前的解决方案是round(log(x)/log(base))round 返回整数,但我至少在跳跃以避免对日志进行两次评估。
round(log(x)/log(base))
log(base)是一个常数,所以只需评估一次并取其倒数(将其转换为乘法而不是昂贵的除法)。
log(base)
const float k = 1.0f / log(base); // init constant once y = round(log(x) * k); // each evaluation only requires one log, // one multiply and one round