当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript integer.Integer类代码示例

本文整理汇总了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]));
  }
开发者ID:kittttttan,项目名称:ktn-js,代码行数:34,代码来源:rational.ts

示例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];
 }
开发者ID:kittttttan,项目名称:ktn-js,代码行数:17,代码来源:rational.ts

示例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);
 }
开发者ID:kittttttan,项目名称:ktn-js,代码行数:16,代码来源:rational.ts

示例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));
 }
开发者ID:kittttttan,项目名称:ktn-js,代码行数:24,代码来源:rational.ts

示例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);
 }
开发者ID:kittttttan,项目名称:ktn-js,代码行数:15,代码来源:rational.ts

示例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);
 }
开发者ID:kittttttan,项目名称:ktn-js,代码行数:8,代码来源:rational.ts

示例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));
 }
开发者ID:kittttttan,项目名称:ktn-js,代码行数:8,代码来源:rational.ts

示例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));
 }
开发者ID:kittttttan,项目名称:ktn-js,代码行数:10,代码来源:rational.ts

示例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));
 }
开发者ID:kittttttan,项目名称:ktn-js,代码行数:10,代码来源:rational.ts

示例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));
 }
开发者ID:kittttttan,项目名称:ktn-js,代码行数:11,代码来源:rational.ts


注:本文中的@ktn/integer.Integer类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。