本文整理汇总了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;
}
示例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);
}
示例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;
}
示例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);
}
示例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;
});
示例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;
}