Display option: .tex uses \dfrac instead of \frac. Chainable, persistent, kept by clone.
Display option: .tex uses \frac (default). Chainable, persistent, kept by clone.
Display option: .tex uses \tfrac instead of \frac. Chainable, persistent, kept by clone.
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.
ASCIIMath rendering of the current state (terminal output, not chainable).
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.
Numerator, as stored (it carries the sign unless the denominator is negative).
LaTeX rendering of the current state (terminal output, not chainable).
Decimal value (numerator / denominator). Negative zero is returned as 0.
Display option: cancel withSign. Chainable.
Display option: show a + before a positive value (zero included). Chainable, persistent, kept by clone.
Cancel it with withoutSign.
Replace the fraction by its absolute value.
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.
Compare this fraction with another value using the given relational sign.
the value to compare against.
Optionalsign: CompareSign
relation to test (default '='); accepts =, <, <=, >, >= and their aliases (leq, geq, neq, same, =<, =>, <>).
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.
Divide by another value. Total operation (see the "IExpression totality" convention): dividing by zero does not throw but yields a signed infinity, preserving the dividend's sign (e.g. (-3) / 0 → -∞).
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.)
Turn the receiver into +∞. Use opposite afterwards for -∞.
Turn the receiver into an invalid fraction (NaN). Test it with isNaN.
Swap numerator and denominator, keeping the sign. Total operation, like divide: the inverse of zero is +∞ (1 / 0), not an error.
True when both values are equal. Fractions are compared by cross product: 2/4 equals 1/2.
A NaN is equal to nothing.
True for an even integer. A fraction that is not an integer is neither even nor odd.
True unless the fraction is infinite or invalid.
True when the receiver is greater than or equal to than.
True when the receiver is strictly greater than than.
True for +∞ and -∞.
True when the receiver is less than or equal to than.
True when the receiver is strictly less than than.
True for an invalid fraction (0/0, a malformed string, a failed parse).
True for a non-negative integer (zero included).
True when the value is strictly negative. Zero and NaN are not negative (see sign).
True when the numerator is not zero.
True for an odd integer, negative ones included.
True for the literal 1/1. A non-reduced 2/2 is not "one": call reduce first.
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.
True when the value is positive or zero (see sign).
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.
True when numerator and denominator have no common divisor other than 1.
True when the fraction represents a relative integer, i.e. its reduced denominator equals 1 (e.g. 6/3 = 2). See hasFractionalPart for the exact non-integer case.
True when the reduced fraction is the square of a rational (4/9). A negative value is never a square.
True when the value is below zero.
True when the value is above zero.
True for 1 and -1, as integers.
True when the numerator is zero.
Multiply by a value (fraction, number or string). Modifies and returns the receiver.
the factor.
reduce the result (default), or keep the product as it is when false.
Set the value to 1. The display options are kept.
Change the sign of the fraction (- times the numerator).
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.
Optionaldenominator: numberRaise 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.
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.
Subtract a value (fraction, number or string) and reduce the result. Modifies and returns the receiver.
Set the value to 0. The display options are kept.
StaticareTrue when every given fraction has the same value as the first one (2/4 equals 1/2).
StaticaverageArithmetic mean of the values, as a new fraction. Without any value the result is +∞.
StaticfromBuild a new fraction from a single number (integer or decimal).
StaticfromBuild 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.
StaticfromBuild a new fraction from a decimal whose last length digits repeat
(e.g. fromPeriodic(2.3125, 2) → 11447/4950).
StaticfromStaticisTrue when new Fraction(value) understands the value: a Fraction, a number, "a/b", a decimal string,
a periodic notation such as "2.31(25)", or an empty string (zero).
StaticmaxLargest of the values, as a new fraction.
StaticminSmallest of the values, as a new fraction.
StaticsortSort by value, ascending (descending when reverse is true). Returns a new array; the Fraction
instances of the input are reused, not cloned.
Optionalreverse: booleanStatictoNew fractions sharing the least common denominator, without reducing them.
StaticuniqueRemove the duplicates by value (2/4 and 1/2 are the same). Keeps the first occurrence, as a clone.
StaticxProduct of the values, as a new fraction that is not reduced (call reduce to simplify it).
An exact rational number such as
a/b. AFractionis exact: it stores an integer numerator and an integer denominator, and flags a value that had to be approximated (exactis thenfalse). It is permissive like a JavaScript number: an impossible value isNaNor infinite (isNaN(),isFinite()) and does not throw. Operations modify the fraction and return it;clone()keeps the original.Example