當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript type.int類代碼示例

本文整理匯總了TypeScript中@ktn/type.int的典型用法代碼示例。如果您正苦於以下問題:TypeScript int類的具體用法?TypeScript int怎麽用?TypeScript int使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了int類的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: golomb

  /**
   * @method Encode.golomb
   * @param {!int} n natural number
   * @param {int=} [m=8]
   * @return {!string} Golomb coding
   */
  public static golomb(n: int, m: int = 8): string {
    if (n < 0) { throw new Error(`golomb(n, m): n=${n} must >= 0`); }

    m = (m | 0) || 8;
    if (m < 1) { throw new Error(`golomb(n, m): m=${m} must >= 1`); }

    const q: int = n / m | 0;
    const r: int = n % m;
    const bin: string = m.toString(2);
    let b: int = bin.length - 1;
    const isBin: boolean = (b !== 1) && !(b & (b - 1));

    let res: string = Encode.unary(q, true);
    if (isBin) {
      res += ('0'.repeat(b) + r.toString(2)).slice(-b);
    } else {
      ++b;
      if (r < (1 << b) - m) {
        res += ('0'.repeat(b - 1) + r.toString(2)).slice(-(b - 1));
      } else {
        res += ('0'.repeat(b) + (r + (1 << b) - m).toString(2)).slice(-b);
      }
    }
    // console.log('m=%d, b=%d (bin=%s), 2^b-m=%d, r=%d',
    //    m, b, (isBin?'true':'false'), (1<<b)-m, r);

    return res;
  }
開發者ID:kittttttan,項目名稱:ktn-js,代碼行數:34,代碼來源:enc.ts

示例2: eliasDelta

  /**
   * @method Encode.eliasDelta
   * @param {!int} n natural number
   * @return {!string} Elias delta coding
   */
  public static eliasDelta(n: int): string {
    if (n < 1) { throw new Error(`eliasDelta(n): n=${n} must >= 1`); }

    const bin: string = n.toString(2);
    const gamma: string = Encode.eliasGamma(bin.length);
    return gamma + bin.substring(1);
  }
開發者ID:kittttttan,項目名稱:ktn-js,代碼行數:12,代碼來源:enc.ts

示例3: eliasOmega

  /**
   * @method Encode.eliasOmega
   * @param {!int} n natural number
   * @return {!string} Elias omega coding
   */
  public static eliasOmega(n: int): string {
    if (n < 1) { throw new Error(`eliasOmega(n): n=${n} must >= 1`); }

    let res: string = '0';
    while (n > 1) {
      const bin: string = n.toString(2);
      res = bin + res;
      n = bin.length - 1;
    }

    return res;
  }
開發者ID:kittttttan,項目名稱:ktn-js,代碼行數:17,代碼來源:enc.ts

示例4: hex

/**
 * @private
 * @param {int} n
 * @param {int} len
 * @return {string}
 */
function hex(n: int, len: int): string {
  let z: string;

  switch (len) {
  case 4: z = '0000'; break;
  case 8: z = '00000000'; break;
  case 12: z = '000000000000'; break;
  case 16: z = '0000000000000000'; break;
  default: throw new Error(`invalid value: ${len}`);
  }

  return (z + n.toString(16)).slice(-len);
}
開發者ID:kittttttan,項目名稱:ktn-js,代碼行數:19,代碼來源:uuid.ts

示例5: if

     (src: string, flag: string, width: int, prec: string, type: string): string => {
   if (type === '%') {
     return '%';
   }
   let s: string = '';
   let n: int = 0;
   if (type === 's') {
     s = argv[index];
   } else if (type === 'd') {
     n = argv[index] | 0;
     s = (flag === '+' && n > 0 ? '+' : '') + n;
   } else if (type === 'o') {
     n = argv[index] | 0;
     s = (flag === '+' && n > 0 ? '+' : '')
       + (flag === '#' ? '0' : '') + n.toString(8);
   } else if (type === 'x') {
     n = argv[index] | 0;
     s = (flag === '+' && n > 0 ? '+' : '')
       + (flag === '#' ? '0x' : '') + n.toString(16);
   } else if (type === 'e') {
     s = (flag === '+' && argv[index] > 0 ? '+' : '')
       + (prec ? argv[index].toExponential(prec) : argv[index].toString());
   } else if (type === 'f') {
     s = (flag === '+' && argv[index] > 0 ? '+' : '')
       + (prec ? argv[index].toFixed(prec) : argv[index].toString());
   }
   ++index;
   if (width > s.length) {
     if (flag === '-') {
       s += (flag === '0' ? '0' : ' ').repeat(width - s.length);
       return s;
     }
     s = (flag === '0' ? '0' : ' ').repeat(width) + s;
     return s.slice(-width);
   }
   return s;
 });
開發者ID:kittttttan,項目名稱:ktn-js,代碼行數:37,代碼來源:string.ts

示例6: eliasGamma

  /**
   * @method Encode.eliasGamma
   * @param {!int} n natural number
   * @return {!string} Elias gamma coding
   */
  public static eliasGamma(n: int): string {
    if (n < 1) { throw new Error(`eliasGamma(n): n=${n} must >= 1`); }

    const bin: string = n.toString(2);
    return '0'.repeat(bin.length - 1) + bin;
  }
開發者ID:kittttttan,項目名稱:ktn-js,代碼行數:11,代碼來源:enc.ts


注:本文中的@ktn/type.int類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。