Companion object to Rational. Represents a Rational number, with various methods for conversion, approximation, and utility. Provides a rich set of operations and utilities to handle rational numbers, convert data types, and manage normalization. Rational numbers are represented as fractions with BigInt numerator and denominator.
Fields:
- bigZero: Commonly used representation for Rational zero.
- bigOne: Commonly used representation for Rational one.
- bigTwo: Commonly used representation for Rational two.
- bigThree: Commonly used representation for Rational three.
- bigFour: Commonly used representation for Rational four.
- bigFive: Commonly used representation for Rational five.
- bigSeven: Commonly used representation for Rational seven.
- bigNegOne: Commonly used representation for Rational negative one.
- bigTen: Commonly used representation for Rational ten.
- zero: Rational representation for zero.
- infinity: Positive infinity in Rational form.
- negInfinity: Negative infinity in Rational form.
- one: Rational representation for one.
- ten: Rational representation for ten.
- two: Rational representation for two.
- half: Rational representation for one-half.
- NaN: Represents "Not a Number" in Rational form.
- negZero: Represents negative zero in Rational form.
- negZeroDouble: Represents negative zero as a Double in Rational form.
- pi_5000: Predefined Rational representation of π/5000.
- squareRoots: A collection of predefined square root values in Rational form.
Attributes
Members list
Type members
Classlikes
Implicit object which bestows all the Ordering, Numeric, etc. functionality to a Rational.
Implicit object which bestows all the Ordering, Numeric, etc. functionality to a Rational.
Attributes
- Supertypes
-
trait RationalIsFractionaltrait Fractional[Rational]trait RationalIsNumerictrait Numeric[Rational]trait RationalIsOrderingtrait Ordering[Rational]trait PartialOrdering[Rational]trait Equiv[Rational]trait Serializabletrait Comparator[Rational]class Objecttrait Matchableclass AnyShow all
- Self type
-
RationalFractional.type
Implicit class RationalHelper to allow definition of Rationals by Strings of the form r"n/d".
Implicit class RationalHelper to allow definition of Rationals by Strings of the form r"n/d".
Value parameters
- sc
-
a StringContext.
Attributes
- Supertypes
-
class AnyValtrait Matchableclass Any
Trait defined to support the methods of Fractional[Rational].
Trait defined to support the methods of Fractional[Rational].
Attributes
- Supertypes
-
trait Fractional[Rational]trait RationalIsNumerictrait Numeric[Rational]trait RationalIsOrderingtrait Ordering[Rational]trait PartialOrdering[Rational]trait Equiv[Rational]trait Serializabletrait Comparator[Rational]class Objecttrait Matchableclass AnyShow all
- Known subtypes
-
object RationalFractional
Trait defined to support the methods of Numeric[Rational].
Trait defined to support the methods of Numeric[Rational].
Attributes
- Supertypes
- Known subtypes
-
trait RationalIsFractionalobject RationalFractional
Trait defined to support the methods of Ordering[Rational].
Trait defined to support the methods of Ordering[Rational].
Attributes
- Supertypes
- Known subtypes
Implicit class RationalOps to allow an Int to be treated as a Rational for the purpose of allowing operations with right-hand Rational arguments or for division by an Int. A particular case is rational division where we must convert the numerator to Rational so that we don't end up with 0 as the result of an Int division.
Implicit class RationalOps to allow an Int to be treated as a Rational for the purpose of allowing operations with right-hand Rational arguments or for division by an Int. A particular case is rational division where we must convert the numerator to Rational so that we don't end up with 0 as the result of an Int division.
Value parameters
- x
-
an Int
Attributes
- Supertypes
-
class Objecttrait Matchableclass Any
Inherited and Abstract types
The names of the product elements.
The names of the product elements.
Attributes
- Inherited from:
- Mirror
The name of the type.
The name of the type.
Attributes
- Inherited from:
- Mirror
Value members
Concrete methods
Method to construct a Rational from two BigInt values. NOTE: the this method ensures that the numerator and denominator are normalized.
Method to construct a Rational from two BigInt values. NOTE: the this method ensures that the numerator and denominator are normalized.
Value parameters
- d
-
the denominator.
- n
-
the numerator.
Attributes
- Returns
-
a Rational with the same ratio as n/d.
Method to construct a Rational from a BigInt numerator and a Long denominator. NOTE: the this method ensures that the numerator and denominator are normalized.
Method to construct a Rational from a BigInt numerator and a Long denominator. NOTE: the this method ensures that the numerator and denominator are normalized.
Value parameters
- d
-
the denominator.
- n
-
the numerator.
Attributes
- Returns
-
a Rational with the same ratio as n/d.
Creates a Rational number from the given numerator and denominator values.
Creates a Rational number from the given numerator and denominator values.
Value parameters
- d
-
the denominator of the rational number
- n
-
the numerator of the rational number
Attributes
- Returns
-
a Rational number representing the fraction n/d
Method to construct a Rational from a BigInt.
Method to construct a Rational from a BigInt.
Value parameters
- n
-
the value.
Attributes
- Returns
-
a Rational with the same value as n.
Method to construct a Rational from a BigInt with sign defined by "negative".
Method to construct a Rational from a BigInt with sign defined by "negative".
Value parameters
- n
-
the value.
- negative
-
if true then the sign of the result will be flipped.
Attributes
- Returns
-
a Rational with the same value as n.
Method to construct a Rational from a Long.
Method to construct a Rational from a Long.
Value parameters
- n
-
the value.
Attributes
- Returns
-
a Rational with the same value as n.
Method to construct a Rational from a Long.
Method to construct a Rational from a Long.
Value parameters
- n
-
the value.
Attributes
- Returns
-
a Rational with the same value as n.
Method to construct a Rational based on a BigDecimal.
Method to construct a Rational based on a BigDecimal.
Value parameters
- x
-
the BigDecimal to convert.
Attributes
- Returns
-
a Rational which is equal to x.
- Note
-
Throws RationalException if x cannot be represented as a Rational.
Method to construct a Rational based on a String. NOTE: this method is NOT safe. It is much better to invoke parse(w).
Method to construct a Rational based on a String. NOTE: this method is NOT safe. It is much better to invoke parse(w).
Value parameters
- w
-
the String value.
Attributes
- Returns
-
a Rational corresponding to the value given by w.
- Note
-
Throws RationalParserException if w is malformed.
Creates a new Rational object by applying the given tuple consisting of numerator and denominator.
Creates a new Rational object by applying the given tuple consisting of numerator and denominator.
Value parameters
- t
-
a tuple where the first element is the numerator and the second element is the denominator
Attributes
- Returns
-
a new Rational object constructed from the provided numerator and denominator
Method to take a Double in the range 0 thru 1 and approximate it by a Rational number to within the given tolerance (epsilon).
Method to take a Double in the range 0 thru 1 and approximate it by a Rational number to within the given tolerance (epsilon).
NOTE: this Farey sequence converges very slowly. In order to put an absolute limit on the recursion (which otherwise might never terminate), we limit the size of the denominator of r1 to be 10E12. It shouldn't matter much whether we test r1 or r2 in this context.
Value parameters
- epsilon
-
(implicit) the tolerance.
- x
-
the value to approximate (should be between 0 and 1).
Attributes
- Returns
-
a Rational such that the difference between the result and x is less than epsilon.
- Note
-
Throws java.lang.IllegalArgumentException if x is not between 0 and 1.
Method to approximate an irrational number as a Rational. Application code should always call this method or, preferably, Rational.apply(Double).
Method to approximate an irrational number as a Rational. Application code should always call this method or, preferably, Rational.apply(Double).
NOTE: this method is designed for true Doubles, not floating-point representations of decimal numbers. Such decimal numbers should be converted to BigDecimal first using BigDecimal.valueOf(x).
CONSIDER what we need here is to pass in a function and a desired Rational result, and perhaps even the first derivative of the function, so that we can approximate the solution (root) of the function.
Value parameters
- epsilon
-
(implicit) the tolerance.
- x
-
the value to approximate.
Attributes
- Returns
-
a Rational such that the difference between the result and x is less than epsilon.
converter from Double to Rational.
converter from Double to Rational.
Value parameters
- x
-
the value.
Attributes
- Returns
-
a Rational equal to or approximately equal to x.
- Note
-
Throws java.util.NoSuchElementException because we invoke get on an Option[Rational].
Creates an exact Rational representation of the given Double value if possible.
Creates an exact Rational representation of the given Double value if possible.
Value parameters
- x
-
the
Doublevalue to be converted to an exactRational
Attributes
- Returns
-
a
Trycontaining the exactRationalif the conversion is possible and exact, or aFailurecontaining anArithmeticExceptionif the conversion is not possible or loses precision
Method to yield a Rational exponent (in the sense of the a literal Double: for example 1.0Ex).
Method to yield a Rational exponent (in the sense of the a literal Double: for example 1.0Ex).
Value parameters
- x
-
the power to which we should raise 10.
Attributes
- Returns
-
10 raised to the power x, expressed as a Rational.
Generate the String representation of n/d where d is a Prime number (at least, very probably).
Generate the String representation of n/d where d is a Prime number (at least, very probably).
Value parameters
- d
-
the (prime) denominator.
- n
-
the numerator.
Attributes
- Returns
-
a String of repeated digits.
Narrows a given BigInt value to ensure it falls within a specified range. If the value is outside the range, a Failure is returned with a RationalException.
Narrows a given BigInt value to ensure it falls within a specified range. If the value is outside the range, a Failure is returned with a RationalException.
Value parameters
- max
-
the maximum allowed value (inclusive)
- min
-
the minimum allowed value (inclusive)
- x
-
the
BigIntvalue to be narrowed
Attributes
- Returns
-
a
Successcontaining the value if it falls within the range, or aFailurewith aRationalExceptionin case of precision loss
Method to construct a Try[Rational] based on a String.
Method to construct a Try[Rational] based on a String.
Value parameters
- w
-
the String value.
Attributes
- Returns
-
either Success(Rational) with value corresponding to the value given by w or Failure(RationalParserException) if w is malformed.
Method to get the (integral) xth root of b.
Method to get the (integral) xth root of b.
Value parameters
- b
-
a BigInt.
- x
-
an Int.
Attributes
- Returns
-
an optional BigInt which, when raised to the power of x, equals b.
This method gets an Int from a Rational.
This method gets an Int from a Rational.
XXX: Needs to be public for testing
Value parameters
- x
-
a Rational.
Attributes
- Returns
-
an Try[Int]
This method gets an optional Int from a Rational.
This method gets an optional Int from a Rational.
Value parameters
- x
-
a Rational.
Attributes
- Returns
-
an Option[Int]
Get Rational x as a pair of Ints (if possible). The result will be at full precision or an exception will be thrown.
Get Rational x as a pair of Ints (if possible). The result will be at full precision or an exception will be thrown.
Value parameters
- x
-
a Rational.
Attributes
- Returns
-
an optional tuple of Ints.
Attempts to convert a Rational value to a Long within the range of Long values.
Attempts to convert a Rational value to a Long within the range of Long values.
Value parameters
- x
-
the Rational value to convert to a Long
Attributes
- Returns
-
a Try containing the Long value if the conversion is successful, or a Failure if the value is out of range or cannot be converted
Get Rational x as a pair of Longs (if possible). The result will be at full precision or an exception will be thrown.
Get Rational x as a pair of Longs (if possible). The result will be at full precision or an exception will be thrown.
Value parameters
- x
-
a Rational.
Attributes
- Returns
-
an optional tuple of Longs.
Concrete fields
Represents a Not-a-Number (NaN) value as a rational number. It is created by initializing a Rational object with both numerator and denominator as zero. This is a special case and does not represent a valid rational number or a real number.
Represents a Not-a-Number (NaN) value as a rational number. It is created by initializing a Rational object with both numerator and denominator as zero. This is a special case and does not represent a valid rational number or a real number.
Attributes
A constant value representing the big integer 1.
A constant value representing the big integer 1.
Attributes
A lazily evaluated map that provides the base-2 logarithms of specific powers of 2. The keys represent powers of 2 (e.g., 1, 2, 4, 8, etc.), and the values are their corresponding base-2 logarithms.
A lazily evaluated map that provides the base-2 logarithms of specific powers of 2. The keys represent powers of 2 (e.g., 1, 2, 4, 8, etc.), and the values are their corresponding base-2 logarithms.
Attributes
A lazy value representing a map of predefined integers and their corresponding cube root values.
A lazy value representing a map of predefined integers and their corresponding cube root values.
This map contains key-value pairs where the key represents a non-negative integer and the value is its integer cube root. The cube root values are exact for perfect cubes included in the map.
Attributes
Represents the reciprocal (inverted form) of the five Rational number.
Represents the reciprocal (inverted form) of the five Rational number.
Attributes
Represents a Rational number with a value equivalent to five. The value is constructed using an underlying representation of bigFive.
Represents a Rational number with a value equivalent to five. The value is constructed using an underlying representation of bigFive.
Attributes
A constant value representing the rational number 4. This is created using the Rational class.
A constant value representing the rational number 4. This is created using the Rational class.
Attributes
A Rational number representing the reciprocal of two. The invert method on the two object is used to calculate this value.
A Rational number representing the reciprocal of two. The invert method on the two object is used to calculate this value.
Attributes
Represents a rational number with an infinite value, defined as the inversion of the zero rational number.
Represents a rational number with an infinite value, defined as the inversion of the zero rational number.
Attributes
Regular expression pattern to match an integer in string form, which can be positive or negative. The pattern ensures that the entire string represents a valid integer.
Regular expression pattern to match an integer in string form, which can be positive or negative. The pattern ensures that the entire string represents a valid integer.
Examples of matching strings:
- "123"
- "-456"
- "0"
Examples of non-matching strings:
- "12.3" (not an integer)
- "abc" (not numeric)
- "123abc" (contains invalid characters)
Attributes
A constant value representing the maximum square value that is encoded in squareRoots (above).
A constant value representing the maximum square value that is encoded in squareRoots (above).
Attributes
Represents the negation of the Rational value one. The result is a Rational object equivalent to -1.
Represents the negation of the Rational value one. The result is a Rational object equivalent to -1.
Attributes
Represents a Rational number with a numerator of 0 and a denominator of -1. Although mathematically this simplifies to zero, the negative denominator is retained in this instance. This is the only instance in the domain of Rational with a negative denominator.
Represents a Rational number with a numerator of 0 and a denominator of -1. Although mathematically this simplifies to zero, the negative denominator is retained in this instance. This is the only instance in the domain of Rational with a negative denominator.
Attributes
Represents a rational number instance initialized with the value 9.
Represents a rational number instance initialized with the value 9.
Attributes
A constant value representing the rational number equivalent to 1. It is defined as an instance of the Rational class initialized with the value of bigOne.
A constant value representing the rational number equivalent to 1. It is defined as an instance of the Rational class initialized with the value of bigOne.
Attributes
Represents a rational number equivalent to one-fourth by inverting the value of four.
Represents a rational number equivalent to one-fourth by inverting the value of four.
This val performs the inversion of a predefined rational number four, resulting in 1/4 or a quarter of a unit.
Attributes
A lazy value representing a map of predefined integers and their corresponding square root values.
A lazy value representing a map of predefined integers and their corresponding square root values.
This map contains key-value pairs where the key represents a non-negative integer and the value is its integer square root. The square root values are exact for perfect squares included in the map.
Attributes
Represents the rational number 10 as an instance of the Rational type. Utilizes the predefined value bigTen to create the Rational instance.
Represents the rational number 10 as an instance of the Rational type. Utilizes the predefined value bigTen to create the Rational instance.
Attributes
A Rational number representing the reciprocal of three. The invert method on the three object is used to calculate this value.
A Rational number representing the reciprocal of three. The invert method on the three object is used to calculate this value.
Attributes
Represents the rational number three as a constant of type Rational. It is constructed using Rational(bigThree).
Represents the rational number three as a constant of type Rational. It is constructed using Rational(bigThree).
Attributes
Represents the rational number two as a constant of type Rational. It is constructed using Rational(bigTwo), where bigTwo presumably signifies the numerical value 2 in a predefined or imported context.
Represents the rational number two as a constant of type Rational. It is constructed using Rational(bigTwo), where bigTwo presumably signifies the numerical value 2 in a predefined or imported context.
Attributes
A regular expression that matches a single Unicode character belonging to the specified ranges:
A regular expression that matches a single Unicode character belonging to the specified ranges:
- Superscript digits (⁰)
- Subscript digits (₉)
- Vulgar fractions (½ to ⅞)
Attributes
A predefined instance of the Rational class representing the value zero. This constant can be used as a default or as a comparison operand in operations involving rational numbers.
A predefined instance of the Rational class representing the value zero. This constant can be used as a default or as a comparison operand in operations involving rational numbers.
Attributes
Implicits
Implicits
Implicit class RationalHelper to allow definition of Rationals by Strings of the form r"n/d".
Implicit class RationalHelper to allow definition of Rationals by Strings of the form r"n/d".
Value parameters
- sc
-
a StringContext.
Attributes
Implicit class RationalOps to allow an Int to be treated as a Rational for the purpose of allowing operations with right-hand Rational arguments or for division by an Int. A particular case is rational division where we must convert the numerator to Rational so that we don't end up with 0 as the result of an Int division.
Implicit class RationalOps to allow an Int to be treated as a Rational for the purpose of allowing operations with right-hand Rational arguments or for division by an Int. A particular case is rational division where we must convert the numerator to Rational so that we don't end up with 0 as the result of an Int division.
Value parameters
- x
-
an Int
Attributes
Implicit converter from BigInt to Rational.
Implicit converter from BigInt to Rational.
Value parameters
- x
-
the value.
Attributes
- Returns
-
a Rational equal to x.
Implicit converter from Long to Rational.
Implicit converter from Long to Rational.
Value parameters
- x
-
the value.
Attributes
- Returns
-
a Rational equal to x.