Skip to content
FutureBlitz

    Install

    Import std.math.big.rational

    Rational

    std.math.big.rational

    class

    Public Class Rational Implements Comparable, Hashable, Printable, Numeric

    A fraction of two BigInts, in lowest terms, which is exact however it's added and multiplied.

    Examples

    Local sum = Rational(1, 3) + Rational(1, 6)
    Expect(sum == Rational(1, 2))
    Expect(sum.Numerator == BigInt(1) && sum.Denominator == BigInt(2))
    Expect(Rational(7, 2).Floor() == BigInt(3))
    Expect(Rational(2, -4) == Rational(-1, 2))

    std/math/big/rational.fb, line 29

    • Public ReadOnly Field Numerator: BigInt field #

      Its numerator, which has its sign.

    • Public ReadOnly Field Denominator: BigInt field #

      Its denominator, which is above 0.

    • @PanicsAtCaller Public New(numerator: BigInt, denominator: BigInt) constructor #

      The fraction in lowest terms, with its denominator above 0, so Rational(2, -4) is -1/2. A denominator of 0 panics.

    • @PanicsAtCaller Public New(numerator: Int64, denominator: Int64 = 1) constructor #

      The fraction numerator over denominator, in lowest terms. A denominator of 0 panics.

    • Public New(value: BigInt) constructor #

      The BigInt, over 1.

    • Public Static Method Zero(): Rational static method #

      The zero Rational, 0, which adding leaves alone.

    • Public Static Method One(): Rational static method #

      The Rational 1, which multiplying leaves alone.

    • @PanicsAtCaller Public Static Method FromDouble(value: Double): Rational static method #

      The Double exactly, as a fraction over a power of 2, so 0.1 is 3602879701896397/36028797018963968. A NaN or an infinity panics.

    • Public Static Method FromDecimal(value: Decimal): Rational static method #

      The Decimal exactly, as 0.75 is 3/4: its digits times or over a power of 10, from its text, as "-1.25E-7" and "0.75" are, so 1E+999999 doesn't go through a million digits.

    • Public Static Method Parse(text: String): Rational? static method #

      The fraction the text is: an integer, a "/", and digits, as "-3/4" is, or a Decimal's text, as "0.75" and "1e-3" are. Other text is Null, and so is a denominator of 0.

    • Public Method ToString(): String method #

      "3/4", or only the numerator when the denominator is 1, as in "-5".

    • Public Method ToDouble(): Double method #

      The Double nearest it, ties to even: an infinity past the largest Double, and 0 below the smallest.

    • @PanicsAtCaller Public Method ToDecimal(): Decimal method #

      It as a Decimal, in a Decimal's 28 digits.

    • Public Property Sign: Int property #

      -1, 0, or 1 as it's below, at, or above 0.

    • Public Method Abs(): Rational method #

      Its distance from 0: the same fraction, without its sign.

    • Public Method Truncate(): BigInt method #

      The whole number it rounds to: toward 0, down, up, or to the nearest, a half to the even one.

    • Public Method Floor(): BigInt method #

      The whole number it rounds down to.

    • Public Method Ceiling(): BigInt method #

      The whole number it rounds up to.

    • Public Method Round(): BigInt method #

      The whole number nearest it, a half to the even one.

    • Public Method Hash(): UInt64 method #

      A hash of its numerator and denominator, which Rationals that are equal share.

    • Operator ==(a: Rational, b: Rational): Bool operator #

      Whether a and b are the same fraction.

    • Operator ==(a: Rational, b: Int64): Bool operator #

      Whether a and b are equal.

    • Operator ==(a: Int64, b: Rational): Bool operator #

      Whether a and b are equal.

    • Operator <(a: Rational, b: Rational): Bool operator #

      Whether a is less than b.

    • Operator <(a: Rational, b: Int64): Bool operator #

      Whether a comes before b.

    • Operator <(a: Int64, b: Rational): Bool operator #

      Whether a comes before b.

    • Operator >(a: Rational, b: Int64): Bool operator #

      Whether a comes after b.

    • Operator >(a: Int64, b: Rational): Bool operator #

      Whether a comes after b.

    • Operator <=(a: Rational, b: Int64): Bool operator #

      Whether a comes before b or is equal to it.

    • Operator <=(a: Int64, b: Rational): Bool operator #

      Whether a comes before b or is equal to it.

    • Operator >=(a: Rational, b: Int64): Bool operator #

      Whether a comes after b or is equal to it.

    • Operator >=(a: Int64, b: Rational): Bool operator #

      Whether a comes after b or is equal to it.

    • Operator +(a: Rational, b: Rational): Rational operator #

      a plus b, exactly, in lowest terms.

    • Operator +(a: Rational, b: Int64): Rational operator #

      a plus b.

    • Operator +(a: Int64, b: Rational): Rational operator #

      a plus b.

    • Operator -(a: Rational, b: Rational): Rational operator #

      a minus b, exactly, in lowest terms.

    • Operator -(a: Rational, b: Int64): Rational operator #

      a minus b.

    • Operator -(a: Int64, b: Rational): Rational operator #

      a minus b.

    • Operator *(a: Rational, b: Rational): Rational operator #

      a times b, exactly, in lowest terms.

    • Operator *(a: Rational, b: Int64): Rational operator #

      a times b.

    • Operator *(a: Int64, b: Rational): Rational operator #

      a times b.

    • @PanicsAtCaller Operator /(a: Rational, b: Rational): Rational operator #

      a divided by b, exactly, in lowest terms. A b of 0 panics.

    • @PanicsAtCaller Operator /(a: Rational, b: Int64): Rational operator #

      a divided by b.

    • @PanicsAtCaller Operator /(a: Int64, b: Rational): Rational operator #

      a divided by b.

    • @PanicsAtCaller Operator **(a: Rational, b: Int64): Rational operator #

      To a whole power, which can be below 0, as 2/3 to -2 is 9/4. Anything to 0 is 1, and 0 to a power below 0 panics.

    • Operator -(a: Rational): Rational operator #

      a negated.

    Report an issue with this page or with FutureBlitz on GitHub.