是否存在用于计算或估计二进制整数的位数的公式? [英] Is there a formula for calculating or estimating the number of digits of a binary integer?

查看:78
本文介绍了是否存在用于计算或估计二进制整数的位数的公式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ex:以10为基数(999)=> 3位数字===以2为基数(1111100111)=> 10位数字

ex : base 10(999) => 3 digits === base 2(1111100111) => 10 digits

我当前正在使用表格来完成此操作估算值,但在base10中仍限制为15位数字,因为在JS中,下表仍然是我们被MAX_SAFE_INTEGER阻止的列表(请运行代码段查看)。

I am currently using a table to make this estimate, but it remains limited to 15 digits in base10 because under JS we remain blocked by MAX_SAFE_INTEGER is the following table (run snippet to see it).

如何通过公式扩展此表,直到以10为底的255个数字?

How to extend this table by a formula, until 255 digits en base 10 ?

const TDigits   = document.querySelector('#T-Digits tbody')
,     MaxDigits = Number.MAX_SAFE_INTEGER.toString().length
;

var x9 = '9';

for (let n=1; n < MaxDigits; n++)
{
  let newRow = TDigits.insertRow(-1)

  newRow.insertCell(0).textContent = n
  newRow.insertCell(1).textContent = (x9-0).toString(2).length

  newRow.insertCell(2).textContent = Math.ceil(Math.log2(Math.pow(10,n)))
  
  x9 += '9';
}

table { margin:1em}
table thead { background-color: cadetblue }
table td { text-align: center; padding: .2em .5em; border-bottom: 1px solid grey }

<table id="T-Digits">
  <caption>Max digits corresponding</caption>
  <thead>
    <tr><td>base 10</td> <td>base 2</td><td> log </td></tr>
  </thead>
  <tbody>
      <tr><td>0</td> <td>1</td><td>1</td></tr>
  </tbody>
</table>

Jaromanda X ,;;

推荐答案

for (n = 1; n < 256; n +=1 ) {
    console.log(n, Math.ceil(Math.log2(Math.pow(10,n))));
}

据我所知,这些值匹配

这篇关于是否存在用于计算或估计二进制整数的位数的公式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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