std::int
Int, the one integer type, and its operations. An Int is exact at any
size: +, - and * never overflow.
- Subtypes.
NonZeroInt,NonNegativeIntandPositiveInt(all in the prelude),RadixandInt64are Ints that passed a check./and%take aNonZeroIntdivisor,Int.powaNonNegativeIntexponent andInt.to_digitsaRadix, so none of them can fail while the program runs. A literal is checked when the program is compiled, andNonZeroInt.try_from(n)checks a value the program computes. - Division.
/and%round toward zero;Int.floor_divandInt.moduloround toward negative infinity. - Math.
Int.abs,Int.sign,Int.pow,Int.sqrt,Int.gcdandInt.lcm. - Conversion.
Int.to_float,Int.to_decimal,Int.to_byte,Int.to_codepoint,Int.to_digitsandInt.from_digits.String.to_intreads an Int from text. - Bits.
&&&,|||,^^^,~~~,<<<and>>>,Int.bit_countandInt.bit_length, and the 64-bit modularInt.wrapping_add,Int.wrapping_subandInt.wrapping_mul. - Text for a reader.
Int.formatandInt.parse_inwrite and read an Int as a locale writes numbers.
Import with import std::int.
Exports
Section titled “Exports”Formatting numbers for a reader
Section titled “Formatting numbers for a reader”Display stays locale-independent: "${1234567}" is 1234567
everywhere, because that text goes into file names, URLs, headers and CSV.
Text for a reader comes from owner functions that take a Locale, with
options as named arguments: Int.format, Float.format and
Float.format_percent (std::float), and
Decimal.format and Decimal.format_percent
(std::decimal). These rules hold for all of them:
- Symbols and patterns are the locale’s CLDR decimal and percent
patterns and number symbols: decimal and group separators, plus, minus
and percent signs, infinity and NaN. Grouping follows the pattern’s sizes
(
#,##,##0groups lakhs), and CLDR’s minimum grouping digits: Polish and Spanish group only from five integer digits, so1234inplis1234and12345is12 345. - Digits are the locale’s default numbering system’s (
ar-EGwrites Arabic-Indic digits). A-u-nu-extension naming a numeric system overrides it (ar-EG-u-nu-latn,en-u-nu-arab), and-u-nu-nativepicks the locale’s native system. When the locale has no symbols for that system, its Latin symbols stand:en-u-nu-arabwrites١,٢٣٤,٥٦٧. - Options are typed: a digit count is a
NonNegativeInt, and the digits after the decimal point are oneClosedRange<NonNegativeInt>,fraction: 0..=2, so a negative count or a backwards range is a compile error. A count has no upper bound: it is what the caller asked for.
Parsing numbers a reader wrote
Section titled “Parsing numbers a reader wrote”Int.parse_in, Float.parse_in and Decimal.parse_in, and the
parse_percent_in forms of Float and Decimal, read what the matching
formatter writes, and return a NumberParseError
(std::localize) when the text is not a number in
the locale. Parsing never guesses the locale: 1.234 is one thousand two
hundred thirty-four in de and one point two three four in en.
- Strict (the default) accepts what the matching formatter writes for
the locale, with three allowances: group separators may be left out, any
Unicode space stands for a space the locale writes (a reader cannot type
U+202F), and ASCII digits stand for the locale’s own. Anything else is an
error: a group separator where the pattern puts none is
Grouping(en12,34,567isGrouping{at: 2};hi1,234,567isGrouping{at: 1}), and any other text isUnexpected, including a group separator after the decimal separator (en1.234,5isUnexpected{at: 5, text: ","}), surrounding space, a leading+and a fraction in anInt. Text that stops early isUnexpectedat the text’s length with emptytext(Decimal.parse_percent_in("12.5", Locale"en")).atcounts grapheme clusters, asString.lengthdoes. - Lenient (
lenient: True) also accepts group separators anywhere in the integer part, space around the text, a leading+, a minus sign after the number (12-) or parentheses around it ((12)), ASCII-and%for the locale’s signs, the percent sign on either side, and leaves out the invisible direction marks some locales’ signs carry. - Results. A number the type cannot hold is
OutOfRange: the locale’s infinity or NaN symbol read as anIntor aDecimal, and aFloatwhose digits overflow to infinity. AnIntis exact at any size, so digits alone never put one out of range.Decimal.parse_inkeeps the digits typed after the decimal separator as the scale, so12,50inplis12.50d.Decimal.parse_percent_individes by 100 exactly:12,5 %indeis0.125d, andFloat.parse_percent_inreads the same text as the nearestFloatto the fraction,0.125.Float.parse_inreads the infinity and NaN symbols. A formatted number parses back to its value in either mode.
type Int
Section titled “type Int”type Int
An integer of any size. Arithmetic is exact: +, - and * never
overflow, and a value grows past 64 bits as it needs to.
assert 9_223_372_036_854_775_807 + 1 == 9_223_372_036_854_775_808
assert 9_223_372_036_854_775_807 * 2 == 18_446_744_073_709_551_614
Int.to_float
Section titled “Int.to_float”fn to_float(x: Int) -> Float
type function on Int
The Float nearest to x, a tie going to the even one. A Float holds
53 bits of precision, so every Int from -2^53 to 2^53 converts
exactly and one past that may be rounded. An Int past the largest
Float is an infinity.
assert Int.to_float(5) == 5.0
assert Int.to_float(-7) == -7.0
assert Int.to_float(9_007_199_254_740_993) == 9_007_199_254_740_992.0
assert Int.to_float(Int.pow(10, 400)) == Float.positive_infinity()
Int.to_decimal
Section titled “Int.to_decimal”fn to_decimal(n: Int) -> Decimal
type function on Int
n as a Decimal with no digits after the decimal point. The
conversion is exact for every Int.
assert Int.to_decimal(42) == 42d
assert Int.to_decimal(-7) == -7d
assert Int.to_decimal(123_456_789_012_345_678_901) == 123_456_789_012_345_678_901d
Int.to_byte
Section titled “Int.to_byte”fn to_byte(n: Int) -> Maybe<Byte>
type function on Int
n as a Byte, or None when n is outside 0 to 255.
b = Int.to_byte(65)?
assert Byte.to_int(b) == 65
assert Int.to_byte(-1) == None
assert Int.to_byte(256) == None
assert Int.to_byte(Int.pow(2, 70)) == None
Int.to_codepoint
Section titled “Int.to_codepoint”fn to_codepoint(n: Int) -> Maybe<Codepoint>
type function on Int
The Codepoint numbered n, or None when n is not a Unicode
scalar value: one from 0 through 0x10FFFF that is not in the
UTF-16 surrogate block, 0xD800 through 0xDFFF.
assert Int.to_codepoint(65) == Some('A')
assert Int.to_codepoint(55_296) == None
assert Int.to_codepoint(1_114_112) == None
Int.wrapping_add
Section titled “Int.wrapping_add”fn wrapping_add(a: Int, b: Int) -> Int
type function on Int
64-bit modular addition: the low 64 bits of a + b, read as a signed
64-bit integer (two’s complement), so the result is between
-9_223_372_036_854_775_808 and 9_223_372_036_854_775_807. + is exact
at any size; this is for hashes, checksums, and algorithms defined in
64-bit arithmetic.
assert Int.wrapping_add(9_223_372_036_854_775_807, 1) == -9_223_372_036_854_775_808
assert 9_223_372_036_854_775_807 + 1 == 9_223_372_036_854_775_808
Int.wrapping_sub
Section titled “Int.wrapping_sub”fn wrapping_sub(a: Int, b: Int) -> Int
type function on Int
64-bit modular subtraction. See Int.wrapping_add.
assert Int.wrapping_sub(-9_223_372_036_854_775_808, 1) == 9_223_372_036_854_775_807
Int.wrapping_mul
Section titled “Int.wrapping_mul”fn wrapping_mul(a: Int, b: Int) -> Int
type function on Int
64-bit modular multiplication. See Int.wrapping_add.
assert Int.wrapping_mul(9_223_372_036_854_775_807, 2) == -2
Int.bit_count
Section titled “Int.bit_count”fn bit_count(n: NonNegativeInt) -> NonNegativeInt
type function on Int
The number of 1 bits in n, its population count. A negative Int,
read as two’s complement, has endless 1 bits, so n is a
NonNegativeInt: Int.bit_count(-1) is a compile error, and a
computed n goes through NonNegativeInt.try_from, or count
Int.abs(n)’s or ~~~n’s bits, whichever the algorithm means.
assert Int.bit_count(0) == 0
assert Int.bit_count(13) == 3
assert Int.bit_count(1_267_650_600_228_229_401_496_703_205_375) == 100
assert Int.bit_count(Int.abs(-13)) == 3
Int.bit_length
Section titled “Int.bit_length”fn bit_length(n: Int) -> NonNegativeInt
type function on Int
The number of bits n needs, not counting a sign: the position of a
non-negative n’s highest 1 bit plus one, so 0 for 0 and k
for any n from 2^(k-1) up to 2^k - 1. A negative n reads as
two’s complement, and its length is ~~~n’s, the bits below which
every bit is 1: Int.bit_length(-1) == 0, Int.bit_length(-8) == 3.
Go’s big.Int.BitLen for a non-negative n, Java’s
BigInteger.bitLength for any.
assert Int.bit_length(0) == 0
assert Int.bit_length(1) == 1
assert Int.bit_length(255) == 8
assert Int.bit_length(256) == 9
assert Int.bit_length(Int.pow(2, 100)) == 101
assert Int.bit_length(-1) == 0
assert Int.bit_length(-8) == 3
assert Int.bit_length(-9) == 4
Int.modulo
Section titled “Int.modulo”fn modulo(x: Int, y: NonZeroInt) -> Int
type function on Int
Floored modulo: the result is zero or has the sign of the divisor y,
as Python’s %, Elixir’s Integer.mod/2 and Gleam’s int.modulo
return, so it wraps a value into a range: Int.modulo(-1, 1440) is
1439. x % y differs for operands of opposite signs: it is the
remainder, takes the sign of the dividend x, and matches /
(-1 % 1440 is -1). Int.modulo(x, y) is x - y * Int.floor_div(x, y).
y is a NonZeroInt, so Int.modulo(x, 0) is a compile error, and
a computed y goes through NonZeroInt.try_from.
assert Int.modulo(7, 3) == 1
assert Int.modulo(-7, 3) == 2
assert Int.modulo(7, -3) == -2
assert Int.modulo(-7, -3) == -1
assert Int.modulo(-1, 1440) == 1439
assert -1 % 1440 == -1
Int.floor_div
Section titled “Int.floor_div”fn floor_div(x: Int, y: NonZeroInt) -> Int
type function on Int
Floored division: x / y rounded toward negative infinity, as
Python’s //, Elixir’s Integer.floor_div/2 and Gleam’s
int.floor_divide return. It is the partner of Int.modulo:
x == y * Int.floor_div(x, y) + Int.modulo(x, y) for every x and
non-zero y. x / y differs for operands of opposite signs: it
truncates toward zero (-7 / 2 is -3). y is a NonZeroInt, as
Int.modulo’s is.
assert Int.floor_div(7, 2) == 3
assert Int.floor_div(-7, 2) == -4
assert Int.floor_div(7, -2) == -4
assert Int.floor_div(-7, -2) == 3
assert -7 / 2 == -3
assert 2 * Int.floor_div(-7, 2) + Int.modulo(-7, 2) == -7
Int.abs
Section titled “Int.abs”fn abs(n: Int) -> NonNegativeInt
type function on Int
The absolute value of n: n without its sign. The result is a
NonNegativeInt, so it passes where one is needed:
Int.to_digits(Int.abs(n)) takes any n.
assert Int.abs(-5) == 5
assert Int.abs(0) == 0
assert Int.abs(-9_223_372_036_854_775_808) == 9_223_372_036_854_775_808
Int.sign
Section titled “Int.sign”fn sign(n: Int) -> Int
type function on Int
The sign of n: -1 for a negative n, 0 for 0 and 1 for a
positive one.
assert Int.sign(-7) == -1
assert Int.sign(0) == 0
assert Int.sign(7) == 1
Int.pow
Section titled “Int.pow”fn pow(n: Int, exp: NonNegativeInt) -> Int
type function on Int
n multiplied by itself exp times, exact; Int.pow(n, 0) is 1
for every n, 0 included. exp is a NonNegativeInt, since a
negative exponent’s result is a fraction, which is Float.pow’s; an
exponent computed at run time goes through NonNegativeInt.try_from.
assert Int.pow(2, 10) == 1024
assert Int.pow(-3, 3) == -27
assert Int.pow(0, 0) == 1
assert Int.pow(2, 100) == 1_267_650_600_228_229_401_496_703_205_376
assert Int.pow(-2, 63) == -9_223_372_036_854_775_808
Int.sqrt
Section titled “Int.sqrt”fn sqrt(n: NonNegativeInt) -> NonNegativeInt
type function on Int
The integer square root of n: the greatest Int whose square is at
most n, so the root rounded down. n is a NonNegativeInt, and so
is its root; a value from data goes through
NonNegativeInt.try_from.
assert Int.sqrt(16) == 4
assert Int.sqrt(15) == 3
assert Int.sqrt(0) == 0
assert Int.sqrt(9_223_372_036_854_775_807) == 3_037_000_499
assert Int.sqrt(NonNegativeInt.try_from(Int.pow(10, 40) + 1)!) == Int.pow(10, 20)
Int.gcd
Section titled “Int.gcd”fn gcd(a: Int, b: Int) -> NonNegativeInt
type function on Int
The greatest common divisor of a and b: the largest Int that
divides both, never negative whatever their signs. Int.gcd(n, 0) is
Int.abs(n) and Int.gcd(0, 0) is 0.
assert Int.gcd(12, 18) == 6
assert Int.gcd(-12, 18) == 6
assert Int.gcd(7, 0) == 7
assert Int.gcd(0, 0) == 0
assert Int.gcd(Int.pow(6, 40), Int.pow(4, 30)) == Int.pow(2, 40)
Int.lcm
Section titled “Int.lcm”fn lcm(a: Int, b: Int) -> NonNegativeInt
type function on Int
The least common multiple of a and b: the smallest non-negative
Int both divide, and 0 when either is 0.
assert Int.lcm(4, 6) == 12
assert Int.lcm(-4, 6) == 12
assert Int.lcm(5, 0) == 0
Int.to_digits
Section titled “Int.to_digits”fn to_digits(n: NonNegativeInt, base: Radix = 10) -> List<NonNegativeInt>
type function on Int
The digits of n in base, most significant first: [1, 2, 3] for
123, and [0] for 0. The digits are of a NonNegativeInt;
Int.abs a value first when its sign does not matter. base is a
Radix, 2 or more. Int.from_digits reads them back.
assert Int.to_digits(123) == [1, 2, 3]
assert Int.to_digits(0) == [0]
assert Int.to_digits(10, 2) == [1, 0, 1, 0]
assert Int.to_digits(255, 16) == [15, 15]
assert Int.to_digits(18_446_744_073_709_551_616, 65_536) == [1, 0, 0, 0, 0]
Int.from_digits
Section titled “Int.from_digits”fn from_digits(digits: List<Int>, base: Radix = 10) -> Maybe<NonNegativeInt>
type function on Int
The Int whose digits in base, most significant first, are digits,
as Int.to_digits writes them, and Some(0) for no digits. None
when a digit is outside 0 to base - 1. base is a Radix, 2 or
more.
assert Int.from_digits([1, 2, 3]) == Some(123)
assert Int.from_digits([1, 0, 1, 0], 2) == Some(10)
assert Int.from_digits([]) == Some(0)
assert Int.from_digits([1, 2], 2) == None
assert Int.from_digits([-1]) == None
assert Int.from_digits([9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9])
== Some(99_999_999_999_999_999_999)
Int.format
Section titled “Int.format”fn format(n: Int, locale: Locale, grouping: Bool = True, min_digits: NonNegativeInt = 1) -> String
type function on Int
n as a reader of locale writes it: the locale’s digits, minus
sign and group separator, grouped as the locale groups (hi writes
lakhs). grouping: False leaves the separators out, and min_digits
pads with zeros to at least that many digits; 0 and 1 pad nothing.
Display stays 1234567 everywhere. Polish and Spanish group only
from five integer digits.
assert Int.format(1234567, Locale"en") == "1,234,567"
assert Int.format(1234567, Locale"de") == "1.234.567"
assert Int.format(1234567, Locale"pl") == "1\u{a0}234\u{a0}567"
assert Int.format(1234567, Locale"hi") == "12,34,567"
assert Int.format(1234567, Locale"ar-EG") == "١٬٢٣٤٬٥٦٧"
assert Int.format(1234567, Locale"ar-EG-u-nu-latn") == "1,234,567"
assert Int.format(1234, Locale"pl") == "1234"
assert Int.format(12345, Locale"pl") == "12\u{a0}345"
assert Int.format(1234567, Locale"en-u-nu-arab") == "١,٢٣٤,٥٦٧"
assert Int.format(1234567, Locale"en", False) == "1234567"
assert Int.format(7, Locale"en", True, 3) == "007"
assert Int.format(7, Locale"en", True, 0) == "7"
assert Int.format(Int.pow(10, 21), Locale"de") == "1.000.000.000.000.000.000.000"
Int.parse_in
Section titled “Int.parse_in”fn parse_in(text: String, locale: Locale, lenient: Bool = False) -> Result<Int, NumberParseError>
type function on Int
Read text a reader of locale wrote as an Int. Strict by default:
the text must be what Int.format writes, except that the group
separators may be left out, any space stands for the locale’s space,
and ASCII digits for the locale’s digits. lenient: True also takes
separators anywhere, surrounding space, +, (12) and 12-. A
fraction is Unexpected at the decimal separator, even a zero one.
assert Int.parse_in("1.234.567", Locale"de") == Ok(1234567)
assert Int.parse_in("12,34,567", Locale"hi") == Ok(1234567)
assert Int.parse_in("1,234", Locale"de") == Err(NumberParseError.Unexpected{at: 1, text: ","})
assert Int.parse_in("12,34,567", Locale"en") == Err(NumberParseError.Grouping{at: 2})
assert Int.parse_in(" 12,34,567 ", Locale"en", True) == Ok(1234567)
assert Int.parse_in("", Locale"en") == Err(NumberParseError.Empty)
assert Int.parse_in("+12", Locale"en") == Err(NumberParseError.Unexpected{at: 0, text: "+"})
assert Int.parse_in("+12", Locale"en", True) == Ok(12)
assert Int.parse_in("12-", Locale"en", True) == Ok(-12)
assert Int.parse_in("(12)", Locale"en", True) == Ok(-12)
assert Int.parse_in("12.0", Locale"en") == Err(NumberParseError.Unexpected{at: 2, text: "."})
assert Int.parse_in("1,234,567", Locale"hi") == Err(NumberParseError.Grouping{at: 1})
assert Int.parse_in("∞", Locale"en") == Err(NumberParseError.OutOfRange)
assert Int.parse_in("1,000,000,000,000,000,000,000", Locale"en") == Ok(Int.pow(10, 21))
Int.to_string
Section titled “Int.to_string”impl Display for Int
fn to_string(n: Int) -> String
impl Display.to_string
n in decimal digits, with a - before a negative one and no
separators: the text io.print and string interpolation write.
Int.format writes an Int for a reader in a locale.
assert Int.to_string(42) == "42"
assert Int.to_string(-7) == "-7"
assert "${1234567}" == "1234567"
assert Int.to_string(Int.pow(2, 70)) == "1180591620717411303424"
Int.add
Section titled “Int.add”impl Add<Int, Int> for Int
fn add(lhs: Int, rhs: Int) -> Int
impl Add.add
lhs + rhs, exact at any size. Int.add names it where a function
is passed: Iter.reduce(xs, 0, Int.add).
assert 2 + 3 == 5
assert Int.add(2, 3) == 5
Int.empty_sum
Section titled “Int.empty_sum”impl Summable for Int
fn empty_sum() -> Int
impl Summable.empty_sum
The sum of no Ints: 0.
assert Int.empty_sum() == 0
assert 7 + Int.empty_sum() == 7
Int.subtract
Section titled “Int.subtract”impl Subtract<Int, Int> for Int
fn subtract(lhs: Int, rhs: Int) -> Int
impl Subtract.subtract
lhs - rhs, exact at any size.
assert 2 - 3 == -1
assert Int.subtract(2, 3) == -1
Int.multiply
Section titled “Int.multiply”impl Multiply<Int, Int> for Int
fn multiply(lhs: Int, rhs: Int) -> Int
impl Multiply.multiply
lhs * rhs, exact at any size.
assert 4 * 3 == 12
assert Int.multiply(4, 3) == 12
Int.empty_product
Section titled “Int.empty_product”impl Productable for Int
fn empty_product() -> Int
impl Productable.empty_product
The product of no Ints: 1.
assert Int.empty_product() == 1
assert 7 * Int.empty_product() == 7
Int.divide
Section titled “Int.divide”impl Divide<NonZeroInt, Int> for Int
fn divide(lhs: Int, rhs: NonZeroInt) -> Int
impl Divide.divide
Integer division, lhs / rhs, rounded toward zero. The divisor is a
NonZeroInt, so a division never fails while the program runs: it
is a literal other than 0, a value whose type rules out zero, or an
Int the code has compared with 0 on the way to the division. 7 / d
with a d that is only known to be an Int is a compile error.
Int.floor_div rounds toward negative infinity.
assert 7 / 2 == 3
assert -7 / 2 == -3
share = fn total: Int, people: Int -> if people == 0 then 0 else total / people
assert share(10, 4) == 2
assert share(10, 0) == 0
nz = NonZeroInt.try_from(3)!
assert Int.divide(10, nz) == 3
Int.remainder
Section titled “Int.remainder”impl Remainder<NonZeroInt, Int> for Int
fn remainder(lhs: Int, rhs: NonZeroInt) -> Int
impl Remainder.remainder
lhs % rhs: the remainder of /’s division, which truncates toward
zero, so it takes the sign of lhs. (a / b) * b + a % b == a.
Int.modulo takes the divisor’s sign instead. The divisor is a
NonZeroInt, as /’s is.
assert 7 % 3 == 1
assert -7 % 3 == -1
assert 7 % -3 == 1
assert Int.remainder(-7, -3) == -1
Int.bitwise_and
Section titled “Int.bitwise_and”impl BitwiseAnd<Int, Int> for Int
fn bitwise_and(lhs: Int, rhs: Int) -> Int
Bitwise and, a &&& b: each bit set in both. An Int reads as two’s
complement with as many bits as it needs, a negative one with endless
leading ones, so the result holds at any size.
assert Int.bitwise_and(12, 10) == 8
assert 12 &&& 10 == 8
assert (Int.pow(2, 100) + 5) &&& 7 == 5
assert -1 &&& Int.pow(2, 80) == Int.pow(2, 80)
Int.bitwise_or
Section titled “Int.bitwise_or”impl BitwiseOr<Int, Int> for Int
fn bitwise_or(lhs: Int, rhs: Int) -> Int
impl BitwiseOr.bitwise_or
Bitwise or, a ||| b: each bit set in either. See Int.bitwise_and.
assert Int.bitwise_or(12, 10) == 14
assert 12 ||| 10 == 14
assert -8 ||| 3 == -5
Int.bitwise_xor
Section titled “Int.bitwise_xor”impl BitwiseXor<Int, Int> for Int
fn bitwise_xor(lhs: Int, rhs: Int) -> Int
Bitwise exclusive or, a ^^^ b: each bit set in one but not both.
See Int.bitwise_and.
assert Int.bitwise_xor(12, 10) == 6
assert 12 ^^^ 10 == 6
assert -1 ^^^ 5 == -6
Int.bitwise_not
Section titled “Int.bitwise_not”impl BitwiseNot<Int> for Int
fn bitwise_not(value: Int) -> Int
Bitwise not, ~~~n: every bit flipped, the one’s complement, which
is -n - 1. See Int.bitwise_and.
assert Int.bitwise_not(0) == -1
assert ~~~5 == -6
assert ~~~Int.pow(2, 70) == -Int.pow(2, 70) - 1
Int.shift_left
Section titled “Int.shift_left”impl ShiftLeft<NonNegativeInt, Int> for Int
fn shift_left(lhs: Int, rhs: NonNegativeInt) -> Int
impl ShiftLeft.shift_left
Shift left, a <<< n: a times 2 to the n, exact, so no bit is
ever shifted out. The count is a NonNegativeInt.
assert Int.shift_left(1, 4) == 16
assert 1 <<< 64 == 18_446_744_073_709_551_616
assert -3 <<< 2 == -12
Int.shift_right
Section titled “Int.shift_right”impl ShiftRight<NonNegativeInt, Int> for Int
fn shift_right(lhs: Int, rhs: NonNegativeInt) -> Int
Arithmetic shift right, a >>> n: a divided by 2 to the n,
rounded toward negative infinity. A count past a’s bits yields 0
for a non-negative a and -1 for a negative one. The count is a
NonNegativeInt.
assert Int.shift_right(-8, 1) == -4
assert Int.pow(2, 100) >>> 98 == 4
assert -1 >>> 200 == -1
assert 7 >>> 200 == 0
Int.inspect
Section titled “Int.inspect”impl Debug for Int
fn inspect(n: Int) -> String
impl Debug.inspect
The same text as Display (42): an Int’s literal is its
user-facing form.
assert Debug.inspect(42) == Display.to_string(42)
Int.equal
Section titled “Int.equal”impl Equatable for Int
fn equal(a: Int, b: Int) -> Bool
impl Equatable.equal
Int.hash
Section titled “Int.hash”impl Hashable for Int
fn hash(n: Int) -> Int
impl Hashable.hash
Int.compare
Section titled “Int.compare”impl Comparable for Int
fn compare(a: Int, b: Int) -> Ordering
impl Comparable.compare
The order of a and b as numbers: Less, Equal or Greater.
It is what <, >, Iter.sort, Iter.min and Iter.max use.
assert Int.compare(1, 2) == Ordering.Less
assert Int.compare(2, 2) == Ordering.Equal
assert Int.compare(3, 2) == Ordering.Greater
Int.next
Section titled “Int.next”impl Discrete for Int
fn next(n: Int) -> Maybe<Int>
impl Discrete.next
Int.steps_between
Section titled “Int.steps_between”impl Discrete for Int
fn steps_between(start: Int, stop: Int) -> Maybe<Int>
Int.step_by
Section titled “Int.step_by”impl Steppable for Int
fn step_by(n: Int, by: Int) -> Maybe<Int>
impl Steppable.step_by
type NonZeroInt
Section titled “type NonZeroInt”subtype NonZeroInt Int when fn n -> n != 0
An Int that is not zero, which / and % take as their divisor, so a
division cannot fail while the program runs. A literal is
checked when the program is compiled; NonZeroInt.try_from(n) checks a
value at run time. A NonZeroInt is an Int, and passes wherever one is
expected. It is in the prelude.
subtype: NonZeroInt is a subtype of Int. A literal where a NonZeroInt is expected is checked when the program is compiled; NonZeroInt.try_from(...) checks a value when it runs.
d: NonZeroInt = 4
assert 12 / d == 3
assert NonZeroInt.try_from(5) == Some(5)
assert NonZeroInt.try_from(0) == None
NonZeroInt.try_from
Section titled “NonZeroInt.try_from”fn try_from(value: Int) -> Maybe<NonZeroInt>
type function on NonZeroInt
Some of the value when it satisfies NonZeroInt’s rules, None when it does not.
type NonNegativeInt
Section titled “type NonNegativeInt”subtype NonNegativeInt Int when fn n -> n >= 0
An Int that is zero or more: a count, an index, an exponent. A literal is
checked when the program is compiled; NonNegativeInt.try_from(n) checks
a value at run time. It is in the prelude.
subtype: NonNegativeInt is a subtype of Int. A literal where a NonNegativeInt is expected is checked when the program is compiled; NonNegativeInt.try_from(...) checks a value when it runs.
e: NonNegativeInt = 3
assert Int.pow(2, e) == 8
assert NonNegativeInt.try_from(0) == Some(0)
assert NonNegativeInt.try_from(-1) == None
NonNegativeInt.try_from
Section titled “NonNegativeInt.try_from”fn try_from(value: Int) -> Maybe<NonNegativeInt>
type function on NonNegativeInt
Some of the value when it satisfies NonNegativeInt’s rules, None when it does not.
type PositiveInt
Section titled “type PositiveInt”subtype PositiveInt NonNegativeInt and NonZeroInt when fn n -> n > 0
An Int greater than zero. It is a subtype of both NonNegativeInt and
NonZeroInt, so a PositiveInt passes wherever either, or an Int, is
expected. It is in the prelude.
subtype: PositiveInt is a subtype of NonNegativeInt and NonZeroInt, and through them Int. A literal where a PositiveInt is expected is checked when the program is compiled; PositiveInt.try_from(...) checks a value when it runs.
p: PositiveInt = 4
nz: NonZeroInt = p
assert 12 / nz == 3
assert PositiveInt.try_from(5) == Some(5)
assert PositiveInt.try_from(-3) == None
assert PositiveInt.try_from(0) == None
PositiveInt.try_from
Section titled “PositiveInt.try_from”fn try_from(value: Int) -> Maybe<PositiveInt>
type function on PositiveInt
Some of the value when it satisfies PositiveInt’s rules, None when it does not.
type Radix
Section titled “type Radix”subtype Radix Int when fn b -> b >= 2
A base to write digits in: an Int of 2 or more, which Int.to_digits
and Int.from_digits take. A literal is checked when the program is
compiled; Radix.try_from(b) checks a value at run time.
subtype: Radix is a subtype of Int. A literal where a Radix is expected is checked when the program is compiled; Radix.try_from(...) checks a value when it runs.
assert Int.to_digits(5, 2) == [1, 0, 1]
assert Radix.try_from(1) == None
Radix.try_from
Section titled “Radix.try_from”fn try_from(value: Int) -> Maybe<Radix>
type function on Radix
Some of the value when it satisfies Radix’s rules, None when it does not.
type Int64
Section titled “type Int64”subtype Int64 Int when fn n -> n >= -9223372036854775808 and n <= 9223372036854775807
An Int that fits in 64 bits, from -9223372036854775808 to
9223372036854775807: what a Go int64 holds. A std function or field
whose limit is 64 bits is an Int64, as a Date’s month and day are.
A literal past the range is an error when the program is compiled, and
Int64.try_from(n) checks a value at run time, before the call that
needs it. An Int64 is an Int, and passes wherever one is expected.
subtype: Int64 is a subtype of Int. A literal where a Int64 is expected is checked when the program is compiled; Int64.try_from(...) checks a value when it runs.
n: Int64 = 9223372036854775807
assert n + 1 == 9223372036854775808
assert Int64.try_from(-9223372036854775808) == Some(-9223372036854775808)
assert Int64.try_from(9223372036854775808) == None
Int64.try_from
Section titled “Int64.try_from”fn try_from(value: Int) -> Maybe<Int64>
type function on Int64
Some of the value when it satisfies Int64’s rules, None when it does not.