PiMath API
    Preparing search index...

    Class Fraction

    An exact rational number such as a/b. A Fraction is exact: it stores an integer numerator and an integer denominator, and flags a value that had to be approximated (exact is then false). It is permissive like a JavaScript number: an impossible value is NaN or infinite (isNaN(), isFinite()) and does not throw. Operations modify the fraction and return it; clone() keeps the original.

    new Fraction("1/2").add("1/3").display // "5/6"
    

    Implements

    Index
    • get denominator(): number

      Denominator, as stored (it is not reduced automatically). Setting it to zero gives a signed infinity, or an invalid fraction when the numerator is zero too.

      Returns number

    • set denominator(value: number): void

      Parameters

      • value: number

      Returns void

    • get exact(): boolean

      False when the value is an approximation (inexact root, very long decimal, integer part above 2^53). An approximate fraction is displayed as a decimal with 3 digits.

      Returns boolean

    • set exact(value: boolean): void

      Parameters

      • value: boolean

      Returns void

    • Add a value (fraction, number or string) and reduce the result. Modifies and returns the receiver. The result is flagged approximate (exact is false) when an operand is approximate or an integer part exceeds 2^53.

      Parameters

      Returns this

      new Fraction("1/2").add("1/3").display // "5/6"
      
    • Scale numerator and denominator by the same non-zero integer factor.

      Parameters

      • k: number

      Returns this

      if k is not a safe integer, or is zero.

    • Copy the mathematical value of another fraction into this one.

      NOTE: unlike clone, copy intentionally transfers only the mathematical state (#numerator, #denominator, #exact) and NOT the display state (#type, #digits, #withSign). This divergence is by design: parse() routes a Fraction input to copy(), so the receiving fraction keeps its own formatting configuration.

      Parameters

      Returns this

    • True when the fraction is exact and NOT an integer, i.e. it has a non-zero fractional part (e.g. 3/2). Use isRelative to test for an integer. (This was the former behaviour of isRational.)

      Returns boolean

    • True for an even integer. A fraction that is not an integer is neither even nor odd.

      Returns boolean

    • True when the decimal expansion of the exact fraction never stops (1/3, 1/7), that is when the reduced denominator has a prime factor other than 2 and 5. False for an approximate or non finite value.

      Returns boolean

    • True when the fraction is an exact rational number: a finite, exact numerator/denominator. Integers are included (4 = 4/1 is rational). Approximate (non-exact) fractions and ±Infinity / NaN are not rational.

      Returns boolean

    • True when the reduced fraction is the square of a rational (4/9). A negative value is never a square.

      Returns boolean

    • Multiply by a value (fraction, number or string). Modifies and returns the receiver.

      Parameters

      • F: InputValue<Fraction>

        the factor.

      • reduce: boolean = true

        reduce the result (default), or keep the product as it is when false.

      Returns this

      new Fraction("2/3").multiply("3/4").display // "1/2"
      
    • Router: inspect the input type/format and delegate to the matching from*** builder (fromString / fromNumber / fromNumbers), or to copy() for a Fraction input. Does not build the fraction inline.

      Parameters

      Returns this

    • Raise to the power p. The fraction is reduced first; a negative p inverts it. An integer exponent gives an exact result. A fractional exponent is exact only when numerator and denominator are perfect powers ((9/4)^0.5 = 3/2); otherwise the result is approximate. A rejected call leaves the receiver intact.

      Parameters

      Returns this

      if the base is negative and the exponent is not an integer.

    • Divide numerator and denominator by their greatest common divisor, and keep the sign on the numerator.

      Returns this

      new Fraction(6, -4).reduce().display // "-3/2"
      
    • p-th root of the fraction, exact when it is a perfect power, otherwise an approximation (exact becomes false). A rejected root leaves the receiver intact (atomicity).

      Parameters

      • p: number

      Returns this

      if p is not a safe integer.

      if the radicand is negative and p is even (not real).

    • Sign of the fraction, as +1 or -1.

      NOTE (assumed): zero AND NaN are classified as positive (sign() returns 1). Consequently isPositive() and isNatural() are true for zero, while isNegative() is false. A NaN has no real sign; +1 is returned so an invalid fraction is not misclassified as negative.

      Returns -1 | 1

    • Decompose the fraction into its repeating-decimal parts (sign, integer part, non-periodic digits, periodic digits and period length).

      Returns {
          integerPart: string;
          nonPeriodic: string;
          periodic: string;
          periodLength: number;
          sign: -1 | 1;
      }

      if the fraction is non-exact or infinite.

    • Show the value as a decimal with digits digits (exact fractions included), or null for the default display: an exact fraction as a/b, an approximate value with 3 digits.

      Parameters

      • digits: number | null

      Returns this

      if digits is neither null nor an integer from 0 to 100.

    • Build a new fraction from a single number (integer or decimal).

      Parameters

      • value: number

      Returns Fraction

      for exponential notation (e.g. 1e-7), which would break the integer numerator/denominator invariant.

    • Build a new fraction from a numerator and a denominator, without reducing it. A zero denominator gives a signed infinity (3/0), or an invalid fraction for 0/0. Values that are not safe integers are converted through fromNumber and become approximate.

      Parameters

      • numerator: number
      • denominator: number

      Returns Fraction

    • Build a new fraction from a decimal whose last length digits repeat (e.g. fromPeriodic(2.3125, 2) → 11447/4950).

      Parameters

      • value: string | number
      • length: number

      Returns Fraction

      if length is not a positive integer, or exceeds the number of decimal digits.

    • Build a new fraction from a string: an integer, a decimal, "a/b" or a periodic decimal "2.31(25)". A malformed string does not throw: the result is an invalid fraction (see isNaN).

      Parameters

      • value: string

      Returns Fraction