本文整理匯總了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;
}