Stringthe text to readReturns the Integer, or nothing when the text has any other shape.
Whole numbers of any size that never overflow: arithmetic, parsing, testing and printing.
Taught on: Method calls, Numbers and Checked refinements.
namespace Integer for Integertype NonZeroInteger = Integer where @::isNot(0)The Integers that are not zero, as a checked refinement of Integer.type NonNegativeInteger = Integer where @::isGreaterThanOrEqualTo(0)The Integers from zero upward, as a checked refinement of Integer.type PositiveInteger = Integer where @::isPositive()The Integers above zero, as a checked refinement of Integer.DivisionHow a division pairs its quotient with its remainder.on Integer
Reads an Integer from its text form.
The text form is the notation the language writes an Integer in, and a leading sign in front of it. It is digits, with an optional underscore between two digits. A leading + is read for text a Program was handed rather than wrote. Text of any other shape answers empty, and the defaultingTo: entry answers the given Integer instead.
Stringthe text to readReturns the Integer, or nothing when the text has any other shape.
Reads an Integer from its text form, with a value to answer when the text has another shape.
Stringthe text to readIntegerthe value to answer with when the text is no IntegerReturns the Integer, or the given value in its place.
Reads an Integer from its text form in the given base.
The text form is the one the entries above read, in digits of that base: a leading sign, and an optional underscore between two digits. A digit above nine is a letter, and a capital letter reads as its lowercase. Text of any other shape answers empty. A base below two is read as two, and a base above thirty-six as thirty-six.
Stringthe text to readIntegerthe base the digits are written inReturns the Integer, or nothing when the text has any other shape.
Reads an Integer from its text form in the given base, with a value to answer when the text has another shape.
Stringthe text to readIntegerthe base the digits are written inIntegerthe value to answer with when the text is no IntegerReturns the Integer, or the given value in its place.
on Integer
Answers whether this Integer has the same value as a number.
An Integer equals a Rational when that Rational is whole and its numerator is this Integer.
Answers whether this Integer has the same value as another Integer.
Integerthe Integer to compare againstReturns true when both are equal.
Answers whether this Integer has the same value as a Rational.
Rationalthe Rational to compare againstReturns true when both are equal.
on Integer
Answers whether this Integer differs from a number.
Every Integer differs from a Rational that is not whole.
Answers whether this Integer differs from another Integer.
Integerthe Integer to compare againstReturns true when the two differ.
Answers whether this Integer differs from a Rational.
Rationalthe Rational to compare againstReturns true when the two differ.
on Integer
Orders the Integer against another Integer.
Integerthe Integer to order againstReturns Ordering#Less, Ordering#Equal or Ordering#Greater.
on Integer
Answers the Integer as a String.
The plain entry writes the decimal digits, and the as: entries write what Rational writes for the same value.
Answers the Integer as a String, in decimal digits.
Returns the String representation of the Integer.
Answers the Integer in the named format.
The #Decimal and #Fraction formats both write the digits alone, since an Integer is whole. The #Percent format writes one hundred times the value, with a % after it. The #Scientific format writes one digit before the point, and the power of ten after an e.
Returns the String representation of the Integer.
Answers the Integer in the named format, with exactly that many places.
The places are written as zeroes for the #Decimal format, so 5 over two places is 5.00. The #Fraction format ignores both the count and the direction. The count and the direction reach the other two formats as they reach Rational.
Integerhow many digits to write after the pointReturns the String representation of the Integer.
Answers the Integer in the named format, with a separator between the digit groups.
The separator goes between every three digits, counted from the right, so 1234567 is 1,234,567 under a comma.
Stringthe text to write between the digit groupsReturns the String representation of the Integer.
Answers the Integer in the named format, with that many places and a separator between the digit groups.
Only the digits before the point are grouped, so 1234567 over two places is 1,234,567.00 under a comma.
Integerhow many digits to write after the pointStringthe text to write between the digit groupsReturns the String representation of the Integer.
Answers the Integer as a decimal scaled down by that many places.
The receiver is read as a count of the last place, so 1234 scaled by two places is 12.34. A count of zero writes the digits alone. The sign is written in front, so -1234 scaled by two places is -12.34.
Returns the String representation of the scaled Integer.
Answers the Integer with its sign written in the named style.
The #Always Case writes a + in front of a value that is not negative, so 3 is +3 and zero is +0. The #Negative Case writes what the plain toString() writes.
Returns the String representation of the Integer.
Answers the Integer in the given base.
A digit above nine is written as a lowercase letter, so 255 in base sixteen is ff. The sign is written in front. A base below two is read as two, and a base above thirty-six as thirty-six. Integer.parse(_ , inBase:) reads back what this writes.
Integerthe base to write the digits inReturns the String representation of the Integer.
on Integer
Adds a number to this Integer.
Adds an Integer to this Integer.
The sum is an Integer.
Integerthe Integer to addReturns the sum.
Adds a Rational to this Integer.
The sum is a Rational, since it need not be whole.
Rationalthe Rational to addReturns the sum.
Adds an Algebraic to this Integer.
The sum stays exact. Shifting the rational part of a + b·√d leaves the radical untouched.
Algebraicthe Algebraic to addReturns the exact sum.
Adds a Transcendental to this Integer.
The sum stays exact. Shifting the rational part of a + b·π + c·e leaves the base terms untouched.
Transcendentalthe Transcendental to addReturns the exact sum.
on NonNegativeInteger
Adds an Integer proven not to be negative to this NonNegativeInteger.
A sum of two non-negative Integers is never negative, and a positive summand makes it positive.
Adds an Integer 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 an Integer proven not to be negative.
Neither summand is negative, so the sum is not negative either.
Returns the sum, which is never negative.
on PositiveInteger
Adds an Integer proven not to be negative to this PositiveInteger.
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 Integer
Subtracts a number from this Integer, staying exact for every member of the numeric tower.
Subtracts an Integer from this Integer.
The difference is an Integer.
Integerthe Integer to subtractReturns the difference.
Subtracts a Rational from this Integer.
The difference is a Rational, since it need not be whole.
Rationalthe Rational to subtractReturns the difference.
Subtracts an Algebraic from this Integer.
The difference stays exact. Negating a + b·√d flips both parts and leaves the radical itself in place.
Algebraicthe Algebraic to subtractReturns the exact difference.
Subtracts a Transcendental from this Integer.
The difference stays exact. Negating a + b·π + c·e flips every part and leaves the base terms in place.
Transcendentalthe Transcendental to subtractReturns the exact difference.
on Integer
Multiplies this Integer with a number, staying exact for every member of the numeric tower.
Multiplies this Integer with another Integer.
The product is an Integer.
Integerthe Integer to multiply withReturns the product.
Multiplies this Integer with a Rational.
The product is a Rational, since it need not be whole.
Rationalthe Rational to multiply withReturns the product.
Multiplies this Integer with an Algebraic.
The product stays exact. A zero factor collapses it to the Rational zero, so the answer is a Union of both kinds.
Algebraicthe Algebraic to multiply withReturns the exact product, an Algebraic or a Rational.
Multiplies this Integer with a Transcendental.
The product stays exact. A zero factor collapses it to the Rational zero, so the answer is a Union of both kinds.
Transcendentalthe Transcendental to multiply withReturns the exact product, a Transcendental or a Rational.
on NonZeroInteger
Multiplies this NonZeroInteger with a number.
A product of two non-zero Integers is never zero. An irrational scaled by a non-zero Integer stays irrational, so those two entries answer the irrational kind itself.
Multiplies this NonZeroInteger with another.
The product is never zero, so the answer is a NonZeroInteger too.
Returns the product, which is not zero.
Multiplies this NonZeroInteger with an Algebraic.
The factor is proven, so the radical survives and the answer is an Algebraic rather than a Union.
Algebraicthe Algebraic to multiply withReturns the exact product.
Multiplies this NonZeroInteger with a Transcendental.
The factor is proven, so every base term survives and the answer is a Transcendental rather than a Union.
Transcendentalthe Transcendental to multiply withReturns the exact product.
on NonNegativeInteger
Multiplies this NonNegativeInteger with an Integer proven not to be negative.
Neither factor is negative, so the product is not negative either.
Returns the product, which is never negative.
on PositiveInteger
Multiplies this PositiveInteger with another.
Both factors are above zero, so the product is above zero.
Returns the product, which is above zero.
on Integer
Divides this Integer by a number, exactly.
Dividing by an Integer or a Rational answers empty for a zero divisor. Dividing this Integer by a NonZeroInteger, by a NonZeroRational or by an Algebraic can not fail. The first two divisors are proven, and an Algebraic is irrational and so never zero. The defaultingTo: entries answer the given value in place of empty.
Divides this Integer by an Integer.
The quotient is a Rational, since it need not be whole. A zero divisor answers empty.
Integerthe divisorReturns the exact quotient, or nothing when the divisor is zero.
Divides this Integer by a Rational.
The quotient is a Rational, since it need not be whole. A zero divisor answers empty.
Rationalthe divisorReturns the exact quotient, or nothing when the divisor is zero.
Divides this Integer by an Algebraic.
An Algebraic is irrational, so it is never zero and the division can not fail. A zero receiver answers the Rational zero.
Algebraicthe divisorReturns the exact quotient, an Algebraic or a Rational.
Divides this Integer by 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 exact quotient.
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 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 this Integer by a Rational proven not to be zero.
The division can not fail, so the answer is the quotient itself rather than an Optional.
Returns the exact quotient.
on NonZeroInteger
Divides this NonZeroInteger by a divisor proven not to be zero.
Neither operand is zero, so the quotient is not zero either. So the answer is a NonZeroRational, and it reaches reciprocal without an Optional in between.
Returns the exact quotient, which is not zero.
on Integer
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::remainder(dividingBy 3) is 1, and -7::remainder(dividingBy 3) is 2 rather than the -1 truncating division leaves. A zero divisor answers empty, and the defaultingTo: entry answers the given Integer instead.
Answers the remainder over a divisor nothing is known about.
Integerthe 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.
Integerthe divisorIntegerthe value to answer with when there is no remainderReturns the remainder, or the given value in its place.
Answers what is left over, paired with the quotient in the named way.
The entries above answer the #Euclidean pairing, which is why this one names no default. The #Truncating Case answers a remainder carrying the sign of the dividend, so -7::remainder(dividingBy 3, as #Truncating) is -1 rather than 2. The two agree wherever the dividend is not negative.
Returns the remainder.
on Integer
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 Integer. The remainder is never negative, so the quotient floors towards negative infinity rather than truncating towards zero. For example, 7::quotient(dividingBy 3) is 2, and -7::quotient(dividingBy 3) is -3, leaving a remainder of 2. A zero divisor answers empty, and the defaultingTo: entry answers the given Integer instead.
Answers the quotient over a divisor nothing is known about.
Integerthe 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.
Integerthe divisorIntegerthe value to answer with when there is no quotientReturns the quotient, or the given value in its place.
Answers how many whole divisors fit, rounded in the named direction.
The answer is the exact quotient rounded in that direction, so 7::quotient(dividingBy 2, toward #Up) is 4. The entries above pair their quotient with a remainder that is never negative. That pairing is the floor for a positive divisor and the ceiling for a negative one. So #Down answers what they answer for every divisor above zero.
Returns the quotient.
on Integer
Raises the Integer to the given power.
A non-negative exponent answers an Integer, and a negative one answers the exact reciprocal as a Rational. Zero to the power of zero is one. Raising this Integer to an exponent proven not to be negative can not fail. A receiver proven not to be zero can not fail either, and answers through NonZeroInteger. Zero raised to a negative power answers empty, and the defaultingTo: entry answers the given value instead.
Integerthe exponentReturns the power, or nothing when raising zero to a negative power.
Raises the Integer to the given power, and answers the given value when there is no power.
Integerthe exponentInteger | Rationalthe value to answer with when there is no powerReturns the power, or the given value in its place.
Raises the Integer to an exponent proven not to be negative.
Every Integer has such a power and every one of them is whole. So the answer is the power itself rather than an Optional, and it is an Integer rather than a Rational.
Returns the power.
on NonZeroInteger
Raises this NonZeroInteger to the given power.
A base that is not zero has every power, so the answer is the power itself rather than an Optional. An exponent proven not to be negative narrows that answer to an Integer.
Raises this NonZeroInteger to the given power.
A non-negative exponent answers an Integer, and a negative one answers the exact reciprocal as a Rational.
Integerthe exponentReturns the power.
Raises this NonZeroInteger to an exponent proven not to be negative.
Every such power is whole, so the answer is an Integer rather than a Union. The entry is Integer's own, declared here as well, because a receiver carrying more proof must not answer wider than one carrying none.
Returns the power.
on PositiveInteger
Raises this PositiveInteger to an exponent proven not to be negative.
Every such power is whole and above zero. Zero as an exponent answers one.
Returns the power, which is above zero.
on Integer
Answers the exact square root.
A perfect square answers an Integer, and any other non-negative Integer answers an exact Algebraic. A negative Integer answers empty, and the defaultingTo: entry answers the given value instead. A receiver proven not to be negative answers the root itself, through NonNegativeInteger.
Returns the root, or nothing for a negative Integer.
Answers the exact square root, with a value to answer for a negative Integer.
Integer | Algebraicthe value to answer with when there is no rootReturns the root, or the given value in its place.
on NonNegativeInteger
Answers the exact square root of this NonNegativeInteger.
A negative Integer is the only 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 perfect square answers an Integer, and every other value answers an exact Algebraic.
Returns the root.
on PositiveInteger
Answers the exact square root of this PositiveInteger.
A perfect square above zero has a root above zero, and every other value answers an exact Algebraic.
Returns the root.
on Integer
Answers whether this Integer is strictly below the given number.
Answers whether this Integer is strictly below another Integer.
Integerthe Integer to compare againstReturns true when this Integer is below the other.
Answers whether this Integer is strictly below a Rational.
Rationalthe Rational to compare againstReturns true when this Integer is below the Rational.
on Integer
Answers whether this Integer is below the given number, or equal to it.
Answers whether this Integer is below another Integer, or equal to it.
Integerthe Integer to compare againstReturns true when this Integer is not above the other.
Answers whether this Integer is below a Rational, or equal to it.
An Integer equals a Rational only when that Rational is whole.
Rationalthe Rational to compare againstReturns true when this Integer is not above the Rational.
on Integer
Answers whether this Integer is strictly above the given number.
Answers whether this Integer is strictly above another Integer.
Integerthe Integer to compare againstReturns true when this Integer is above the other.
Answers whether this Integer is strictly above a Rational.
Rationalthe Rational to compare againstReturns true when this Integer is above the Rational.
on Integer
Answers whether this Integer is above the given number, or equal to it.
Answers whether this Integer is above another Integer, or equal to it.
Integerthe Integer to compare againstReturns true when this Integer is not below the other.
Answers whether this Integer is above a Rational, or equal to it.
An Integer equals a Rational only when that Rational is whole.
Rationalthe Rational to compare againstReturns true when this Integer is not below the Rational.
on Integer · 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.
Integerone bound of the range, includedIntegerthe other bound of the range, includedReturns true when the value is within the bounds.
on Integer · 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.
Integerone bound of the rangeIntegerthe other bound of the rangeReturns the clamped value.
on Integer
Answers whether the Integer is divisible by two.
Zero is even.
on Integer
Answers whether the Integer is not divisible by two.
on Integer
Answers whether the Integer is above zero.
Zero is neither positive nor negative.
on Integer
Answers whether the Integer is below zero.
Zero is neither positive nor negative.
on Integer
Answers whether the Integer is exactly zero.
on Integer
Answers whether the Integer is a whole number.
Every Integer is whole, so the answer is always true.
on Integer
Answers whether the Integer is a whole multiple of the given divisor.
Zero is a multiple of every Integer. A zero divisor answers false: there is no remainder to read, and an empty answer is not zero.
Integerthe divisorReturns true when the division leaves no remainder.
on Integer
Answers whether the Integer has exactly two whole divisors above zero.
Two is the lowest prime. Zero, one and every negative Integer are not prime. The test is a Miller-Rabin test over the first twelve primes as witnesses. It decides every Integer below 3317044064679887385961981 exactly. A larger Integer it accepts is a strong probable prime to those twelve witnesses.
Returns true when the Integer is prime.
on Integer
Answers the Integer without its sign, which is its distance from zero.
Returns the distance, which is never negative.
on NonZeroInteger
Answers this NonZeroInteger 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 PositiveInteger.
Returns the distance, which is above zero.
on Integer
Answers the Integer with its sign flipped.
on NonZeroInteger
Answers this NonZeroInteger with its sign flipped.
Negation keeps a value away from zero, so the answer is a NonZeroInteger too.
Returns the negation, which is not zero.
on Integer
Answers the Integer, rounded in the named direction.
An Integer is already whole and is on every decimal grid, so every entry here answers the receiver itself. The direction is #Nearest when a call names none.
Answers the Integer, rounded in the named direction.
Returns the Integer itself.
Answers the Integer, rounded to a decimal grid of the given width.
Integerhow many decimal places to keepReturns the Integer itself.
on Integer
Answers the Integer read off a decimal grid of the given width.
An Integer is on every decimal grid, so the answer is the receiver itself as a Rational. 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 Integer itself, as a Rational.
on Integer
Answers the largest Integer that divides both this Integer and the given one.
The sign of either Integer is ignored, so the answer is never negative. Zero is divided by every Integer, so the divisor of zero and an Integer is that Integer without its sign. The divisor of two zeroes is zero.
Integerthe Integer to share a divisor withReturns the greatest common divisor, which is never negative.
on Integer
Answers the smallest Integer above zero that both this Integer and the given one divide.
The sign of either Integer is ignored, so the answer is never negative. Zero has no multiple above zero, so a zero operand answers zero. The product of the two answers holds for every pair: leastCommonMultiple · greatestCommonDivisor is the product of the two Integers without its sign.
Integerthe Integer to share a multiple withReturns the least common multiple, which is never negative.
on Integer
Answers the product of every Integer from one up to this Integer.
The factorial of zero is one. A negative Integer has no factorial, and the defaultingTo: entry answers the given Integer instead. A receiver proven not to be negative answers the factorial itself, through NonNegativeInteger.
Returns the factorial, or nothing for a negative Integer.
Answers the factorial, with a value to answer for a negative Integer.
Integerthe value to answer with when there is no factorialReturns the factorial, or the given value in its place.
on NonNegativeInteger
Answers the product of every Integer from one up to this NonNegativeInteger.
A negative Integer is the one receiver with no factorial, and the receiver is proven not to be one. So the answer is the factorial itself rather than an Optional. The factorial of zero is one.
Returns the factorial, which is above zero.
on Integer
Answers the Integer as a Rational.
Every Integer is a Rational over one, so the crossing never fails. The Rational is whole, and prints as the digits alone.
Returns the Rational holding this value.
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 Integers that are not zero, as a checked refinement of Integer.
A value carries a proof of the predicate before it is one of these. The proof is what lets an operation say it can not fail. A division by one of these answers a Rational rather than an Optional, and negate and absolute stay within the proof.
Tighter here multiply, divide, raise, absolute, negate
Returned by NonZeroInteger::multiply(with:), NonZeroInteger::negate(), NonZeroRational::numerator()
The Integers from zero upward, as a checked refinement of Integer.
An exponent proven to be one of these raises any Integer to a whole power. A receiver proven to be one has a real square root. Both operations answer an Optional without the proof.
Tighter here add, multiply, squareRoot, factorial
Returned by Dictionary::length(), Dictionary::count(where:), Integer::absolute(), Integer::greatestCommonDivisor(with:), Integer::leastCommonMultiple(with:), NonNegativeInteger::add(_:), NonNegativeInteger::multiply(with:), List::length(), List::count(of:), List::count(where:), Randomness::drawInteger(below:), String::length(), String::count(of:), String::count(of:comparing:)
The Integers above zero, as a checked refinement of Integer.
A value above zero is neither zero nor below it, so one of these is accepted wherever a NonZeroInteger or a NonNegativeInteger is wanted. Addition, multiplication and raise answer one of these again.
Tighter here add, multiply, raise, squareRoot
Returned by NonEmptyDictionary::length(), GroupedList::tally(), GroupedNonEmptyList::tally(), Integer::factorial(), NonZeroInteger::absolute(), NonNegativeInteger::add(_:), NonNegativeInteger::factorial(), PositiveInteger::add(_:), PositiveInteger::multiply(with:), PositiveInteger::raise(to:), PositiveInteger::squareRoot(), NonEmptyList::length(), Rational::denominator(), NonEmptyString::length()
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 Division.cases(). None of them is written out for a mode: Protocols says where each one comes from.
How a division pairs its quotient with its remainder.
Under #Euclidean the remainder is never negative, so it is always below the divisor's magnitude. Under #Truncating the remainder takes the sign of the dividend, which is what an algorithm ported from another language expects.
Cases #Euclidean, #Truncating
Taken by