/** * Exact rational numbers for ratios that decimal arithmetic cannot represent. * A `Rational` stores a `bigint` numerator and denominator in lowest terms, so * values such as one third survive multiplication, comparison and compounding * without rounding. Use `BigDecimal` for decimal quantities such as money, and * `Rational` when a ratio must stay exact until an explicit conversion. * * @stability unstable * @since 4.1.3 */ import * as BigDecimal from "./BigDecimal.ts"; import * as Equal from "./Equal.ts"; import * as Equ from "./Equivalence.ts"; import { type Inspectable } from "./Inspectable.ts"; import * as Option from "./Option.ts"; import * as order from "./Order.ts"; import type { Ordering } from "./Ordering.ts"; import { type Pipeable } from "./Pipeable.ts"; declare const TypeId = "~effect/Rational"; /** * Represents an exact rational number as a reduced fraction. * * **Details** * * Every `Rational` is kept in canonical form: the numerator and denominator * share no common factor, the denominator is positive, and zero is `0/1`. * Two rationals are equal exactly when their fields are equal. * * **Example** (Inspecting Rational storage) * * ```ts import.meta.vitest * import { Rational } from "effect" * * const r = Rational.makeUnsafe(6n, -4n) * * r.numerator // => -3n * r.denominator // => 2n * ``` * * @stability unstable * @category models * @since 4.1.3 */ export interface Rational extends Equal.Equal, Pipeable, Inspectable { readonly [TypeId]: typeof TypeId; readonly numerator: bigint; readonly denominator: bigint; } /** * Checks whether a given value is a `Rational`. * * **Example** (Checking Rational values) * * ```ts import.meta.vitest * import { Rational } from "effect" * * Rational.isRational(Rational.fromBigInt(1n)) // => true * Rational.isRational("1/3") // => false * ``` * * @stability unstable * @category guards * @since 4.1.3 */ export declare const isRational: (u: unknown) => u is Rational; /** * Creates a `Rational` from a numerator and denominator, returning * `Option.none()` when the denominator is zero. * * **Details** * * The result is reduced to lowest terms with a positive denominator, so * `make(6n, -4n)` and `make(-3n, 2n)` produce the same value. * * **Example** (Creating rationals safely) * * ```ts import.meta.vitest * import { Option, Rational } from "effect" * * Rational.make(6n, -4n) // => Option.some(Rational.makeUnsafe(-3n, 2n)) * Rational.make(1n, 0n) // => Option.none() * ``` * * @see {@link makeUnsafe} for throwing on a zero denominator * * @stability unstable * @category constructors * @since 4.1.3 */ export declare const make: (numerator: bigint, denominator: bigint) => Option.Option; /** * Creates a `Rational` from a numerator and denominator. * * **Gotchas** * * Throws a `RangeError` if the denominator is zero. * * **Example** (Creating rationals from bigint pairs) * * ```ts import.meta.vitest * import { Rational } from "effect" * * Rational.format(Rational.makeUnsafe(2n, 6n)) // => "1/3" * ``` * * @see {@link make} for returning `Option.none` on a zero denominator * * @stability unstable * @category constructors * @since 4.1.3 */ export declare const makeUnsafe: (numerator: bigint, denominator: bigint) => Rational; /** * Creates an integer `Rational` from a `bigint`. * * **Example** (Creating rationals from bigint) * * ```ts import.meta.vitest * import { Rational } from "effect" * * Rational.format(Rational.fromBigInt(-3n)) // => "-3/1" * ``` * * @stability unstable * @category constructors * @since 4.1.3 */ export declare const fromBigInt: (n: bigint) => Rational; /** * Converts a `BigDecimal` to the exact `Rational` it denotes. * * **When to use** * * Use as the exact route from decimal text: `0.1` becomes `1/10`, which * {@link fromNumber} cannot produce. * * **Example** (Converting decimals exactly) * * ```ts import.meta.vitest * import { BigDecimal, Rational } from "effect" * * Rational.fromBigDecimal(BigDecimal.fromStringUnsafe("-12.50")) // => Rational.makeUnsafe(-25n, 2n) * ``` * * @see {@link toBigDecimal} for the rounded conversion back * * @stability unstable * @category constructors * @since 4.1.3 */ export declare const fromBigDecimal: (n: BigDecimal.BigDecimal) => Rational; /** * Creates a `Rational` from a finite `number`, returning `Option.none()` for * `NaN`, `+Infinity` and `-Infinity`. * * **Details** * * The result is the exact binary value of the number, not its decimal display. * Negative zero becomes `0/1`. * * **Gotchas** * * `0.1` is not one tenth: the nearest double is `3602879701896397 / 2^55`, and * that is the value this function returns. Use {@link fromBigDecimal} with a * decimal string when the decimal reading is intended. * * **Example** (Creating rationals from numbers) * * ```ts import.meta.vitest * import { Option, Rational } from "effect" * * Rational.fromNumber(0.75) // => Option.some(Rational.makeUnsafe(3n, 4n)) * Rational.fromNumber(NaN) // => Option.none() * ``` * * @see {@link fromNumberUnsafe} for throwing when the number is not finite * * @stability unstable * @category constructors * @since 4.1.3 */ export declare const fromNumber: (n: number) => Option.Option; /** * Creates a `Rational` from a finite `number`. * * **Gotchas** * * Throws a `RangeError` if the number is not finite. See {@link fromNumber} * for why `0.1` does not become one tenth. * * **Example** (Creating rationals from trusted numbers) * * ```ts import.meta.vitest * import { Rational } from "effect" * * Rational.fromNumberUnsafe(0.5) // => Rational.makeUnsafe(1n, 2n) * ``` * * @see {@link fromNumber} for returning `Option.none` when the number is not finite * * @stability unstable * @category constructors * @since 4.1.3 */ export declare const fromNumberUnsafe: (n: number) => Rational; /** * Parses an integer or fraction string into a `Rational` safely. * * **Details** * * Accepts `[+-]digits` or `[+-]digits/[+-]digits`. Reducible fractions and * negative denominators are normalized. Whitespace, decimal points, exponents, * the empty string and a zero denominator produce `Option.none()`. * * **Example** (Parsing fraction strings) * * ```ts import.meta.vitest * import { Option, Rational } from "effect" * * Rational.fromString("-4/-6") // => Option.some(Rational.makeUnsafe(2n, 3n)) * Rational.fromString("3") // => Option.some(Rational.fromBigInt(3n)) * Rational.fromString("1.5") // => Option.none() * ``` * * @see {@link fromStringUnsafe} for parsing that throws on invalid input * @see {@link format} for the inverse operation * * @stability unstable * @category constructors * @since 4.1.3 */ export declare const fromString: (s: string) => Option.Option; /** * Parses an integer or fraction string into a `Rational`. * * **Gotchas** * * Throws an `Error` if the string is not a valid fraction. See * {@link fromString} for the accepted grammar. * * **Example** (Parsing trusted fraction strings) * * ```ts import.meta.vitest * import { Rational } from "effect" * * Rational.fromStringUnsafe("6/5") // => Rational.makeUnsafe(6n, 5n) * ``` * * @see {@link fromString} for returning `Option.none` on invalid input * * @stability unstable * @category constructors * @since 4.1.3 */ export declare const fromStringUnsafe: (s: string) => Rational; /** * Adds two rationals exactly. * * **Example** (Adding rationals) * * ```ts import.meta.vitest * import { Rational } from "effect" * * Rational.sum(Rational.makeUnsafe(1n, 3n), Rational.makeUnsafe(1n, 6n)) // => Rational.makeUnsafe(1n, 2n) * ``` * * @stability unstable * @category math * @since 4.1.3 */ export declare const sum: { /** * Adds two rationals exactly. * * **Example** (Adding rationals) * * ```ts import.meta.vitest * import { Rational } from "effect" * * Rational.sum(Rational.makeUnsafe(1n, 3n), Rational.makeUnsafe(1n, 6n)) // => Rational.makeUnsafe(1n, 2n) * ``` * * @stability unstable * @category math * @since 4.1.3 */ (that: Rational): (self: Rational) => Rational; /** * Adds two rationals exactly. * * **Example** (Adding rationals) * * ```ts import.meta.vitest * import { Rational } from "effect" * * Rational.sum(Rational.makeUnsafe(1n, 3n), Rational.makeUnsafe(1n, 6n)) // => Rational.makeUnsafe(1n, 2n) * ``` * * @stability unstable * @category math * @since 4.1.3 */ (self: Rational, that: Rational): Rational; }; /** * Sums all rationals in an iterable, returning zero for an empty collection. * * **Example** (Summing rationals) * * ```ts import.meta.vitest * import { Rational } from "effect" * * const third = Rational.makeUnsafe(1n, 3n) * * Rational.sumAll([third, third, third]) // => Rational.fromBigInt(1n) * ``` * * @stability unstable * @category math * @since 4.1.3 */ export declare const sumAll: (collection: Iterable) => Rational; /** * Subtracts `that` from `self` exactly. * * **Example** (Subtracting rationals) * * ```ts import.meta.vitest * import { Rational } from "effect" * * Rational.subtract(Rational.fromBigInt(1n), Rational.makeUnsafe(1n, 3n)) // => Rational.makeUnsafe(2n, 3n) * ``` * * @stability unstable * @category math * @since 4.1.3 */ export declare const subtract: { /** * Subtracts `that` from `self` exactly. * * **Example** (Subtracting rationals) * * ```ts import.meta.vitest * import { Rational } from "effect" * * Rational.subtract(Rational.fromBigInt(1n), Rational.makeUnsafe(1n, 3n)) // => Rational.makeUnsafe(2n, 3n) * ``` * * @stability unstable * @category math * @since 4.1.3 */ (that: Rational): (self: Rational) => Rational; /** * Subtracts `that` from `self` exactly. * * **Example** (Subtracting rationals) * * ```ts import.meta.vitest * import { Rational } from "effect" * * Rational.subtract(Rational.fromBigInt(1n), Rational.makeUnsafe(1n, 3n)) // => Rational.makeUnsafe(2n, 3n) * ``` * * @stability unstable * @category math * @since 4.1.3 */ (self: Rational, that: Rational): Rational; }; /** * Multiplies two rationals exactly. * * **Example** (Multiplying rationals) * * ```ts import.meta.vitest * import { Rational } from "effect" * * Rational.multiply(Rational.makeUnsafe(1n, 3n), Rational.fromBigInt(3n)) // => Rational.fromBigInt(1n) * ``` * * @stability unstable * @category math * @since 4.1.3 */ export declare const multiply: { /** * Multiplies two rationals exactly. * * **Example** (Multiplying rationals) * * ```ts import.meta.vitest * import { Rational } from "effect" * * Rational.multiply(Rational.makeUnsafe(1n, 3n), Rational.fromBigInt(3n)) // => Rational.fromBigInt(1n) * ``` * * @stability unstable * @category math * @since 4.1.3 */ (that: Rational): (self: Rational) => Rational; /** * Multiplies two rationals exactly. * * **Example** (Multiplying rationals) * * ```ts import.meta.vitest * import { Rational } from "effect" * * Rational.multiply(Rational.makeUnsafe(1n, 3n), Rational.fromBigInt(3n)) // => Rational.fromBigInt(1n) * ``` * * @stability unstable * @category math * @since 4.1.3 */ (self: Rational, that: Rational): Rational; }; /** * Multiplies all rationals in an iterable, returning one for an empty * collection. * * **Example** (Compounding ratios) * * ```ts import.meta.vitest * import { Rational } from "effect" * * Rational.multiplyAll([Rational.makeUnsafe(3n, 2n), Rational.makeUnsafe(4n, 3n)]) // => Rational.fromBigInt(2n) * ``` * * @stability unstable * @category math * @since 4.1.3 */ export declare const multiplyAll: (collection: Iterable) => Rational; /** * Divides `self` by `that`, returning `Option.none()` when `that` is zero. * * **Example** (Dividing rationals safely) * * ```ts import.meta.vitest * import { Option, Rational } from "effect" * * const third = Rational.makeUnsafe(1n, 3n) * * Rational.divide(third, Rational.fromBigInt(3n)) // => Option.some(Rational.makeUnsafe(1n, 9n)) * Rational.divide(third, Rational.fromBigInt(0n)) // => Option.none() * ``` * * @see {@link divideUnsafe} for division that throws when the divisor is zero * * @stability unstable * @category math * @since 4.1.3 */ export declare const divide: { /** * Divides `self` by `that`, returning `Option.none()` when `that` is zero. * * **Example** (Dividing rationals safely) * * ```ts import.meta.vitest * import { Option, Rational } from "effect" * * const third = Rational.makeUnsafe(1n, 3n) * * Rational.divide(third, Rational.fromBigInt(3n)) // => Option.some(Rational.makeUnsafe(1n, 9n)) * Rational.divide(third, Rational.fromBigInt(0n)) // => Option.none() * ``` * * @see {@link divideUnsafe} for division that throws when the divisor is zero * * @stability unstable * @category math * @since 4.1.3 */ (that: Rational): (self: Rational) => Option.Option; /** * Divides `self` by `that`, returning `Option.none()` when `that` is zero. * * **Example** (Dividing rationals safely) * * ```ts import.meta.vitest * import { Option, Rational } from "effect" * * const third = Rational.makeUnsafe(1n, 3n) * * Rational.divide(third, Rational.fromBigInt(3n)) // => Option.some(Rational.makeUnsafe(1n, 9n)) * Rational.divide(third, Rational.fromBigInt(0n)) // => Option.none() * ``` * * @see {@link divideUnsafe} for division that throws when the divisor is zero * * @stability unstable * @category math * @since 4.1.3 */ (self: Rational, that: Rational): Option.Option; }; /** * Divides `self` by `that`. * * **Gotchas** * * Throws a `RangeError` if the divisor is zero. * * **Example** (Dividing rationals) * * ```ts import.meta.vitest * import { Rational } from "effect" * * Rational.divideUnsafe(Rational.makeUnsafe(-2n, 3n), Rational.makeUnsafe(4n, 9n)) // => Rational.makeUnsafe(-3n, 2n) * ``` * * @see {@link divide} for division that returns `Option.none` when the divisor is zero * * @stability unstable * @category math * @since 4.1.3 */ export declare const divideUnsafe: { /** * Divides `self` by `that`. * * **Gotchas** * * Throws a `RangeError` if the divisor is zero. * * **Example** (Dividing rationals) * * ```ts import.meta.vitest * import { Rational } from "effect" * * Rational.divideUnsafe(Rational.makeUnsafe(-2n, 3n), Rational.makeUnsafe(4n, 9n)) // => Rational.makeUnsafe(-3n, 2n) * ``` * * @see {@link divide} for division that returns `Option.none` when the divisor is zero * * @stability unstable * @category math * @since 4.1.3 */ (that: Rational): (self: Rational) => Rational; /** * Divides `self` by `that`. * * **Gotchas** * * Throws a `RangeError` if the divisor is zero. * * **Example** (Dividing rationals) * * ```ts import.meta.vitest * import { Rational } from "effect" * * Rational.divideUnsafe(Rational.makeUnsafe(-2n, 3n), Rational.makeUnsafe(4n, 9n)) // => Rational.makeUnsafe(-3n, 2n) * ``` * * @see {@link divide} for division that returns `Option.none` when the divisor is zero * * @stability unstable * @category math * @since 4.1.3 */ (self: Rational, that: Rational): Rational; }; /** * Raises a rational to an integer power, returning `Option.none()` for zero * raised to a negative exponent. * * **Details** * * A negative exponent inverts the fraction before raising it, and any value * to the power zero is one, including zero itself. * * **Gotchas** * * The exponent must be a safe integer. Anything else, including `0.5`, `NaN` * and `Infinity`, throws a `RangeError` instead of being rounded. * * **Example** (Raising to integer powers) * * ```ts import.meta.vitest * import { Option, Rational } from "effect" * * Rational.pow(Rational.makeUnsafe(1n, 3n), -1) // => Option.some(Rational.fromBigInt(3n)) * Rational.pow(Rational.fromBigInt(0n), -1) // => Option.none() * ``` * * @see {@link powUnsafe} for throwing when zero is raised to a negative exponent * @see {@link nthRoot} for the inverse of a power * * @stability unstable * @category math * @since 4.1.3 */ export declare const pow: { /** * Raises a rational to an integer power, returning `Option.none()` for zero * raised to a negative exponent. * * **Details** * * A negative exponent inverts the fraction before raising it, and any value * to the power zero is one, including zero itself. * * **Gotchas** * * The exponent must be a safe integer. Anything else, including `0.5`, `NaN` * and `Infinity`, throws a `RangeError` instead of being rounded. * * **Example** (Raising to integer powers) * * ```ts import.meta.vitest * import { Option, Rational } from "effect" * * Rational.pow(Rational.makeUnsafe(1n, 3n), -1) // => Option.some(Rational.fromBigInt(3n)) * Rational.pow(Rational.fromBigInt(0n), -1) // => Option.none() * ``` * * @see {@link powUnsafe} for throwing when zero is raised to a negative exponent * @see {@link nthRoot} for the inverse of a power * * @stability unstable * @category math * @since 4.1.3 */ (exponent: number): (self: Rational) => Option.Option; /** * Raises a rational to an integer power, returning `Option.none()` for zero * raised to a negative exponent. * * **Details** * * A negative exponent inverts the fraction before raising it, and any value * to the power zero is one, including zero itself. * * **Gotchas** * * The exponent must be a safe integer. Anything else, including `0.5`, `NaN` * and `Infinity`, throws a `RangeError` instead of being rounded. * * **Example** (Raising to integer powers) * * ```ts import.meta.vitest * import { Option, Rational } from "effect" * * Rational.pow(Rational.makeUnsafe(1n, 3n), -1) // => Option.some(Rational.fromBigInt(3n)) * Rational.pow(Rational.fromBigInt(0n), -1) // => Option.none() * ``` * * @see {@link powUnsafe} for throwing when zero is raised to a negative exponent * @see {@link nthRoot} for the inverse of a power * * @stability unstable * @category math * @since 4.1.3 */ (self: Rational, exponent: number): Option.Option; }; /** * Raises a rational to an integer power. * * **Gotchas** * * Throws a `RangeError` for zero raised to a negative exponent, and for an * exponent that is not a safe integer. * * **Example** (Compounding a ratio) * * ```ts import.meta.vitest * import { Rational } from "effect" * * // Twelve pure fifths overshoot seven octaves by the Pythagorean comma. * Rational.powUnsafe(Rational.makeUnsafe(3n, 2n), 12) // => Rational.makeUnsafe(531441n, 4096n) * ``` * * @see {@link pow} for returning `Option.none` when zero is raised to a negative exponent * * @stability unstable * @category math * @since 4.1.3 */ export declare const powUnsafe: { /** * Raises a rational to an integer power. * * **Gotchas** * * Throws a `RangeError` for zero raised to a negative exponent, and for an * exponent that is not a safe integer. * * **Example** (Compounding a ratio) * * ```ts import.meta.vitest * import { Rational } from "effect" * * // Twelve pure fifths overshoot seven octaves by the Pythagorean comma. * Rational.powUnsafe(Rational.makeUnsafe(3n, 2n), 12) // => Rational.makeUnsafe(531441n, 4096n) * ``` * * @see {@link pow} for returning `Option.none` when zero is raised to a negative exponent * * @stability unstable * @category math * @since 4.1.3 */ (exponent: number): (self: Rational) => Rational; /** * Raises a rational to an integer power. * * **Gotchas** * * Throws a `RangeError` for zero raised to a negative exponent, and for an * exponent that is not a safe integer. * * **Example** (Compounding a ratio) * * ```ts import.meta.vitest * import { Rational } from "effect" * * // Twelve pure fifths overshoot seven octaves by the Pythagorean comma. * Rational.powUnsafe(Rational.makeUnsafe(3n, 2n), 12) // => Rational.makeUnsafe(531441n, 4096n) * ``` * * @see {@link pow} for returning `Option.none` when zero is raised to a negative exponent * * @stability unstable * @category math * @since 4.1.3 */ (self: Rational, exponent: number): Rational; }; /** * Takes the exact n-th root of a rational, returning `Option.none()` when the * root is not rational. * * **Details** * * A rational in lowest terms is the n-th power of a rational exactly when its * numerator and denominator are n-th powers of integers. An odd degree also * takes the root of a negative rational. Returns `Option.none()` when the * degree is even and the rational negative, or when the root is irrational. * * **Gotchas** * * The degree must be a positive safe integer. Anything else, including `0`, * `0.5`, `NaN` and `Infinity`, throws a `RangeError` instead of being rounded. * * **Example** (Taking exact roots) * * ```ts import.meta.vitest * import { Option, Rational } from "effect" * * Rational.nthRoot(Rational.makeUnsafe(8n, 27n), 3) // => Option.some(Rational.makeUnsafe(2n, 3n)) * Rational.nthRoot(Rational.makeUnsafe(-8n, 27n), 3) // => Option.some(Rational.makeUnsafe(-2n, 3n)) * Rational.nthRoot(Rational.fromBigInt(2n), 2) // => Option.none() * ``` * * @see {@link nthRootUnsafe} for throwing when the root is not rational * @see {@link pow} for integer powers * * @stability unstable * @category math * @since 4.1.5 */ export declare const nthRoot: { /** * Takes the exact n-th root of a rational, returning `Option.none()` when the * root is not rational. * * **Details** * * A rational in lowest terms is the n-th power of a rational exactly when its * numerator and denominator are n-th powers of integers. An odd degree also * takes the root of a negative rational. Returns `Option.none()` when the * degree is even and the rational negative, or when the root is irrational. * * **Gotchas** * * The degree must be a positive safe integer. Anything else, including `0`, * `0.5`, `NaN` and `Infinity`, throws a `RangeError` instead of being rounded. * * **Example** (Taking exact roots) * * ```ts import.meta.vitest * import { Option, Rational } from "effect" * * Rational.nthRoot(Rational.makeUnsafe(8n, 27n), 3) // => Option.some(Rational.makeUnsafe(2n, 3n)) * Rational.nthRoot(Rational.makeUnsafe(-8n, 27n), 3) // => Option.some(Rational.makeUnsafe(-2n, 3n)) * Rational.nthRoot(Rational.fromBigInt(2n), 2) // => Option.none() * ``` * * @see {@link nthRootUnsafe} for throwing when the root is not rational * @see {@link pow} for integer powers * * @stability unstable * @category math * @since 4.1.5 */ (degree: number): (self: Rational) => Option.Option; /** * Takes the exact n-th root of a rational, returning `Option.none()` when the * root is not rational. * * **Details** * * A rational in lowest terms is the n-th power of a rational exactly when its * numerator and denominator are n-th powers of integers. An odd degree also * takes the root of a negative rational. Returns `Option.none()` when the * degree is even and the rational negative, or when the root is irrational. * * **Gotchas** * * The degree must be a positive safe integer. Anything else, including `0`, * `0.5`, `NaN` and `Infinity`, throws a `RangeError` instead of being rounded. * * **Example** (Taking exact roots) * * ```ts import.meta.vitest * import { Option, Rational } from "effect" * * Rational.nthRoot(Rational.makeUnsafe(8n, 27n), 3) // => Option.some(Rational.makeUnsafe(2n, 3n)) * Rational.nthRoot(Rational.makeUnsafe(-8n, 27n), 3) // => Option.some(Rational.makeUnsafe(-2n, 3n)) * Rational.nthRoot(Rational.fromBigInt(2n), 2) // => Option.none() * ``` * * @see {@link nthRootUnsafe} for throwing when the root is not rational * @see {@link pow} for integer powers * * @stability unstable * @category math * @since 4.1.5 */ (self: Rational, degree: number): Option.Option; }; /** * Takes the exact n-th root of a rational. * * **Gotchas** * * Throws a `RangeError` when the root is not rational, or when the degree is * not a positive safe integer. * * **Example** (Undoing a compounded ratio) * * ```ts import.meta.vitest * import { Rational } from "effect" * * Rational.nthRootUnsafe(Rational.makeUnsafe(531441n, 4096n), 12) // => Rational.makeUnsafe(3n, 2n) * ``` * * @see {@link nthRoot} for returning `Option.none` when the root is not rational * @see {@link powUnsafe} for integer powers * * @stability unstable * @category math * @since 4.1.5 */ export declare const nthRootUnsafe: { /** * Takes the exact n-th root of a rational. * * **Gotchas** * * Throws a `RangeError` when the root is not rational, or when the degree is * not a positive safe integer. * * **Example** (Undoing a compounded ratio) * * ```ts import.meta.vitest * import { Rational } from "effect" * * Rational.nthRootUnsafe(Rational.makeUnsafe(531441n, 4096n), 12) // => Rational.makeUnsafe(3n, 2n) * ``` * * @see {@link nthRoot} for returning `Option.none` when the root is not rational * @see {@link powUnsafe} for integer powers * * @stability unstable * @category math * @since 4.1.5 */ (degree: number): (self: Rational) => Rational; /** * Takes the exact n-th root of a rational. * * **Gotchas** * * Throws a `RangeError` when the root is not rational, or when the degree is * not a positive safe integer. * * **Example** (Undoing a compounded ratio) * * ```ts import.meta.vitest * import { Rational } from "effect" * * Rational.nthRootUnsafe(Rational.makeUnsafe(531441n, 4096n), 12) // => Rational.makeUnsafe(3n, 2n) * ``` * * @see {@link nthRoot} for returning `Option.none` when the root is not rational * @see {@link powUnsafe} for integer powers * * @stability unstable * @category math * @since 4.1.5 */ (self: Rational, degree: number): Rational; }; /** * Negates a rational. * * @stability unstable * @category math * @since 4.1.3 */ export declare const negate: (n: Rational) => Rational; /** * Returns the absolute value of a rational. * * @stability unstable * @category math * @since 4.1.3 */ export declare const abs: (n: Rational) => Rational; /** * Returns the sign of a rational as an `Ordering`. * * @stability unstable * @category math * @since 4.1.3 */ export declare const sign: (n: Rational) => Ordering; /** * An `Order` instance comparing rationals by exact value. * * **Example** (Comparing rationals) * * ```ts import.meta.vitest * import { BigDecimal, Rational } from "effect" * * const third = Rational.makeUnsafe(1n, 3n) * const nearlyThird = Rational.fromBigDecimal(BigDecimal.fromStringUnsafe("0.3333333333")) * * Rational.Order(third, nearlyThird) // => 1 * ``` * * @stability unstable * @category instances * @since 4.1.3 */ export declare const Order: order.Order; /** * An `Equivalence` instance for rationals. Canonical form makes this a field * comparison. * * @stability unstable * @category instances * @since 4.1.3 */ export declare const Equivalence: Equ.Equivalence; /** * Checks whether two rationals are equal. * * @see {@link Equivalence} for passing rational equality to APIs that require an `Equivalence` * * @stability unstable * @category predicates * @since 4.1.3 */ export declare const equals: { /** * Checks whether two rationals are equal. * * @see {@link Equivalence} for passing rational equality to APIs that require an `Equivalence` * * @stability unstable * @category predicates * @since 4.1.3 */ (that: Rational): (self: Rational) => boolean; /** * Checks whether two rationals are equal. * * @see {@link Equivalence} for passing rational equality to APIs that require an `Equivalence` * * @stability unstable * @category predicates * @since 4.1.3 */ (self: Rational, that: Rational): boolean; }; /** * Returns `true` if the first argument is less than the second. * * @stability unstable * @category predicates * @since 4.1.3 */ export declare const isLessThan: { /** * Returns `true` if the first argument is less than the second. * * @stability unstable * @category predicates * @since 4.1.3 */ (that: Rational): (self: Rational) => boolean; /** * Returns `true` if the first argument is less than the second. * * @stability unstable * @category predicates * @since 4.1.3 */ (self: Rational, that: Rational): boolean; }; /** * Returns `true` if the first argument is less than or equal to the second. * * @stability unstable * @category predicates * @since 4.1.3 */ export declare const isLessThanOrEqualTo: { /** * Returns `true` if the first argument is less than or equal to the second. * * @stability unstable * @category predicates * @since 4.1.3 */ (that: Rational): (self: Rational) => boolean; /** * Returns `true` if the first argument is less than or equal to the second. * * @stability unstable * @category predicates * @since 4.1.3 */ (self: Rational, that: Rational): boolean; }; /** * Returns `true` if the first argument is greater than the second. * * @stability unstable * @category predicates * @since 4.1.3 */ export declare const isGreaterThan: { /** * Returns `true` if the first argument is greater than the second. * * @stability unstable * @category predicates * @since 4.1.3 */ (that: Rational): (self: Rational) => boolean; /** * Returns `true` if the first argument is greater than the second. * * @stability unstable * @category predicates * @since 4.1.3 */ (self: Rational, that: Rational): boolean; }; /** * Returns `true` if the first argument is greater than or equal to the second. * * @stability unstable * @category predicates * @since 4.1.3 */ export declare const isGreaterThanOrEqualTo: { /** * Returns `true` if the first argument is greater than or equal to the second. * * @stability unstable * @category predicates * @since 4.1.3 */ (that: Rational): (self: Rational) => boolean; /** * Returns `true` if the first argument is greater than or equal to the second. * * @stability unstable * @category predicates * @since 4.1.3 */ (self: Rational, that: Rational): boolean; }; /** * Checks whether a rational lies within an inclusive range. * * @stability unstable * @category predicates * @since 4.1.3 */ export declare const between: { /** * Checks whether a rational lies within an inclusive range. * * @stability unstable * @category predicates * @since 4.1.3 */ (options: { minimum: Rational; maximum: Rational; }): (self: Rational) => boolean; /** * Checks whether a rational lies within an inclusive range. * * @stability unstable * @category predicates * @since 4.1.3 */ (self: Rational, options: { minimum: Rational; maximum: Rational; }): boolean; }; /** * Restricts a rational to an inclusive range. * * @stability unstable * @category math * @since 4.1.3 */ export declare const clamp: { /** * Restricts a rational to an inclusive range. * * @stability unstable * @category math * @since 4.1.3 */ (options: { minimum: Rational; maximum: Rational; }): (self: Rational) => Rational; /** * Restricts a rational to an inclusive range. * * @stability unstable * @category math * @since 4.1.3 */ (self: Rational, options: { minimum: Rational; maximum: Rational; }): Rational; }; /** * Returns the smaller of two rationals. * * @stability unstable * @category math * @since 4.1.3 */ export declare const min: { /** * Returns the smaller of two rationals. * * @stability unstable * @category math * @since 4.1.3 */ (that: Rational): (self: Rational) => Rational; /** * Returns the smaller of two rationals. * * @stability unstable * @category math * @since 4.1.3 */ (self: Rational, that: Rational): Rational; }; /** * Returns the larger of two rationals. * * @stability unstable * @category math * @since 4.1.3 */ export declare const max: { /** * Returns the larger of two rationals. * * @stability unstable * @category math * @since 4.1.3 */ (that: Rational): (self: Rational) => Rational; /** * Returns the larger of two rationals. * * @stability unstable * @category math * @since 4.1.3 */ (self: Rational, that: Rational): Rational; }; /** * Checks whether a rational is zero. * * @stability unstable * @category predicates * @since 4.1.3 */ export declare const isZero: (n: Rational) => boolean; /** * Checks whether a rational is strictly negative. * * @stability unstable * @category predicates * @since 4.1.3 */ export declare const isNegative: (n: Rational) => boolean; /** * Checks whether a rational is strictly positive. * * @stability unstable * @category predicates * @since 4.1.3 */ export declare const isPositive: (n: Rational) => boolean; /** * Checks whether a rational is an integer, that is, whether its denominator is * one. * * @stability unstable * @category predicates * @since 4.1.3 */ export declare const isInteger: (n: Rational) => boolean; /** * Formats a rational as `numerator/denominator`. * * **Details** * * The output is always a fraction in lowest terms, so integers render as * `3/1` and zero as `0/1`. It is the inverse of {@link fromString}. * * **Example** (Formatting rationals) * * ```ts import.meta.vitest * import { Rational } from "effect" * * Rational.format(Rational.makeUnsafe(12n, 10n)) // => "6/5" * Rational.format(Rational.fromBigInt(0n)) // => "0/1" * ``` * * @stability unstable * @category converting * @since 4.1.3 */ export declare const format: (n: Rational) => string; /** * Converts a rational to the nearest JavaScript `number`. * * **Details** * * The ratio is rounded as a whole, so a numerator and denominator that each * exceed `Number.MAX_VALUE` still produce a finite result when their quotient * is finite. Every finite double round-trips through {@link fromNumber}. * * **Gotchas** * * This conversion is unsafe because most rationals are not representable as * doubles: the result is rounded, and very large magnitudes become * `Infinity`. * * **Example** (Converting rationals to numbers) * * ```ts import.meta.vitest * import { Rational } from "effect" * * Rational.toNumberUnsafe(Rational.makeUnsafe(1n, 3n)) // => 1 / 3 * ``` * * @see {@link toBigDecimal} for a conversion with an explicit scale * * @stability unstable * @category converting * @since 4.1.3 */ export declare const toNumberUnsafe: (n: Rational) => number; /** * Converts a rational to a `BigDecimal` rounded at the given scale. * * **Details** * * `scale` is the number of decimal places to keep; a negative scale rounds to * tens, hundreds and so on. `mode` defaults to `"half-from-zero"`, matching * `BigDecimal.round`. * * **Gotchas** * * Throws a `RangeError` if `scale` is not a safe integer. * * **Example** (Rounding rationals to decimals) * * ```ts import.meta.vitest * import { BigDecimal, Rational } from "effect" * * const twoThirds = Rational.makeUnsafe(2n, 3n) * * Rational.toBigDecimal(twoThirds, { scale: 3 }) // => BigDecimal.fromStringUnsafe("0.667") * Rational.toBigDecimal(twoThirds, { scale: 3, mode: "floor" }) // => BigDecimal.fromStringUnsafe("0.666") * ``` * * @see {@link fromBigDecimal} for the exact conversion back * * @stability unstable * @category converting * @since 4.1.3 */ export declare const toBigDecimal: { /** * Converts a rational to a `BigDecimal` rounded at the given scale. * * **Details** * * `scale` is the number of decimal places to keep; a negative scale rounds to * tens, hundreds and so on. `mode` defaults to `"half-from-zero"`, matching * `BigDecimal.round`. * * **Gotchas** * * Throws a `RangeError` if `scale` is not a safe integer. * * **Example** (Rounding rationals to decimals) * * ```ts import.meta.vitest * import { BigDecimal, Rational } from "effect" * * const twoThirds = Rational.makeUnsafe(2n, 3n) * * Rational.toBigDecimal(twoThirds, { scale: 3 }) // => BigDecimal.fromStringUnsafe("0.667") * Rational.toBigDecimal(twoThirds, { scale: 3, mode: "floor" }) // => BigDecimal.fromStringUnsafe("0.666") * ``` * * @see {@link fromBigDecimal} for the exact conversion back * * @stability unstable * @category converting * @since 4.1.3 */ (options: { readonly scale: number; readonly mode?: BigDecimal.RoundingMode; }): (self: Rational) => BigDecimal.BigDecimal; /** * Converts a rational to a `BigDecimal` rounded at the given scale. * * **Details** * * `scale` is the number of decimal places to keep; a negative scale rounds to * tens, hundreds and so on. `mode` defaults to `"half-from-zero"`, matching * `BigDecimal.round`. * * **Gotchas** * * Throws a `RangeError` if `scale` is not a safe integer. * * **Example** (Rounding rationals to decimals) * * ```ts import.meta.vitest * import { BigDecimal, Rational } from "effect" * * const twoThirds = Rational.makeUnsafe(2n, 3n) * * Rational.toBigDecimal(twoThirds, { scale: 3 }) // => BigDecimal.fromStringUnsafe("0.667") * Rational.toBigDecimal(twoThirds, { scale: 3, mode: "floor" }) // => BigDecimal.fromStringUnsafe("0.666") * ``` * * @see {@link fromBigDecimal} for the exact conversion back * * @stability unstable * @category converting * @since 4.1.3 */ (self: Rational, options: { readonly scale: number; readonly mode?: BigDecimal.RoundingMode; }): BigDecimal.BigDecimal; }; export {}; //# sourceMappingURL=Rational.d.ts.map