本文整理汇总了TypeScript中@ktn/integer.Integer类的典型用法代码示例。如果您正苦于以下问题:TypeScript Integer类的具体用法?TypeScript Integer怎么用?TypeScript Integer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Integer类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: str
/**
* Convert String to Rational.
* @static
* @method Rational.str
* @param {!string} a ex.'-1/2', '0.1/1.02'.
* @return {!Rational}
*/
public static str(a: string): Rational {
const as: string[] = a.split('/');
as[1] = as[1] || '1';
// sign
const [s1, s2]: boolean[] = [as[0][0] === '-', as[1][0] === '-'];
as[0] = as[0].replace(/[\-\+]/g, '');
as[1] = as[1].replace(/[\-\+]/g, '');
// dot
const [d1, d2]: number[] = [as[0].indexOf('.'), as[1].indexOf('.')];
const [l1, l2]: number[] = [d1 < 0 ? 0 : as[0].length - d1 - 1, d2 < 0 ? 0 : as[1].length - d2 - 1];
as[0] = as[0].replace('.', '').replace(/^0*(\d+)$/, '$1');
as[1] = as[1].replace('.', '').replace(/^0*(\d+)$/, '$1');
if (l1 > l2) {
as[1] = `${as[1]}${'0'.repeat(l1 - l2)}`;
} else if (l1 < l2) {
as[0] = `${as[0]}${'0'.repeat(l2 - l1)}`;
}
// sign
if (s1 !== s2) {
as[0] = `-${as[0]}`;
}
return new Rational(Integer.str(as[0]), Integer.str(as[1]));
}
示例2: cancel
/**
* @static
* @method Rational.cancel
* @param {!Integer} a
* @param {!Integer} b
* @return {!Array<!Integer>}
*/
public static cancel(a: Integer, b: Integer): Integer[] {
const g: Integer = a.gcd(b);
a = a.div(g);
b = b.div(g);
if (!b.sign) {
a.sign = !a.sign;
b.sign = true;
}
return [a, b];
}
示例3: inv
/**
* Multiplicative inverse (or reciprocal)
* @method Rational#inv
* @return {!Rational}
*/
public inv(): Rational {
const n: Integer = this._n;
const d: Integer = this._d;
if (!n.isNonZero()) {
throw new Error('zero division');
}
if (!n.sign) {
return new Rational(d.neg(), n.neg(), true);
}
return new Rational(d, n, true);
}
示例4: any
/**
* Convert anything to Rational.
* @static
* @method Rational.any
* @param {!*} a
* @param {!*=} b
* @throws {Error} ZeroDivisionError
* @return {!Rational}
*/
public static any(a: any, b?: any): Rational {
if (!arguments.length) {
return Rational.zero;
}
if (arguments.length === 1) {
if (a instanceof Rational) { return a.clone(); }
if (typeof a === 'string') { return Rational.str(a); }
return new Rational(Integer.any(a), Integer.one, true);
}
if (!b) {
throw new Error('zero division');
}
if (!a) { return Rational.zero; }
return new Rational(Integer.any(a), Integer.any(b));
}
示例5: num
/**
* Convert Number to Rational.
* @static
* @method Rational.num
* @param {!int} a Numerator
* @param {!int=} b Denominator
* @param {boolean=} c
* @return {!Rational}
*/
public static num(a: int, b?: int, c?: boolean): Rational {
if (!b) {
return new Rational(Integer.num(a), Integer.one, true);
}
return new Rational(Integer.num(a), Integer.num(b), c);
}
示例6: pow
/**
* @method Rational#pow
* @param {!int} b
* @return {!Rational} this ^ b.
*/
public pow(b: int): Rational {
return new Rational(this._n.pow(b), this._d.pow(b), true);
}
示例7: div
/**
* @method Rational#div
* @param {!Rational} b
* @return {!Rational} this / b.
*/
public div(b: Rational): Rational {
return new Rational(this._n.mul(b._d), this._d.mul(b._n));
}
示例8: sub
/**
* @method Rational#sub
* @param {!Rational} b
* @return {!Rational} this - b.
*/
public sub(b: Rational): Rational {
return new Rational(
this._n.mul(b._d).sub(this._d.mul(b._n)),
this._d.mul(b._d));
}
示例9: add
/**
* @method Rational#add
* @param {!Rational} b
* @return {!Rational} this + b.
*/
public add(b: Rational): Rational {
return new Rational(
this._n.mul(b._d).add(this._d.mul(b._n)),
this._d.mul(b._d));
}
示例10: cmp
/**
* @method Rational#cmp
* @param {!Rational} b
* @return {!int}
* 1 (this > b)
* 0 (this = b)
* -1 (this < b).
*/
public cmp(b: Rational): int {
return this._n.mul(b._d).cmp(this._d.mul(b._n));
}