Rational
Represents an exact rational value as a bigint numerator and denominator.
Core rational arithmetic is exact: decimal and scientific-notation input is converted to
the exact fraction represented by the input text rather than stored as a floating-point
approximation. The notable exception is non-integer Rational.pow, which uses
decimal.js numerical exponentiation. The Rational.asDecimal flag records
presentation intent only; it does not change the underlying rational representation.
Values produced by Rational.create and the arithmetic methods are normally reduced
and use a positive denominator. The public constructor is lower level: it stores its two
bigint arguments exactly as supplied and does not reduce the fraction, normalize the
denominator sign, or reject a zero denominator. Sign, comparison, and integer helpers assume
the normal representation, so callers constructing values directly are responsible for
preserving that representation.
Rational instances are mutable because their representation fields are public and
Rational.updateValue updates the instance in place. The ordinary arithmetic methods
return new values and do not mutate their operands.
Members
abs(): RationalMethodReturns the absolute value without modifying this rational.
Returns
Rational — A copied rational whose numerator is nonnegative.
allNegative(...args: Rational[]): booleanMethodTests whether every supplied rational is strictly negative.
Parameters
| Name | Type | Description |
|---|---|---|
args | Rational[] | Rational values to test. |
Returns
boolean — true when each value has sign -1.
asDecimal: booleanPropertyWhether text output should preserve decimal presentation by default. This flag does not change the exact numerator/denominator representation. Most rational arithmetic propagates decimal intent when either operand has it set.
Rational(a: bigint, b: bigint): RationalConstructorCreates a rational from raw numerator and denominator components.
This constructor performs no normalization or validation. Prefer Rational.create for user input and for values that must use Nerdamer's normal reduced-fraction representation.
Parameters
| Name | Type | Description |
|---|---|---|
a | bigint | Numerator to store. |
b | bigint | Denominator to store. |
Returns
copy(): RationalMethodCreates a distinct copy of this rational.
Returns
Rational — A new object with the same numeric representation and presentation metadata.
create(value: string | bigint): RationalMethodCreates a normalized rational from an integer, fraction, decimal, or scientific-notation value.
Fraction strings are reduced and a negative denominator is moved to the numerator. Decimal and scientific-notation strings are converted to the exact rational represented by their finite decimal text. Those forms also set Rational.asDecimal, so later text output normally remains decimal even though the stored arithmetic stays exact.
DivisionByZeroError Thrown when a fraction string has a zero denominator.
Parameters
| Name | Type | Description |
|---|---|---|
value | string | bigint | Integer bigint or numeric string to convert. |
Returns
Rational — A new normalized Rational.
Examples
Rational.create('2/4').text(); // "1/2"
Rational.create('0.125').text(); // "0.125"
Rational.create('4e1').text(); // "40.0"dataType: "RAT"PropertyMarker used by Nerdamer's runtime type guards.
denominator: bigintPropertyDenominator of the exact rational representation.
div(num: Expression): ExpressionMethodDivides using Nerdamer's rational or expression arithmetic overloads.
Rational and string operands compute exact rational division and return a new Rational.
Expression operands promote this rational to an Expression and preserve operand
direction, returning this / expression.
DivisionByZeroError Thrown when exact rational division requires inversion of zero.
Parameters
| Name | Type | Description |
|---|---|---|
num | Expression | Value participating in the division. |
Returns
Expression — A rational result for rational/string input, or an Expression for expression input.
E: RationalPropertyFinite-precision rational approximation of Euler's number at the current configured precision. Recomputed when Rational.set changes the precision.
eq(num: Expression): booleanMethodCompares this rational with another value for mathematical equality.
Rational/string comparisons are exact and use cross multiplication, so decimal-presentation metadata does not affect equality. Expression input delegates to Nerdamer's symbolic comparison semantics.
Parameters
| Name | Type | Description |
|---|---|---|
num | Expression | Value to compare with this rational. |
Returns
boolean — true when the two values compare equal.
evenDenominator(): booleanMethodTests whether the stored denominator is even.
Returns
boolean — true when Rational.denominator is divisible by two.
evenNumerator(): booleanMethodTests whether the stored numerator is even.
Returns
boolean — true when Rational.numerator is divisible by two.
Computes the rational greatest common divisor with another value.
Parameters
| Name | Type | Description |
|---|---|---|
num | string | Rational | Rational or numeric string to combine with this value. |
Returns
Rational — A new reduced rational GCD. Decimal presentation is preserved when either operand
was marked for decimal output.
Computes the rational greatest common divisor across the supplied values.
The operation is folded pairwise and preserves decimal presentation intent when it is present on an operand.
Parameters
| Name | Type | Description |
|---|---|---|
args | Rational[] | Rational values whose common divisor should be computed. |
Returns
Rational — The pairwise rational GCD.
getPrecision(): numberMethodReturns the currently configured rational/Decimal precision setting.
Returns
number — The configured precision value.
gt(num: Expression): booleanMethodTests whether this rational is greater than another value.
Rational/string comparisons are exact. Expression input delegates to Nerdamer's symbolic ordering rules, including their restrictions on unordered complex values.
Parameters
| Name | Type | Description |
|---|---|---|
num | Expression | Value to compare against. |
Returns
boolean — true when this rational is greater than num.
gte(num: string | Rational): booleanMethodTests whether this rational is greater than or equal to another rational value.
Parameters
| Name | Type | Description |
|---|---|---|
num | string | Rational | Rational or numeric string to compare against. |
Returns
boolean — true when this rational is greater than or equal to num.
Returns a value unchanged for compatibility with generic numeric hooks.
Parameters
| Name | Type | Description |
|---|---|---|
value | string | bigint | Rational | Value to pass through. |
Returns
string | bigint | Rational — The same value reference or primitive supplied by the caller.
invert(): RationalMethodReturns the multiplicative inverse of this rational.
The original object is not modified. For normally constructed rationals, the returned denominator remains positive and the sign is carried by the numerator.
DivisionByZeroError Thrown when this rational is exactly zero.
Returns
Rational — A new rational representing 1 / this.
isEven(): booleanMethodTests whether this value is an even integer.
Returns
boolean — true only when the denominator is one and the numerator is even.
isInteger(): booleanMethodTests whether this rational is stored in integer form.
This checks only whether the denominator is exactly 1n. Values built with the raw
constructor must therefore be reduced first if equivalent forms such as 8/4 should be
recognized as integers.
Returns
boolean — true when the stored denominator is one.
isMinusOne(): booleanMethodTests whether this rational is exactly -1 in normalized integer form.
Returns
boolean — true for numerator -1n and denominator 1n.
isNegative(): booleanMethodTests whether the stored numerator is negative.
Nerdamer's normal rational representation keeps the denominator positive, so the numerator carries the sign. Raw constructor values with a negative denominator do not follow that representation.
Returns
boolean — true when the numerator is negative.
isOne(): booleanMethodTests whether this rational is exactly 1 in normalized integer form.
Returns
boolean — true for numerator 1n and denominator 1n.
isRational(value: unknown): valueMethodTests whether a value carries Nerdamer's rational runtime type marker.
This is a marker-based guard rather than an instanceof check, which allows compatible
Nerdamer rational objects to be recognized where constructor identity is not the useful
distinction.
Parameters
| Name | Type | Description |
|---|---|---|
value | unknown | Value to inspect. |
Returns
value — true when value.dataType is Nerdamer's rational marker.
isZero(): booleanMethodTests whether this rational is exactly zero.
Returns
boolean — true when the numerator is zero.
Computes the rational least common multiple with another value.
Parameters
| Name | Type | Description |
|---|---|---|
num | string | Rational | Rational or numeric string to combine with this value. |
Returns
Rational — A new reduced, nonnegative rational LCM. Zero combined with any rational returns
zero. Decimal presentation is preserved when either operand was marked for decimal output.
Computes the rational least common multiple across the supplied values.
The operation is folded pairwise and preserves decimal presentation intent when it is present on an operand.
Parameters
| Name | Type | Description |
|---|---|---|
args | Rational[] | Rational values whose common multiple should be computed. |
Returns
Rational — The pairwise rational LCM.
lt(num: string | Rational): booleanMethodTests whether this rational is less than another rational value.
Parameters
| Name | Type | Description |
|---|---|---|
num | string | Rational | Rational or numeric string to compare against. |
Returns
boolean — true when this rational is less than num.
lte(num: string | Rational): booleanMethodTests whether this rational is less than or equal to another rational value.
Parameters
| Name | Type | Description |
|---|---|---|
num | string | Rational | Rational or numeric string to compare against. |
Returns
boolean — true when this rational is less than or equal to num.
minus(num: Expression): ExpressionMethodSubtracts another rational or expression from this value.
Rational/string input is handled with exact rational arithmetic. Expression input is promoted to Nerdamer's symbolic arithmetic. The original operands are not mutated.
Parameters
| Name | Type | Description |
|---|---|---|
num | Expression | Value to subtract. |
Returns
Expression — A new Rational for rational/string input, or an Expression for expression input.
Computes the exact rational modulo with another value.
The operands are converted to a common denominator, Nerdamer's integer modulo operation is applied to the corresponding numerators, and the resulting fraction is reduced. Decimal presentation is preserved when either operand was marked for decimal output.
A native RangeError when num is zero.
Parameters
| Name | Type | Description |
|---|---|---|
num | string | Rational | Nonzero rational or numeric string used as the modulus. |
Returns
Rational — The reduced rational remainder.
neg(): RationalMethodReturns the additive inverse of this rational.
Returns
Rational — A new rational with the numerator sign reversed.
numerator: bigintPropertyNumerator of the exact rational representation.
PI: RationalPropertyFinite-precision rational approximation of pi at the current configured precision. Recomputed when Rational.set changes the precision.
plus(num: Expression): ExpressionMethodAdds another rational or expression to this value.
Rational/string input is added exactly and reduced. If either rational operand carries decimal presentation intent, the rational result carries it as well. Expression input is promoted to Nerdamer's symbolic arithmetic.
Parameters
| Name | Type | Description |
|---|---|---|
num | Expression | Value to add. |
Returns
Expression — A new Rational for rational/string input, or an Expression for expression input.
pow(num: Expression): ExpressionMethodRaises this rational to a rational or symbolic power.
Expression exponents use Nerdamer's general symbolic power logic. Integer rational
exponents use exact bigint exponentiation, with negative exponents handled by first
inverting the base. Non-integer rational exponents are evaluated through decimal.js and
converted back to a Rational, so that overload is a real numerical approximation rather
than symbolic radical or principal-complex evaluation. Decimal presentation from a numerical
result is retained, while an exact integer result stays exact unless the base already carried
decimal presentation intent.
ZeroToZeroPowerError
Thrown for the indeterminate form 0^0.
DivisionByZeroError Thrown when zero is raised to a negative integer power.
Parameters
| Name | Type | Description |
|---|---|---|
num | Expression | Exponent to apply. |
Returns
Expression — A rational result for rational/string input, or an Expression for expression input.
set(values: { … }): voidMethodUpdates shared numeric settings used by rational decimal conversion.
Currently only precision is acted upon. Setting it updates the global decimal.js
precision, records the value used by Rational.toDecimalString, and recomputes
Rational.PI and Rational.E at the new precision.
Parameters
| Name | Type | Description |
|---|---|---|
values | { … } | Settings object; precision is the supported numeric setting. |
Returns
void
sign(): numberMethodReturns the sign carried by the numerator.
Returns
number — -1 for a negative numerator, 0 for zero, or 1 for a positive numerator.
text(options?: OptionsObject): stringMethodFormats this rational as fraction, integer, or decimal text.
Scientific formatting is selected when options.scientific supplies an integer significant-digit
count from 1 through 1e9. The requested count is preserved in the coefficient, including
trailing zeroes. Otherwise decimal formatting is selected when Rational.asDecimal is set or when
options.decimal is truthy. Non-integer decimal output uses decimal.js; an optional
options.precision temporarily controls its significant-digit precision for this conversion.
Integer decimal output includes a .0 suffix. Without decimal formatting, non-integer
fractions are emitted from the stored numerator and denominator, and integers as plain text.
Parameters
| Name | Type | Description |
|---|---|---|
options | OptionsObject | Formatting options. scientific selects significant-digit scientific output;
decimal forces decimal output and precision controls decimal conversion precision when applicable. |
Returns
string — The formatted numeric text.
times(num: Expression): ExpressionMethodMultiplies this rational by another rational or expression.
Rational/string multiplication is exact and reduced. If either rational operand carries decimal presentation intent, the rational result carries it as well. Expression input is promoted to Nerdamer's symbolic arithmetic.
Parameters
| Name | Type | Description |
|---|---|---|
num | Expression | Value to multiply by. |
Returns
Expression — A new Rational for rational/string input, or an Expression for expression input.
toDecimal(): DecimalMethodConverts the exact fraction to a decimal.js value at the current global Decimal precision.
Integer rationals avoid an unnecessary Decimal division.
Returns
Decimal — A new Decimal representing numerator / denominator.
toDecimalString(precision?: number): stringMethodConverts the exact fraction to decimal text using integer arithmetic.
precision is the maximum number of digits generated after the decimal point. The
conversion truncates at that position rather than rounding, then removes trailing zeros
and a trailing decimal point. When omitted, the configured Rational precision is used.
This method does not depend on Rational.asDecimal; it always requests decimal text.
Parameters
| Name | Type | Description |
|---|---|---|
precision | number | Number of fractional digits to generate. |
Returns
string — Truncated decimal text with unnecessary trailing zeros removed.
Examples
Rational.create('1/3').toDecimalString(5); // "0.33333"
Rational.create('7/4').toDecimalString(5); // "1.75"Converts a string to a rational or normalizes rational ownership for a caller.
Strings always produce a new value through Rational.create. An existing Rational
is returned by identity unless ensureCopy is true.
Parameters
| Name | Type | Description |
|---|---|---|
x | string | Rational | Numeric string or rational value. |
ensureCopy | boolean | Guarantee a distinct object when x is already a Rational. |
Returns
Rational — The converted rational, a copy, or the original rational according to ensureCopy.
toString(options?: OptionsObject): stringMethodReturns the same formatted representation as Rational.text.
Parameters
| Name | Type | Description |
|---|---|---|
options | OptionsObject | Formatting options forwarded to text. |
Returns
string — The formatted rational string.
updateValue(): RationalMethodRefreshes Rational.value from the current numerator and denominator.
This is one of the few mutating methods on Rational; it updates this object and returns
the same reference.
Returns
Rational — This rational instance.
value: stringPropertyThe supplied textual value. Numeric behavior is defined by Rational.numerator and Rational.denominator; callers should not treat this field as an independent authoritative numeric representation.
valueOf(): numberMethodConverts this rational to a native JavaScript number.
This conversion is approximate and subject to the range and precision limits of
JavaScript number. Numerator and denominator values that are both safe integers use
native division directly; larger values retain the Decimal-backed fallback so finite ratios
are not lost merely because an individual component exceeds the native numeric range.
Use the rational representation or Rational.toDecimal when native-number limits
are unacceptable.
Returns
number — The approximate native numeric value.
