Builds the Rational from two Integers, with nothing known about either.
Integerthe numeratorIntegerthe denominatorReturns the Rational, or nothing when the denominator is zero.
Exact fractions in lowest terms: arithmetic, rounding, and the forms a Rational prints in.
Taught on: Method calls and Numbers.
namespace Rational for Rationaltype NonZeroRational = Rational where @::isNot(0/1)The Rationals that are not zero, as a checked refinement of Rational, and the sister of NonZeroInteger.type NonNegativeRational = Rational where @::isGreaterThanOrEqualTo(0/1)The Rationals from zero upward, as a checked refinement of Rational.type PositiveRational = Rational where @::isPositive()The Rationals above zero, as a checked refinement of Rational.NumberFormatThe forms Rational::toString writes: #Fraction is "3/4", #Decimal is "0.75", #Percent is "75%" and #Scientific is "7.5e-1".RoundingWhich step round reaches for a value that is not already on one.SignStyleWhether toString(showingSign:) writes a sign in front of a value that is not negative.on Rational
Builds a Rational from one Integer over another.
This is the way to write a ratio of computed values, where the literal form 3/4 is not available. A zero denominator answers empty, and the defaultingTo: entry answers the given Rational instead.
Builds the Rational from two Integers, with nothing known about either.
Integerthe numeratorIntegerthe denominatorReturns the Rational, or nothing when the denominator is zero.
Builds the Rational over a denominator proven not to be zero.
There is no failure to report, so the Rational itself is the answer.
Returns the Rational.
Builds the Rational from two Integers, with a value to answer when the denominator is zero.
Integerthe numeratorIntegerthe denominatorRationalthe value to answer with when there is no RationalReturns the Rational, or the given value in its place.
on Rational
Reads a Rational from its text form.
The text is a fraction like 3/4, a decimal like 0.75, or a whole number like 3. Each form takes an optional sign in front, and an underscore between two digits, which is the notation the language writes. Text of another shape, or a fraction over zero, answers empty, and the defaultingTo: entry answers the given Rational instead.
Stringthe text to readReturns the Rational, or nothing when the text has any other shape or divides by zero.
Reads a Rational from its text form, with a value to answer when the text has another shape.
Stringthe text to readRationalthe value to answer with when the text is no RationalReturns the Rational, or the given value in its place.
on Rational
Answers whether the Rational has the same value as a number.
The comparison is in lowest terms, so 1/2 equals 2/4. A Rational equals an Integer when it is whole and its numerator is that Integer.
Answers whether the Rational has the same value as another Rational.
Rationalthe Rational to compare againstReturns true when both are equal.
Answers whether the Rational has the same value as an Integer.
Integerthe Integer to compare againstReturns true when the Rational is whole and its numerator is that Integer.
on Rational
Answers whether the Rational differs from a number.
A Rational that is not whole differs from every Integer.
Answers whether the Rational differs from another Rational.
Rationalthe Rational to compare againstReturns true when the two differ.
Answers whether the Rational differs from an Integer.
Integerthe Integer to compare againstReturns true when the two differ.
on Rational
Orders the Rational against another Rational.
Rationalthe Rational to order againstReturns Ordering#Less, Ordering#Equal or Ordering#Greater.
on Rational
Answers the Rational as a String, in lowest terms.
The form is 3/4 when no format is named, and the named format otherwise. A whole Rational prints its numerator alone, so 1/2::add(1/2) prints 1 and 10::divide(by 2) prints 5. The Rational.parse Method reads the fraction form and the decimal form. The fraction form reads back as the same Rational. A decimal form does too when its expansion ends within 80 digits, and reads back as the rounded value otherwise. A count of one or more places writes exactly that many digits after the point.
Returns the String representation of the Rational.
Answers the Rational as a fraction, in lowest terms.
The form is 3/4. A whole Rational prints its numerator alone, so 1/2::add(1/2) prints 1.
Returns the String representation of the Rational.
Answers the Rational in the named format.
The #Decimal format writes the expansion, to at most 80 digits, and the #Fraction format writes 3/4. A whole Rational prints its numerator alone in either. The #Percent format writes that same expansion of one hundred times the value, with a % after it. The #Scientific format writes one digit before the point and the power of ten as an exponent, so 1234 is 1.234e3 and zero is 0e0.
Returns the String representation of the Rational.
Answers the Rational in the named format, with exactly that many places.
The digits are padded with zeroes where the expansion is shorter, so 1/2 over two places is 0.50. The last digit kept is rounded in the named direction, and #Nearest is what a call that names none is given, so 1/8 is 0.13. A count below one rounds to a whole number, and no point is written. The count has no ceiling. The #Percent and #Scientific formats take the count the same way, so 1/8 over one place is 12.5% and 1234 over two is 1.23e3. The #Fraction format ignores both the count and the direction.
Integerhow many digits to write after the pointReturns the String representation of the Rational.
Answers the Rational in the named format, with a separator between the digit groups.
The separator goes between every three digits before the point, counted from the right, so 1234567 is 1,234,567 under a comma. The scientific form writes one digit before the point and takes no separator. The fraction form groups the numerator and the denominator alike.
Stringthe text to write between the digit groupsReturns the String representation of the Rational.
Answers the Rational in the named format, with that many places and a separator between the digit groups.
The count and the direction reach the format exactly as they do without a separator, so 1234567/1 over two places is 1,234,567.00 under a comma. Only the digits before the point are grouped.
Integerhow many digits to write after the pointStringthe text to write between the digit groupsReturns the String representation of the Rational.
Answers the Rational as a fraction, with its sign written in the named style.
The #Always Case writes a + in front of a value that is not negative, so 3/4 is +3/4 and zero is +0. The #Negative Case writes what the plain toString() writes.
Returns the String representation of the Rational.
on Rational
Adds a number to the Rational, and stays exact for every member of the numeric tower.
Rationalthe Rational to addReturns the sum.
Adds an Integer to the Rational.
The sum is a Rational, since a whole number added to a fraction need not be whole.
Integerthe Integer to addReturns the sum, as a Rational.
Adds an Algebraic to the Rational.
The sum is an Algebraic, since adding a Rational leaves the radical part in place.
Algebraicthe Algebraic to addReturns the sum, as an Algebraic.
Adds a Transcendental to the Rational.
The sum is a Transcendental, since adding a Rational leaves every base term in place.
Transcendentalthe Transcendental to addReturns the sum, as a Transcendental.
on NonNegativeRational
Adds a Rational proven not to be negative to this NonNegativeRational.
A sum of two non-negative Rationals is never negative, and a positive summand makes it positive.
Adds a Rational proven to be positive.
The receiver is not negative and the other summand is above zero, so the sum is above zero.
Returns the sum, which is above zero.
Adds a Rational proven not to be negative.
Neither summand is negative, so the sum is not negative either.
Returns the sum, which is never negative.
on PositiveRational
Adds a Rational proven not to be negative to this PositiveRational.
The receiver is above zero and the other summand is not below it, so the sum is above zero.
Returns the sum, which is above zero.
on Rational
Subtracts a number from the Rational, and stays exact for every member of the numeric tower.
Rationalthe Rational to subtractReturns the difference.
Subtracts an Integer from the Rational.
The difference is a Rational, since a whole number taken from a fraction need not be whole.
Integerthe Integer to subtractReturns the difference, as a Rational.
Subtracts an Algebraic from the Rational.
The difference is an Algebraic, since the radical part stays and only its sign flips.
Algebraicthe Algebraic to subtractReturns the difference, as an Algebraic.
Subtracts a Transcendental from the Rational.
The difference is a Transcendental, since every base term stays with its sign flipped.
Transcendentalthe Transcendental to subtractReturns the difference, as a Transcendental.
on Rational
Multiplies the Rational with a number, and stays exact for every member of the numeric tower.
Rationalthe Rational to multiply withReturns the product.
Multiplies the Rational with an Integer.
The product is a Rational, since scaling a fraction by a whole number need not leave a whole number.
Integerthe Integer to multiply withReturns the product, as a Rational.
Multiplies the Rational with an Algebraic.
The product is an Algebraic, since scaling keeps the radical part. A zero receiver answers zero, which is a Rational.
Algebraicthe Algebraic to multiply withReturns the product, as an Algebraic or a Rational.
Multiplies the Rational with a Transcendental.
The product is a Transcendental, since scaling keeps every base term. A zero receiver answers zero, which is a Rational.
Transcendentalthe Transcendental to multiply withReturns the product, as a Transcendental or a Rational.
on NonZeroRational
Multiplies this NonZeroRational with another.
The product is never zero, so the answer is a NonZeroRational too.
Returns the product, which is not zero.
on NonNegativeRational
Multiplies this NonNegativeRational with a Rational proven not to be negative.
Neither factor is negative, so the product is not negative either.
Returns the product, which is never negative.
on PositiveRational
Multiplies this PositiveRational with another.
Both factors are above zero, so the product is above zero.
Returns the product, which is above zero.
on Rational
Divides the Rational by a number, exactly.
Dividing by an Integer or a Rational answers empty for a zero divisor. With defaultingTo, the given value stands in place of empty. Dividing by an Algebraic always answers, because an Algebraic is irrational and so never zero. Dividing by an Integer proven not to be zero always answers too. A Rational proven not to be zero answers as well.
Rationalthe Rational to divide byReturns the quotient, or nothing when the divisor is zero.
Divides the Rational by an Integer.
The quotient is a Rational. A zero divisor answers empty, and the defaultingTo: entry answers the given Rational instead.
Integerthe Integer to divide byReturns the quotient, or nothing when the divisor is zero.
Divides the Rational by an Algebraic.
An Algebraic is irrational and so never zero, which is why the quotient itself is the answer. The quotient is an Algebraic, and a zero receiver answers zero, which is a Rational.
Algebraicthe Algebraic to divide byReturns the quotient, as an Algebraic or a Rational.
Divides by a Rational, and answers the given value when the divisor is zero.
Rationalthe divisorRationalthe value to answer with when there is no quotientReturns the quotient, or the given value in its place.
Divides by an Integer, and answers the given value when the divisor is zero.
Integerthe divisorRationalthe value to answer with when there is no quotientReturns the quotient, or the given value in its place.
Divides by an Integer proven not to be zero.
There is no failure to report, so the quotient itself is the answer.
Returns the quotient.
Divides by a Rational proven not to be zero.
There is no failure to report, so the quotient itself is the answer.
Returns the quotient.
on Rational
Answers what is left over after taking out every whole divisor that fits.
The division is Euclidean, so the remainder is never negative and always below the divisor's magnitude. For example, 7/2::remainder(dividingBy 1/3) is 1/6. A zero divisor answers empty, and the defaultingTo: entry answers the given Rational instead.
Answers the remainder over a divisor nothing is known about.
Rationalthe divisorReturns the remainder, or nothing when dividing by zero.
Answers the remainder over a divisor proven not to be zero.
The division can not fail, so the answer is the remainder itself rather than an Optional.
Returns the remainder.
Answers the remainder, with a value to answer when the divisor is zero.
Rationalthe divisorRationalthe value to answer with when there is no remainderReturns the remainder, or the given value in its place.
on Rational
Answers how many whole divisors fit.
This is the other half of the same Euclidean division as remainder, and the two agree: quotient · divisor + remainder is the original Rational. For example, 7/2::quotient(dividingBy 1/3) is 10. A zero divisor answers empty, and the defaultingTo: entry answers the given Integer instead.
Answers the quotient over a divisor nothing is known about.
Rationalthe divisorReturns the quotient, or nothing when dividing by zero.
Answers the quotient over a divisor proven not to be zero.
The division can not fail, so the answer is the quotient itself rather than an Optional.
Returns the quotient.
Answers the quotient, with a value to answer when the divisor is zero.
Rationalthe divisorIntegerthe value to answer with when there is no quotientReturns the quotient, or the given value in its place.
on Rational
Raises the Rational to the given power.
A negative exponent answers the exact reciprocal power. Zero raised to the power of zero is one. Raising this Rational to an exponent proven not to be negative can not fail. Zero raised to a negative power answers empty, and the defaultingTo: entry answers the given Rational instead.
Integerthe exponentReturns the power, or nothing when raising zero to a negative power.
Raises the Rational to the given power, and answers the given value when there is no power.
Integerthe exponentRationalthe value to answer with when there is no powerReturns the power, or the given value in its place.
Raises the Rational to an exponent proven not to be negative.
Every Rational has such a power, the reciprocal is never taken, and no denominator can reach zero. So the answer is the power itself rather than an Optional.
Returns the power.
on Rational
Answers the exact square root of the Rational.
A perfect square answers a Rational. Any other value that is not negative answers an exact Algebraic. A negative Rational answers empty, and the defaultingTo: entry answers the given value instead.
Returns the root, or nothing for a negative Rational.
Answers the exact square root, with a value to answer for a negative Rational.
Rational | Algebraicthe value to answer with when there is no rootReturns the root, or the given value in its place.
on NonNegativeRational
Answers the exact square root of this NonNegativeRational.
A negative Rational is the one with no real root, and the receiver is proven not to be one. So the answer is the root itself rather than an Optional. A ratio of two perfect squares answers a Rational, and every other value answers an exact Algebraic.
Returns the root.
on PositiveRational
Answers the exact square root of this PositiveRational.
A ratio of two perfect squares above zero has a root above zero, and every other value answers an exact Algebraic.
Returns the root.
on Rational
Answers whether the Rational is strictly below the given number.
Answers whether the Rational is strictly below another Rational.
The comparison is in lowest terms, so 1/2 is not below 2/4.
Rationalthe Rational to compare againstReturns true when the receiver is below the other Rational.
Answers whether the Rational is strictly below an Integer.
The Integer is read as n/1, so 1/2 is below 1.
Integerthe Integer to compare againstReturns true when the Rational is below the Integer.
on Rational
Answers whether the Rational is below the given number, or equal to it.
Answers whether the Rational is below another Rational, or equal to it.
Equal Rationals answer true.
The comparison is in lowest terms, so 1/2 answers true against 2/4.
Rationalthe Rational to compare againstReturns true when the receiver is below the other Rational or equal to it.
Answers whether the Rational is below an Integer, or equal to it.
The Integer is read as n/1, so 2/2 answers true against 1.
Integerthe Integer to compare againstReturns true when the Rational is below the Integer or equal to it.
on Rational
Answers whether the Rational is strictly above the given number.
Answers whether the Rational is strictly above another Rational.
The comparison is in lowest terms, so 2/4 is not above 1/2.
Rationalthe Rational to compare againstReturns true when the receiver is above the other Rational.
Answers whether the Rational is strictly above an Integer.
The Integer is read as n/1, so 3/2 is above 1.
Integerthe Integer to compare againstReturns true when the Rational is above the Integer.
on Rational
Answers whether the Rational is above the given number, or equal to it.
Answers whether the Rational is above another Rational, or equal to it.
Equal Rationals answer true.
The comparison is in lowest terms, so 2/4 answers true against 1/2.
Rationalthe Rational to compare againstReturns true when the receiver is above the other Rational or equal to it.
Answers whether the Rational is above an Integer, or equal to it.
The Integer is read as n/1, so 2/2 answers true against 1.
Integerthe Integer to compare againstReturns true when the Rational is above the Integer or equal to it.
on Rational · provided by Orderable
Answers whether the value lies between the two given ones, both included.
The two bounds name the same range in either order: 7::isBetween(10, and 1) is true, and 15::isBetween(10, and 1) is false.
Rationalone bound of the range, includedRationalthe other bound of the range, includedReturns true when the value is within the bounds.
on Rational · provided by Orderable
Answers the value, pulled into the given bounds.
The answer is the lower bound when the value is below it. It is the upper bound when the value is above it, and the value itself otherwise. The two bounds name the same range in either order: 7::clamp(between 10, and 1) is 7, and 15::clamp(between 10, and 1) is 10.
Rationalone bound of the rangeRationalthe other bound of the rangeReturns the clamped value.
on Rational
Answers whether the Rational is a whole number.
A whole number has the denominator one in lowest terms.
on Rational
Answers whether the Rational is above zero.
Zero is neither positive nor negative.
on Rational
Answers whether the Rational is below zero.
Zero is neither positive nor negative.
on Rational
Answers whether the Rational is exactly zero.
on Rational
Answers the numerator of the Rational in lowest terms.
The numerator carries the sign, because the denominator is always positive.
on NonZeroRational
Answers the numerator of this NonZeroRational in lowest terms.
A Rational is zero exactly when its numerator is, and the receiver is proven not to be zero. So the answer is a NonZeroInteger.
Returns the numerator, which is not zero.
on Rational
Answers the denominator of the Rational in lowest terms.
The denominator is always positive, and never zero. So the answer is a PositiveInteger, which fits every position that asks for a NonZeroInteger or a NonNegativeInteger.
on Rational
Answers the Rational without its sign, which is its distance from zero.
Returns the distance, which is never negative.
on NonZeroRational
Answers this NonZeroRational without its sign, which is its distance from zero.
The receiver is not zero, so its distance from zero is above zero. So the answer is a PositiveRational.
Returns the distance, which is above zero.
on Rational
Answers the Rational with its sign flipped.
on NonZeroRational
Answers this NonZeroRational with its sign flipped.
Negation keeps a value away from zero, so the answer is a NonZeroRational too.
Returns the negation, which is not zero.
on Rational
Answers the reciprocal of the Rational.
The reciprocal exchanges the numerator and the denominator. Zero has no reciprocal, and the defaultingTo: entry answers the given Rational instead. A receiver proven not to be zero answers the reciprocal itself.
Returns the reciprocal, or nothing for zero.
Answers the reciprocal, with a value to answer for zero.
Rationalthe value to answer with when there is no reciprocalReturns the reciprocal, or the given value in its place.
on NonZeroRational
Answers the reciprocal of this NonZeroRational.
Zero is the one Rational with no reciprocal, and the receiver is proven not to be one. So the answer is the reciprocal itself rather than an Optional. Exchanging the two parts of a value that is not zero leaves a value that is not zero, so the answer is proven as well.
Returns the reciprocal, which is not zero.
on Rational
Answers the Rational rounded in the named direction.
The direction is #Nearest when a call names none. A value exactly halfway between two steps rounds away from zero, so 1/2 answers 1 and -1/2 answers -1. Under #NearestEven a value exactly halfway between two steps reaches the even one, so 1/2 answers 0 and 3/2 answers 2. The other directions answer the floor for #Down, the ceiling for #Up, and the value towards zero for #TowardZero. Naming a count of places rounds to a decimal grid of that width instead, and answers a Rational.
Answers the Rational as an Integer, rounded in the named direction.
Returns the rounded Integer.
Answers the Rational rounded to a decimal grid of the given width.
Two places round to hundredths, so 5/3 answers 167/100. The answer is exact, and it is a Rational rather than text: toString(as #Decimal, toPlaces 2) is what writes 1.67. A count below one rounds to a whole number, answered as a Rational. The count has no ceiling, as it has none on toString.
Integerhow many decimal places to keepReturns the rounded Rational.
on Rational
Answers the Rational read off a decimal grid of the given width.
A Rational is exact, so the answer is the same one round(toPlaces:toward:) gives: two places answer hundredths, and 5/3 answers 167/100. The entries of this name on Algebraic and on Transcendental are what make an irrational readable as a number. This one is what lets a Number receiver reach the family.
Returns the Rational on that grid.
on Rational
Answers the Rational as a Rational.
A Rational is one already, so the answer is the receiver itself. The entry is what lets a Scalar receiver reach the crossing that an Integer receiver reaches.
Returns the receiver itself.
Each type below is a checked refinement: a type with a condition. A value has the type only once the compiler knows the condition holds, and Checked refinements shows how it finds out. Under each one, Tighter here lists the methods above that return a more precise type when called on it. Returned by lists the methods that return it.
The Rationals that are not zero, as a checked refinement of Rational, and the sister of NonZeroInteger.
The proof is what lets reciprocal answer a Rational rather than an Optional. The numerator is a NonZeroInteger, and absolute, negate and multiplication answer one of these again.
Tighter here multiply, numerator, absolute, negate, reciprocal
Returned by NonZeroInteger::divide(by:), NonZeroRational::multiply(with:), NonZeroRational::reciprocal(), NonZeroRational::negate()
The Rationals from zero upward, as a checked refinement of Rational.
A receiver proven to be one of these has a real square root, which the bare squareRoot answers an Optional for. Addition and multiplication stay within the proof.
Tighter here add, multiply, squareRoot
Returned by IntegerList::variance(), IntegerList::variance(defaultingTo:), RationalList::variance(), RationalList::variance(defaultingTo:), NumberList::variance(), NumberList::variance(defaultingTo:), NonEmptyIntegerList::variance(), NonEmptyRationalList::variance(), NonEmptyNumberList::variance(), Rational::absolute(), NonNegativeRational::add(_:), NonNegativeRational::multiply(with:)
The Rationals above zero, as a checked refinement of Rational.
A value above zero is neither zero nor below it, so one of these is accepted wherever a NonZeroRational or a NonNegativeRational is wanted. Addition with a value that is not negative, and multiplication with another of these, answer one of these again.
Tighter here add, multiply, squareRoot
Returned by NonZeroRational::absolute(), NonNegativeRational::add(_:), PositiveRational::add(_:), PositiveRational::multiply(with:), PositiveRational::squareRoot()
Each mode below is a choice whose cases carry no payload. You pass one to a method to choose how the method works. Every mode has is, isNot and toString, and cases() on its name lists its cases, as in NumberFormat.cases(). None of them is written out for a mode: Protocols says where each one comes from.
The forms Rational::toString writes: #Fraction is "3/4", #Decimal is "0.75", #Percent is "75%" and #Scientific is "7.5e-1".
The plain toString() entry writes the fraction form, and the as: entries name one of the four. The scientific form writes one digit before the point, and the power of ten it took out as e and an exponent. Only the fraction form and the decimal form read back through Rational.parse.
Cases #Fraction, #Decimal, #Percent, #Scientific
Taken by
Algebraic::toString(as:)Algebraic::toString(as:toPlaces:toward:)Integer::toString(as:)Integer::toString(as:toPlaces:toward:)Integer::toString(as:groupingWith:)Integer::toString(as:toPlaces:groupingWith:toward:)Rational::toString(as:)Rational::toString(as:toPlaces:toward:)Rational::toString(as:groupingWith:)Rational::toString(as:toPlaces:groupingWith:toward:)Transcendental::toString(as:)Transcendental::toString(as:toPlaces:toward:)Which step round reaches for a value that is not already on one.
#Nearest goes to the closest step, and away from zero for a value exactly halfway between two. The #NearestEven Case goes to the closest step as well. A value exactly halfway between two steps reaches the even one of them. The #Down Case answers the floor, #Up the ceiling, and #TowardZero the step towards zero. A call that names no rounding gets #Nearest.
Cases #Nearest, #NearestEven, #Down, #Up, #TowardZero
Taken by
Algebraic::toString(as:toPlaces:toward:), default #NearestAlgebraic::approximate(toPlaces:toward:), default #NearestAlgebraic::round(toward:), default #NearestAlgebraic::round(toPlaces:toward:), default #NearestInteger::toString(as:toPlaces:toward:), default #NearestInteger::toString(as:toPlaces:groupingWith:toward:), default #NearestInteger::quotient(dividingBy:toward:)Integer::round(toward:), default #NearestInteger::round(toPlaces:toward:), default #NearestInteger::approximate(toPlaces:toward:), default #NearestRational::toString(as:toPlaces:toward:), default #NearestRational::toString(as:toPlaces:groupingWith:toward:), default #NearestRational::round(toward:), default #NearestRational::round(toPlaces:toward:), default #NearestRational::approximate(toPlaces:toward:), default #NearestTranscendental::toString(as:toPlaces:toward:), default #NearestTranscendental::approximate(toPlaces:toward:), default #NearestTranscendental::round(toward:), default #NearestTranscendental::round(toPlaces:toward:), default #NearestWhether toString(showingSign:) writes a sign in front of a value that is not negative.
A negative value is written with its minus sign under both Cases. Under #Always a value that is not negative is written with a + in front, and zero is +0. A call that names #Negative gets what the plain toString() writes.
Cases #Negative, #Always
Taken by