手动计算对数 [英] Calculate logarithm by hand

查看:309
本文介绍了手动计算对数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想手动计算):

  10的基数2对数是3.3219280949 

基数15的对数为1.6826061945

...






不过 - 我不想使用已实现的函数调用,如 Math.ceil,Math.log,Math.abs,... ,因为我想要一个干净的原生解决方案,只需dea ls与 + - * / 和一些循环。



这是我到目前为止的代码:



  function myLog(base,x){let result = 0;做{x / = base;结果++; } while(x> = base)return result;} let x = 10,base = 2; let result = myLog(base,x)console.log(result) 



但上面的方法似乎不是计算对数到N的正确方法 - 所以任何帮助如何修复此代码将非常感激。



感谢一百万提前的jonas。

解决方案

你可以使用递归方法:

  const log =(base,n,depth = 20,curr = 64,precision = curr / 2) => 
深度< = 0 || base ** curr === n
? curr
:log(base,n,depth - 1,base ** curr> n?curr - precision:curr + precision,precision / 2);

可用作:

  log(2,4)// 2 
log(2,10)// 3.32196044921875

您可以通过更改深度来影响精度,并且您可以使用更改接受值的范围(当前为~180) curr






工作原理:



如果我们已达到所需深度或已找到准确值:

  depth< = 0 || base ** curr === n 

然后它只返回 curr 已完成。否则,它检查我们想要找到的对数是低于还是高于当前对数:

  base ** curr> n 

然后它会继续通过
递减搜索值1)降低深度按一个
2)增加/减少 curr 按当前精度
3)降低精度






如果你讨厌函数编程,这里有一个命令式版本:

 函数log(base,n,depth = 20){
let curr = 64,precision = curr / 2;
while(depth - > 0&& base ** curr!== n){
if(base ** curr> n){
curr - = precision;
} else {
curr + = precision;
}
precision / = 2;
}
return curr;
}

顺便说一句,我使用的算法叫做对数搜索通常称为二分搜索。


I'd like to calculate the mathematical logarithm "by hand"...

... where stands for the logarithmBase and stands for the value.


Some examples (See Log calculator):

The base 2 logarithm of 10 is 3.3219280949

The base 5 logarithm of 15 is 1.6826061945

...


Hoever - I do not want to use a already implemented function call like Math.ceil, Math.log, Math.abs, ..., because I want a clean native solution that just deals with +-*/ and some loops.

This is the code I got so far:

function myLog(base, x)  {
  let result = 0;
  do {
    x /= base;
    result ++;
  } while (x >= base)
  return result;
}

let x = 10,
    base = 2;

let result = myLog(base, x)
console.log(result)

But it doesn't seems like the above method is the right way to calculate the logarithm to base N - so any help how to fix this code would be really appreciated.

Thanks a million in advance jonas.

解决方案

You could use a recursive approach:

 const log = (base, n, depth = 20, curr = 64, precision = curr / 2) => 
   depth  <= 0 || base ** curr === n 
      ? curr
      : log(base, n, depth - 1, base ** curr > n ? curr - precision : curr + precision, precision / 2);

Usable as:

log(2, 4) // 2
log(2, 10) // 3.32196044921875

You can influence the precision by changing depth, and you can change the range of accepted values (currently ~180) with curr


How it works:

If we already reached the wanted depth or if we already found an accurate value:

 depth  <= 0 || base ** curr === n 

Then it just returns curr and is done. Otherwise it checks if the logarithm we want to find is lower or higher than the current one:

         base ** curr > n

It will then continue searching for a value recursively by 1) lowering depth by one 2) increasing / decreasing curr by the current precision 3) lower precision


If you hate functional programming, here is an imperative version:

  function log(base, n, depth = 20) {
   let curr = 64, precision = curr / 2;
   while(depth-- > 0 && base ** curr !== n) {
     if(base ** curr > n) {
       curr -= precision;
     } else {
       curr += precision;
     }
     precision /= 2;
    }
    return curr;
  }

By the way, the algorithm i used is called "logarithmic search" commonly known as "binary search".

这篇关于手动计算对数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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