0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

0.5^n 表

Last updated at Posted at 2025-02-04

循環小数(2進)にならない浮動小数点用数値、固定小数点の精度(ビット幅)決定の参考値、などに。

See the Pen 0.5^n 表 by Ikiuo (@ikiuo) on CodePen.

<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta charset="utf-8">
    <title>2^-n表</title>
    <style>
     table {
         border: solid 1px #e2e2e2;
         border-collaspe: collaspe;
         border-spacing: 0;
     }
     th, td {
         border: solid 1px #e2e2e2;
     }
     th { background: white; }
     td { padding: 2px 2px 2px 4px; }
     .eline { background: #edefef; }
     .oline { background: #f5f6f6; }
    </style>
  </head>
  <body>

    <script>

     function toClipboard(text) {
         navigator.clipboard.writeText(text).then(() => {
             // success
         }, () => {
             // failed
         });
     }

     function pow5n(n) {
         var a = 1n;
         var b = 5n;
         while (n) {
             if (n & 1)
                 a *= b;
             b *= b;
             n >>= 1;
         }
         return a;
     }

     function expstr(n) {
         const v = '' + pow5n(n);
         const s = v.length >= 9 ? v.slice(0, 3) + '⋅⋅⋅' + v.slice(-3) : v;
         return `10<sup>-${n - v.length}</sup> × 0.${s}`;
     }

     function fullstr(n) {
         const v = '' + pow5n(n);
         return '0'.repeat(n - v.length) + v;
     }

     window.onload = (() => {
         document.body.innerHTML = [
             '<table>',
             // '<tr><th colspan="3" style="font-size: xx-large;">0.5<sup>n</sup> 表</th></tr>',
             '<tr><th>n</th><th>指数形式(途中省略)</th><th>2<sup>-n</sup> 全桁</th></tr>',
             [...Array(64)].map((_, i) => {
                 const s = '0.' + fullstr(i+1);
                 return [
                     `<tr class="${i & 1 ? 'oline' : 'eline'}">`,
                     `<td style="text-align: right;">${i+1}<sup>&nbsp;</sup></td>`,
                     `<td onclick="toClipboard('${s}')">${expstr(i+1)}</td>`,
                     `<td onclick="toClipboard('${s}')">${s}</td>`,
                     '</tr>',
                 ].join('');
             }).join(''),
             '</table>',
         ].join('');
     });

    </script>
  </body>
</html>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?