當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。