is_eq : Utf8Problem, Utf8Problem -> Bool
Builtin Type Module
Str
Utf8Problem
Concatenates two strings together.
expect "ab".concat("cd") == "abcd"
expect "hello".concat("") == "hello"
expect "".concat("") == ""
Determines whether or not the first Str contains the second.
expect "foobarbaz".contains("bar")
expect !"apple".contains("orange")
expect "anything".contains("")
Return the Str with all whitespace removed from both the beginning as well as the end.
expect " Hello \n\n".trim() == "Hello"
Return the Str with all whitespace removed from the beginning.
expect " Hello \n\n".trim_start() == "Hello \n\n"
Return the Str with all whitespace removed from the end.
expect " Hello \n\n".trim_end() == " Hello"
Returns True if all the ASCII characters in both strings are the same,
ignoring differences in capitalization.
Non-ASCII characters must all be exactly the same,
including capitalization. For example:
expect "café".caseless_ascii_equals("CAFé")
expect !"café".caseless_ascii_equals("CAFÉ")
The first call returns True because all the ASCII characters are the same
when ignoring differences in capitalization, and the only non-ASCII character
(é) is the same in both strings. The second call returns False because
é and É are not ASCII characters, and they are different.
This function is useful for things like command-line flags and environment variable names where you know in advance that you're dealing with a string containing only ASCII characters. It has better performance than lowercasing operations which take Unicode into account.
That said, strings received from user input can always contain
non-ASCII Unicode characters, and lowercasing Unicode works
differently in different languages. For example, the string "I" lowercases to "i"
in English and to "ı" (a dotless i)
in Turkish. These rules can also change in each Unicode release,
so we have separate unicode package
for Unicode capitalization that can be upgraded independently from the language's builtins.
To convert a string's ASCII characters to uppercase or lowercase, you can use Str.with_ascii_uppercased or Str.with_ascii_lowercased.
Returns a version of the string with all ASCII characters lowercased. Non-ASCII characters are left unmodified. For example:
expect "CALFÉ".with_ascii_lowercased() == "calfÉ"
This function is useful for things like command-line flags and environment variable names where you know in advance that you're dealing with a string containing only ASCII characters. It has better performance than lowercasing operations which take Unicode into account.
That said, strings received from user input can always contain
non-ASCII Unicode characters, and lowercasing Unicode works
differently in different languages. For example, the string "I" lowercases to "i"
in English and to "ı" (a dotless i)
in Turkish. These rules can also change in each Unicode release,
so we have separate unicode package
for Unicode capitalization that can be upgraded independently from the language's builtins.
To do a case-insensitive comparison of the ASCII characters in a string, you can use Str.caseless_ascii_equals.
Returns a version of the string with all ASCII characters uppercased. Non-ASCII characters are left unmodified. For example:
expect "café".with_ascii_uppercased() == "CAFé"
This function is useful for things like command-line flags and environment variable names where you know in advance that you're dealing with a string containing only ASCII characters. It has better performance than uppercasing operations which take Unicode into account.
That said, strings received from user input can always contain
non-ASCII Unicode characters, and uppercasing Unicode
works differently in different languages.
For example, the string "i" uppercases to "I" in English and to "İ"
(a dotted I) in Turkish.
These rules can also change in each Unicode release,
so we have a separate unicode package for Unicode capitalization
that can be upgraded independently from the language's builtins.
To do a case-insensitive comparison of the ASCII characters in a string, you can use Str.caseless_ascii_equals.
Check if the given Str starts with a value.
expect "ABC".starts_with("A") == Bool.True
expect "ABC".starts_with("X") == Bool.False
Check if the given Str ends with a value.
expect "ABC".ends_with("C") == Bool.True
expect "ABC".ends_with("X") == Bool.False
Repeats a string the given number of times.
expect "z".repeat(3) == "zzz"
expect "na".repeat(8) == "nananananananana"
Returns "" when given "" for the string or 0 for the count.
expect "".repeat(10) == ""
expect "anything".repeat(0) == ""
Adds a prefix to the given Str.
expect "Awesome".with_prefix("Roc") == "RocAwesome"
Gives the number of bytes in a Str value.
expect "Hello World".count_utf8_bytes() == 11
Returns a string of the specified capacity without any content.
This is like calling Str.reserve on an empty string. It's intended for building up a string incrementally, for example by calling Str.concat on it:
greeting = "Hello and welcome to Roc"
subject = "Awesome Programmer"
# Evaluates to "Hello and welcome to Roc, Awesome Programmer!"
# Note that string interpolation with "${greeting}, ${subject}!" would be preferred here,
# this is just a simple example to demonstrate `Str.with_capacity`.
hello_world =
Str.with_capacity(45)
.concat(greeting)
.concat(", ")
.concat(subject)
.concat("!")
When the final size is known up front, Str.with_capacity guarantees that
subsequent calls to Str.concat will not need to reallocate. Whether this
is faster than starting from "" depends on the system allocator: many
allocators can extend an existing allocation in place, making the difference
small or negligible. The benefit is most pronounced when reallocation would
otherwise force a full copy of the string.
If you don't know the exact capacity, passing a value larger than necessary still avoids reallocation, at the cost of using more memory than is needed.
For more details, see Str.reserve.
Increase a string's capacity by at least the given number of additional bytes.
When you plan to append more bytes onto a string, Str.reserve can help avoid intermediate reallocations during a chain of Str.concat calls. Consider the following example, which does not use Str.reserve:
greeting = "Hello and welcome to Roc"
subject = "Awesome Programmer"
# Evaluates to "Hello and welcome to Roc, Awesome Programmer!"
hello_world =
greeting
.concat(", ")
.concat(subject)
.concat("!")
In this example:
1. We start with greeting, which has both a length and capacity of 24 bytes.
2. .concat(", ") sees there isn't enough capacity for 2 more bytes, so
it allocates a new 26-byte buffer, copies the existing 24 bytes into
it, and writes ", " at the end. The old allocation is deallocated.
3. .concat(subject) repeats the process: allocate a 44-byte buffer,
copy the 26 existing bytes, write "Awesome Programmer".
4. .concat("!") repeats once more: allocate 45 bytes, copy 44, write "!".
With Str.reserve, the chain only needs one allocation up front:
hello_world =
greeting
.reserve(21)
.concat(", ")
.concat(subject)
.concat("!")
Here, .reserve(21) ensures there is room for an additional 21 bytes
(", " + "Awesome Programmer" + "!"), allocating a 45-byte buffer
and copying greeting into it. The subsequent Str.concat calls all fit
in that capacity and do not need to reallocate.
Whether this is actually faster depends on the system allocator: many allocators can extend an existing allocation in place, in which case the per-concat reallocation is cheap and Str.reserve makes little observable difference. The benefit is most pronounced when reallocation would otherwise force a full copy of the string.
Str.reserve is not free — when more capacity is needed, it always performs a heap allocation. Only use it when you actually expect to make use of the extra capacity.
When you don't know exactly how many bytes you'll need, choosing a value somewhat higher than necessary is usually safe; a value that's too low may force later reallocation, while a value much higher than necessary just wastes memory.
If you plan to use Str.reserve on an empty string, use Str.with_capacity instead.
Shrink the memory footprint of a str such that its capacity and length are equal. Note: This will also convert seamless slices to regular lists.
Returns a List of the string's U8 UTF-8 code units. (To split the string into a List of smaller Str values instead of U8 values, see Str.split_on.)
expect "Roc".to_utf8() == [82, 111, 99]
expect "鹏".to_utf8() == [233, 185, 143]
expect "சி".to_utf8() == [224, 174, 154, 224, 174, 191]
expect "🐦".to_utf8() == [240, 159, 144, 166]
Converts a List of U8 UTF-8 code units to a string. Any grouping of invalid byte sequences are replaced with a single unicode replacement character '�'.
An invalid byte sequence is defined as - a 2-byte-sequence starting byte, followed by less than 1 continuation byte - a 3-byte-sequence starting byte, followed by less than 2 continuation bytes - a 4-byte-sequence starting byte, followed by less than 3 continuation bytes - an invalid codepoint from the surrogate pair block - an invalid codepoint greater than 0x110000 encoded as a 4-byte sequence - any valid codepoint encoded as an incorrect sequence, for instance a codepoint that should be a 2-byte sequence encoded as a 3- or 4-byte sequence
expect Str.from_utf8_lossy([82, 111, 99, 240, 159, 144, 166]) == "Roc🐦"
expect Str.from_utf8_lossy([82, 255, 99]) == "R�c"
expect Str.from_utf8_lossy([82, 0xED, 0xA0, 0xBD, 99]) == "R�c"
Converts a List of U8 UTF-8 code units to a string.
Returns Err if the given bytes are invalid UTF-8, and returns Ok("") when given [].
expect Str.from_utf8([82, 111, 99]) == Ok("Roc")
expect Str.from_utf8([233, 185, 143]) == Ok("鹏")
expect Str.from_utf8([224, 174, 154, 224, 174, 191]) == Ok("சி")
expect Str.from_utf8([240, 159, 144, 166]) == Ok("🐦")
expect Str.from_utf8([]) == Ok("")
expect Str.from_utf8([255]).is_err()
Split a string around a separator.
Passing "" for the separator is not useful;
it returns the original string wrapped in a List.
expect "1,2,3".split_on(",") == ["1","2","3"]
expect "1,2,3".split_on("") == ["1,2,3"]
Combines a List of strings into a single string, with a separator string in between each.
expect Str.join_with(["one", "two", "three"], ", ") == "one, two, three"
expect Str.join_with(["1", "2", "3", "4"], ".") == "1.2.3.4"
Returns True if the two strings are exactly the same.
inspect : _val -> Str
Returns a human-readable representation of a value, useful for debugging.
Encode a string using a format that provides encode_str
decode : src, fmt -> (Try(Str, err), src) where { fmt.decode_str : fmt, src -> (Try(Str, err), src) }
Decode a string using a format that provides decode_str
List
Returns the length of the list, which is equal to the number of elements it contains.
One List can store up to Num.max_i64 (TODO update for current max function) elements on 64-bit targets and Num.max_i32 (TODO update for current max function) on 32-bit targets like wasm.
This means the #U64 this function returns can always be safely converted to #I64 or #I32, depending on the target.
Check if the list is empty.
[1, 2, 3].is_empty()
[].is_empty()
Put two lists together.
[1, 2, 3].concat([4, 5])
[0, 1, 2].concat([3, 4])
Create a list with space for at least capacity elements
Sort a list using a custom comparison function. The comparator receives two
elements and returns LT, EQ, or GT to indicate their relative order.
expect [3, 1, 2].sort_with(|a, b| if a < b LT else if a > b GT else EQ) == [1, 2, 3]
# Sort in descending order by swapping the LT and GT
expect [3, 1, 2].sort_with(|a, b| if a > b LT else if a < b GT else EQ) == [3, 2, 1]
Returns True if the two lists have the same length and their elements are pairwise equal.
Add a single element to the end of a list.
[1, 2, 3].append(4)
[0, 1, 2].append(3)
Returns the first element in the list, or ListWasEmpty if it was empty.
expect [1, 2, 3].first() == Ok(1)
expect [].first() == Err(ListWasEmpty)
Returns an element from a list at the given index.
Returns Err OutOfBounds if the given index exceeds the List's length
expect [100, 200, 300].get(1) == Ok(200)
expect [100, 200, 300].get(5) == Err(OutOfBounds)
Returns the reversed list.
expect [1, 2, 3].rev() == [3, 2, 1]
expect [].rev() == []
for_each! : List(item), (item => { }) => { }
Convert each element in the list to something new, by calling a conversion function on each of them. Then return a new list of the converted values.
expect [1, 2, 3].map(|num| num + 1) == [2, 3, 4]
expect ["", "a", "bc"].map(Str.is_empty) == [Bool.True, Bool.False, Bool.False]
Run the given function on each element of a list, and return all the
elements for which the function returned Bool.True.
[1, 2, 3, 4].keep_if(|num| num > 2)
## Performance Details
List.keep_if always returns a list that takes up exactly the same amount of memory as the original, even if its length decreases. This is because it can't know in advance exactly how much space it will need, and if it guesses a length that's too low, it would have to re-allocate.
(If you want to do an operation like this which reduces the memory footprint of the resulting list, you can do two passes over the list with List.fold - one to calculate the precise new size, and another to populate the new list.)
If given a unique list, List.keep_if will mutate it in place to assemble the appropriate list. If that happens, this function will not allocate any new memory on the heap. If all elements in the list end up being kept, Roc will return the original list unaltered.
Run the given function on each element of a list, and return all the
elements for which the function returned Bool.False.
[1, 2, 3, 4].drop_if(|num| num > 2)
## Performance Details
List.drop_if has the same performance characteristics as List.keep_if.
See its documentation for details on those characteristics!
Run the given function on each element of a list, and return the
number of elements for which the function returned Bool.True.
expect [1, -2, -3].count_if(I64.is_negative) == 2
expect [1, 2, 3].count_if(|num| num > 1) == 2
fold : List(item), state, (state, item -> state) -> state
Build a value using each element in the list.
Starting with a given state value, this folds through each element in the
list from first to last, running a given step function on that element
which updates the state. It returns the final state at the end.
[2, 4, 8].fold(0, U64.plus)
This returns 14 because:
* state starts at 0
* Each step runs state.plus(elem), and the return value becomes the new state.
Here is a table of how state changes as List.fold folds over the elements
[2, 4, 8] using U64.plus as its step function to determine the next state.
state | elem | U64.plus(state, elem) :---: | :---: | :----------------: 0 | | 0 | 2 | 2 2 | 4 | 6 6 | 8 | 14
The following returns -6:
[1, 2, 3].fold(0, I64.minus)
Note that in other languages, fold is sometimes called reduce,
fold_left, or foldl.
fold_rev : List(item), state, (item, state -> state) -> state
Like List.fold, but walks the list from last to first. The step function
receives the element first and the current state second.
expect [1, 2, 3].fold_rev(0, I64.minus) == 2
Here is a table of how state changes as List.fold_rev folds over the
elements [1, 2, 3] using I64.minus as its step function.
state | elem | I64.minus(elem, state) :---: | :---: | :-------------------: 0 | | 0 | 3 | 3 3 | 2 | -1 -1 | 1 | 2
Note that in other languages, fold_rev is sometimes called reduce_right,
fold_right, or foldr.
Run the given predicate on each element of the list, returning Bool.True if
any of the elements satisfy it.
expect [1, 2, 3].any(|n| n % 2 == 0)
expect ![1, 2, 3].any(|n| n < 0)
Returns Bool.True if the list contains an element equal to the given value.
expect [1, 2, 3].contains(2)
expect ![1, 2, 3].contains(4)
Run the given predicate on each element of the list, returning Bool.True if
all of the elements satisfy it.
expect [2, 4, 6].all(|n| n % 2 == 0)
expect ![1, 2, 3].all(|n| n % 2 == 0)
Returns the last element in the list, or ListWasEmpty if it was empty.
expect [1, 2, 3].last() == Ok(3.0)
expect [].last() == Err(ListWasEmpty)
single : item -> List(item)
Create a list with a single element in it.
expect List.single(42) == [42.0]
Remove the element at the given index. If the index is out of bounds, the list is returned unchanged.
expect [10, 20, 30, 40].drop_at(1) == [10, 30, 40]
expect [10, 20, 30].drop_at(5) == [10, 20, 30]
Return a sublist of the list starting at start and containing up to len
elements. Out-of-bounds ranges are clamped, producing a shorter or empty list.
expect [1, 2, 3, 4, 5].sublist({ start: 1, len: 3 }) == [2, 3, 4]
expect [1, 2, 3].sublist({ start: 1, len: 10 }) == [2, 3]
expect [1, 2, 3].sublist({ start: 10, len: 2 }) == []
Return the first n elements of the list. If the list has fewer than n
elements, the entire list is returned.
expect [1, 2, 3, 4, 5].take_first(3) == [1, 2, 3]
expect [1, 2].take_first(10) == [1, 2]
Returns the given number of elements from the end of the list.
expect [1, 2, 3, 4, 5, 6, 7, 8].take_last(4) == [5, 6, 7, 8]
If there are fewer elements in the list than the requested number, the entire list is returned.
expect [1, 2].take_last(5) == [1, 2]
To *remove* elements from the end of the list, use List.take_first.
To remove elements from both the beginning and end of the list,
use List.sublist.
Drops n elements from the beginning of the list. If n is larger than the
list length, an empty list is returned.
expect [1, 2, 3, 4, 5].drop_first(2) == [3, 4, 5]
expect [1, 2, 3].drop_first(10) == []
Drops n elements from the end of the list. If n is larger than the
list length, an empty list is returned.
expect [1, 2, 3, 4, 5].drop_last(2) == [1, 2, 3]
expect [1, 2, 3].drop_last(10) == []
Join a list of items into a single item, inserting the given separator between
each pair. Works for any type that implements a join_with method, such as Str.
expect ["a", "b", "c"].join_with(", ") == "a, b, c"
expect [].join_with(", ") == ""
Build a list by repeating the given value n times.
expect List.repeat(0, 3) == [0, 0, 0]
expect List.repeat("hi", 0) == []
sum : List(item) -> item where { item.default : -> item, item.plus : item, item -> item }
Sum the elements of a list. Works for any type that implements plus and
default methods, such as the numeric types.
expect List.sum([1.I64, 2, 3, 4]) == 10
expect List.sum([]) == 0.I64
encode : List(item), fmt -> Try(encoded, err) where { fmt.encode_list : fmt, List(item), (item, fmt -> Try(encoded, err)) -> Try(encoded, err), item.encode : item, fmt -> Try(encoded, err) }
Encode a list using a format that provides encode_list
decode : src, fmt -> (Try(List(item), err), src) where { fmt.decode_list : fmt, src, (src, fmt -> (Try(item, err), src)) -> (Try(List(item), err), src), item.decode : src, fmt -> (Try(item, err), src) }
Decode a list using a format that provides decode_list
Bool
Returns Bool.False when given Bool.True, and vice versa. This is
equivalent to the logic NOT
gate. The operator ! can also be used as shorthand for Bool.not.
expect Bool.not(Bool.False) == Bool.True
expect !Bool.False == Bool.True
Returns Bool.True if the two booleans are the same, and Bool.False if they are different.
Encode a bool using a format that provides encode_bool
decode : src, fmt -> (Try(Bool, err), src) where { fmt.decode_bool : fmt, src -> (Try(Bool, err), src) }
Decode a bool using a format that provides decode_bool
Box
box : item -> Box(item)
Wraps a value in a generic, opaque representation (box) that can easily be passed to the platform. Boxing is an expensive process because it copies the value from the stack to the heap. This may provide a performance optimization for advanced use cases with large values.
unbox : Box(item) -> item
Unwraps a value from a box. This is the inverse of Box.box, and is also an expensive operation.
Try
Returns Bool.True if the result indicates a success, else returns Bool.False.
expect Try.Ok(5).is_ok()
Returns Bool.True if the result indicates a failure, else returns Bool.False.
expect Try.Err("uh oh").is_err()
ok_or : Try(ok, _err), ok -> ok
If the result is Ok, returns the value it holds. Otherwise, returns
the given default value.
Note: This function should be used sparingly, because it hides that an error
happened, which will make debugging harder. Prefer using ? to forward errors or
handle them explicitly with match.
expect Try.Err("uh oh").ok_or(42) == 42
expect Try.Ok(7).ok_or(42) == 7
err_or : Try(_ok, err), err -> err
If the result is Err, returns the value it holds. Otherwise, returns
the given default value.
expect Try.Err("uh oh").err_or("fallback") == "uh oh"
expect Try.Ok(7).err_or("fallback") == "fallback"
If the result is Ok, transforms the value it holds by running a conversion
function on it. Then returns a new Ok holding the transformed value. If the
result is Err, this has no effect. Use map_err to transform an Err.
expect Try.Ok(12.I64).map_ok(|n| -n) == Ok(-12)
expect {
err : Try(I64, Str)
err = Err("yipes!")
err.map_ok(|n| -n) == Err("yipes!")
}
Functions like map are common in Roc; see for example List.map and Set.map.
Like map_ok, but the transform function is effectful. If the argument is
an Ok, the effect is run and its return value is wrapped in a new Ok. If
the result is Err, the effect is not run and the Err is returned unchanged.
artist_try.map_ok!(|a| SQL.query!("SELECT * FROM albums WHERE artist_id = ?", [a.id]))
If the result is Err, transforms the value it holds by running a conversion
function on it. Then returns a new Err holding the transformed value. If
the result is Ok, this has no effect. Use map_ok to transform an Ok.
expect [].last().map_err(|_| ProvidedListIsEmpty) == Err(ProvidedListIsEmpty)
expect [4].last().map_err(|_| ProvidedListIsEmpty) == Ok(4.0)
Like map_err, but the transform function is effectful. If the argument is
an Err, the effect is run and its return value is wrapped in a new Err. If
the result is Ok, the effect is not run and the Ok is returned unchanged.
# Log the failure to the database only when the request errored.
request.map_err!(|e| SQL.execute!("INSERT INTO errors (message) VALUES (?)", [e.message]))
is_eq : Try(ok, err), Try(ok, err) -> Bool where { err.is_eq : err, err -> Bool, ok.is_eq : ok, ok -> Bool }
Returns Bool.True if the two Try values are the same variant (Ok or Err) and their contents are pairwise equal. Otherwise, returns Bool.False.
Set
Returns Bool.True if the two sets contain the same values, and Bool.False otherwise.
empty : -> Set(_item)
Creates a new empty Set.
single : item -> Set(item)
Creates a new Set with a single value.
Set.single(42)
Counts the number of values in a given Set.
expect Set.single(42).len() == 1
Check if the set is empty.
Test if a value is in the Set.
Insert a value into a Set.
Removes the value from the given Set.
Retrieve the values in a Set as a List.
Create a Set from a List of values.
Run the given function on each element in the Set, and return
a Set with just the elements for which the function returned Bool.True.
expect Set.from_list([1, 2, 3, 4]).keep_if(|num| num > 2) == Set.from_list([3, 4])
Run the given function on each element in the Set, and return
a Set with just the elements for which the function returned Bool.False.
expect Set.from_list([1, 2, 3, 4]).drop_if(|num| num > 2) == Set.from_list([1, 2])
Combine two Sets by keeping the
union
of all the values.
expect {
a = Set.from_list([1, 2, 3])
b = Set.from_list([3, 4, 5])
Set.union(a, b) == Set.from_list([1, 2, 3, 4, 5])
}
Combine two Sets by keeping the
intersection
of all the values.
expect {
a = Set.from_list([1, 2, 3])
b = Set.from_list([2, 3, 4])
Set.intersection(a, b) == Set.from_list([2, 3])
}
Remove the values in the first Set that are also in the second Set
using the set difference.
expect {
a = Set.from_list([1, 2, 3])
b = Set.from_list([2, 3, 4])
Set.difference(a, b) == Set.from_list([1])
}
Convert each value in the set to something new, by calling a conversion function on each of them. Then return a new set containing the unique converted values.
expect Set.from_list([1, 2, 3]).map(|n| n * 2) == Set.from_list([2, 4, 6])
# Duplicates in the mapped output are collapsed — the result is a Set.
expect Set.from_list([1, -1, 2, -2]).map(|n| n * n) == Set.from_list([1, 4])
Num
Numeral
U8
default : -> U8
Convert a U8 to its decimal string representation.
expect U8.to_str(42) == "42"
Returns Bool.True if the value is 0.
expect U8.is_zero(0)
expect !U8.is_zero(7)
Returns Bool.True if the two values are equal.
expect U8.is_eq(3, 3)
expect !U8.is_eq(3, 4)
Returns Bool.True if the first value is greater than the second.
expect U8.is_gt(5, 3)
expect !U8.is_gt(3, 3)
Returns Bool.True if the first value is greater than or equal to the second.
expect U8.is_gte(3, 3)
expect !U8.is_gte(2, 3)
Returns Bool.True if the first value is less than the second.
expect U8.is_lt(3, 5)
expect !U8.is_lt(3, 3)
Returns Bool.True if the first value is less than or equal to the second.
expect U8.is_lte(3, 3)
expect !U8.is_lte(5, 3)
Returns the greater of two U8 values.
expect U8.max(5, 3) == 5
Returns the smaller of two U8 values.
expect U8.min(5, 3) == 3
Add two U8 values.
expect U8.plus(2, 3) == 5
Subtract the second U8 from the first.
expect U8.minus(5, 3) == 2
Multiply two U8 values.
expect U8.times(4, 3) == 12
Return the remainder of dividing the first U8 by the second.
expect U8.rem_by(7, 3) == 1
Return the absolute difference between two U8 values.
expect U8.abs_diff(2, 5) == 3
expect U8.abs_diff(5, 2) == 3
Shift the bits of a U8 to the left by the given number of positions. Bits shifted past the most significant bit are discarded, and zeros are shifted in on the right. Each left shift by one is equivalent to multiplying by 2 (modulo 256).
expect U8.shift_left_by(1, 3) == 8
expect U8.shift_left_by(0b0000_0101, 2) == 0b0001_0100
Shift the bits of a U8 to the right by the given number of positions. Bits shifted past the least significant bit are discarded, and zeros are shifted in on the left. Each right shift by one is equivalent to integer division by 2. For unsigned integers this behaves the same as shift_right_zf_by.
expect U8.shift_right_by(32, 2) == 8
expect U8.shift_right_by(0b1010_0000, 3) == 0b0001_0100
Shift the bits of a U8 to the right by the given number of positions, filling the vacated high bits with zeros ("zero-fill"). For unsigned integers this behaves the same as shift_right_by.
expect U8.shift_right_zf_by(32, 2) == 8
expect U8.shift_right_zf_by(0b1010_0000, 3) == 0b0001_0100
List of integers beginning with this U8 and ending with the other U8.
(Use until instead to end with the other U8 minus one.)
Returns an empty list if this U8 is greater than the other.
expect U8.to(1, 4) == [1, 2, 3, 4]
expect U8.to(3, 3) == [3]
expect U8.to(5, 2) == []
List of integers beginning with this U8 and ending with the other U8 minus one.
(Use to instead to end with the other U8 exactly, instead of minus one.)
Returns an empty list if this U8 is greater than or equal to the other.
expect U8.until(1, 4) == [1, 2, 3]
expect U8.until(3, 3) == []
expect U8.until(5, 2) == []
Encode a U8 using a format that provides encode_u8
Decode a U8 using a format that provides decode_u8
I8
default : -> I8
Convert an I8 to its decimal string representation.
expect I8.to_str(42) == "42"
expect I8.to_str(-42) == "-42"
Returns Bool.True if the value is 0.
expect I8.is_zero(0)
expect !I8.is_zero(7)
Returns Bool.True if the value is less than 0.
expect I8.is_negative(-3)
expect !I8.is_negative(0)
Returns Bool.True if the value is greater than 0.
expect I8.is_positive(3)
expect !I8.is_positive(0)
Returns Bool.True if the two values are equal.
expect I8.is_eq(3, 3)
expect !I8.is_eq(3, 4)
Returns Bool.True if the first value is greater than the second.
expect I8.is_gt(5, 3)
expect !I8.is_gt(3, 3)
Returns Bool.True if the first value is greater than or equal to the second.
expect I8.is_gte(3, 3)
expect !I8.is_gte(2, 3)
Returns Bool.True if the first value is less than the second.
expect I8.is_lt(3, 5)
expect !I8.is_lt(3, 3)
Returns Bool.True if the first value is less than or equal to the second.
expect I8.is_lte(3, 3)
expect !I8.is_lte(5, 3)
Returns the greater of two I8 values.
expect I8.max(5, 3) == 5
expect I8.max(-3, -1) == -1
Returns the smaller of two I8 values.
expect I8.min(5, 3) == 3
expect I8.min(-3, -1) == -3
Add two I8 values.
expect I8.plus(2, 3) == 5
Subtract the second I8 from the first.
expect I8.minus(5, 3) == 2
Multiply two I8 values.
expect I8.times(4, 3) == 12
Divide the first I8 by the second, truncating toward zero.
expect I8.div_trunc_by(7, 2) == 3
expect I8.div_trunc_by(-7, 2) == -3
Return the remainder of dividing the first I8 by the second. The sign of the result matches the sign of the dividend.
expect I8.rem_by(7, 3) == 1
expect I8.rem_by(-7, 3) == -1
Return the modulus of the first I8 by the second. The modulus is
the remainder left after dividing one number by another, and is
always in the range 0 up to (but not including) the absolute value
of the divisor. Unlike rem_by, the sign of the result matches the
sign of the divisor.
expect I8.mod_by(7, 3) == 1
expect I8.mod_by(-7, 3) == 2
Shift the bits of an I8 to the left by the given number of positions. Bits shifted past the most significant bit are discarded, and zeros are shifted in on the right.
expect I8.shift_left_by(1, 3) == 8
expect I8.shift_left_by(0b0000_0101, 2) == 0b0001_0100
Shift the bits of an I8 to the right by the given number of positions, preserving the sign ("arithmetic shift"). The sign bit is shifted in on the left, so negative values remain negative. Each right shift by one is equivalent to integer division by 2 (rounding toward negative infinity).
expect I8.shift_right_by(32, 2) == 8
expect I8.shift_right_by(-32, 2) == -8
Shift the bits of an I8 to the right by the given number of positions.
expect I8.shift_right_zf_by(32, 2) == 8
expect I8.shift_right_zf_by(0b0101_0000, 3) == 0b0000_1010
List of integers beginning with this I8 and ending with the other I8.
(Use until instead to end with the other I8 minus one.)
Returns an empty list if this I8 is greater than the other.
expect I8.to(1, 4) == [1, 2, 3, 4]
expect I8.to(-2, 1) == [-2, -1, 0, 1]
expect I8.to(5, 2) == []
List of integers beginning with this I8 and ending with the other I8 minus one.
(Use to instead to end with the other I8 exactly, instead of minus one.)
Returns an empty list if this I8 is greater than or equal to the other.
expect I8.until(1, 4) == [1, 2, 3]
expect I8.until(-2, 1) == [-2, -1, 0]
expect I8.until(5, 2) == []
Build an I8 from a list of base-10 digits, most significant first.
Each element of the list must be a digit in the range 0 to 9.
Returns Err(OutOfRange) if the resulting value does not fit in an
I8 (-128 to 127), or if any element is not a valid digit. The
result is always non-negative; to build a negative value, negate
the result.
expect I8.from_int_digits([1, 2, 3]) == Ok(123)
Encode an I8 using a format that provides encode_i8
Decode an I8 using a format that provides decode_i8
U16
default : -> U16
Convert a U16 to its decimal string representation.
expect U16.to_str(42) == "42"
Returns Bool.True if the value is 0.
expect U16.is_zero(0)
expect !U16.is_zero(7)
Returns Bool.True if the two values are equal.
expect U16.is_eq(3, 3)
expect !U16.is_eq(3, 4)
Returns Bool.True if the first value is greater than the second.
expect U16.is_gt(5, 3)
expect !U16.is_gt(3, 3)
Returns Bool.True if the first value is greater than or equal to the second.
expect U16.is_gte(3, 3)
expect !U16.is_gte(2, 3)
Returns Bool.True if the first value is less than the second.
expect U16.is_lt(3, 5)
expect !U16.is_lt(3, 3)
Returns Bool.True if the first value is less than or equal to the second.
expect U16.is_lte(3, 3)
expect !U16.is_lte(5, 3)
Returns the greater of two U16 values.
expect U16.max(5, 3) == 5
Returns the smaller of two U16 values.
expect U16.min(5, 3) == 3
Add two U16 values.
expect U16.plus(2, 3) == 5
Subtract the second U16 from the first.
expect U16.minus(5, 3) == 2
Multiply two U16 values.
expect U16.times(4, 3) == 12
Return the remainder of dividing the first U16 by the second.
expect U16.rem_by(7, 3) == 1
Return the absolute difference between two U16 values.
expect U16.abs_diff(2, 5) == 3
expect U16.abs_diff(5, 2) == 3
Shift the bits of a U16 to the left by the given number of positions. Bits shifted past the most significant bit are discarded, and zeros are shifted in on the right. Each left shift by one is equivalent to multiplying by 2 (modulo 65536).
expect U16.shift_left_by(1, 3) == 8
expect U16.shift_left_by(0b0000_0101, 2) == 0b0001_0100
Shift the bits of a U16 to the right by the given number of positions. Bits shifted past the least significant bit are discarded, and zeros are shifted in on the left. Each right shift by one is equivalent to integer division by 2. For unsigned integers this behaves the same as shift_right_zf_by.
expect U16.shift_right_by(32, 2) == 8
expect U16.shift_right_by(0b1010_0000, 3) == 0b0001_0100
Shift the bits of a U16 to the right by the given number of positions, filling the vacated high bits with zeros ("zero-fill"). For unsigned integers this behaves the same as shift_right_by.
expect U16.shift_right_zf_by(32, 2) == 8
expect U16.shift_right_zf_by(0b1010_0000, 3) == 0b0001_0100
List of integers beginning with this U16 and ending with the other U16.
(Use until instead to end with the other U16 minus one.)
Returns an empty list if this U16 is greater than the other.
expect U16.to(1, 4) == [1, 2, 3, 4]
expect U16.to(3, 3) == [3]
expect U16.to(5, 2) == []
List of integers beginning with this U16 and ending with the other U16 minus one.
(Use to instead to end with the other U16 exactly, instead of minus one.)
Returns an empty list if this U16 is greater than or equal to the other.
expect U16.until(1, 4) == [1, 2, 3]
expect U16.until(3, 3) == []
expect U16.until(5, 2) == []
Build a U16 from a list of base-10 digits, most significant first.
Each element of the list must be a digit in the range 0 to 9.
Returns Err(OutOfRange) if the resulting value does not fit in a
U16 (0 to 65535), or if any element is not a valid digit.
expect U16.from_int_digits([1, 2, 3]) == Ok(123)
I16
default : -> I16
Convert an I16 to its decimal string representation.
expect I16.to_str(42) == "42"
expect I16.to_str(-42) == "-42"
Returns Bool.True if the value is 0.
expect I16.is_zero(0)
expect !I16.is_zero(7)
Returns Bool.True if the value is less than 0.
expect I16.is_negative(-3)
expect !I16.is_negative(0)
Returns Bool.True if the value is greater than 0.
expect I16.is_positive(3)
expect !I16.is_positive(0)
Returns Bool.True if the two values are equal.
expect I16.is_eq(3, 3)
expect !I16.is_eq(3, 4)
Returns Bool.True if the first value is greater than the second.
expect I16.is_gt(5, 3)
expect !I16.is_gt(3, 3)
Returns Bool.True if the first value is greater than or equal to the second.
expect I16.is_gte(3, 3)
expect !I16.is_gte(2, 3)
Returns Bool.True if the first value is less than the second.
expect I16.is_lt(3, 5)
expect !I16.is_lt(3, 3)
Returns Bool.True if the first value is less than or equal to the second.
expect I16.is_lte(3, 3)
expect !I16.is_lte(5, 3)
Returns the greater of two I16 values.
expect I16.max(5, 3) == 5
expect I16.max(-3, -1) == -1
Returns the smaller of two I16 values.
expect I16.min(5, 3) == 3
expect I16.min(-3, -1) == -3
Add two I16 values.
expect I16.plus(2, 3) == 5
Subtract the second I16 from the first.
expect I16.minus(5, 3) == 2
Multiply two I16 values.
expect I16.times(4, 3) == 12
Divide the first I16 by the second, truncating toward zero.
expect I16.div_trunc_by(7, 2) == 3
expect I16.div_trunc_by(-7, 2) == -3
Return the remainder of dividing the first I16 by the second. The sign of the result matches the sign of the dividend.
expect I16.rem_by(7, 3) == 1
expect I16.rem_by(-7, 3) == -1
Return the modulus of the first I16 by the second. The modulus is
the remainder left after dividing one number by another, and is
always in the range 0 up to (but not including) the absolute value
of the divisor. Unlike rem_by, the sign of the result matches the
sign of the divisor.
expect I16.mod_by(7, 3) == 1
expect I16.mod_by(-7, 3) == 2
Shift the bits of an I16 to the left by the given number of positions. Bits shifted past the most significant bit are discarded, and zeros are shifted in on the right.
expect I16.shift_left_by(1, 3) == 8
expect I16.shift_left_by(0b0000_0101, 2) == 0b0001_0100
Shift the bits of an I16 to the right by the given number of positions, preserving the sign ("arithmetic shift"). The sign bit is shifted in on the left, so negative values remain negative. Each right shift by one is equivalent to integer division by 2 (rounding toward negative infinity).
expect I16.shift_right_by(32, 2) == 8
expect I16.shift_right_by(-32, 2) == -8
Shift the bits of an I16 to the right by the given number of positions.
expect I16.shift_right_zf_by(32, 2) == 8
expect I16.shift_right_zf_by(0b0101_0000, 3) == 0b0000_1010
List of integers beginning with this I16 and ending with the other I16.
(Use until instead to end with the other I16 minus one.)
Returns an empty list if this I16 is greater than the other.
expect I16.to(1, 4) == [1, 2, 3, 4]
expect I16.to(-2, 1) == [-2, -1, 0, 1]
expect I16.to(5, 2) == []
List of integers beginning with this I16 and ending with the other I16 minus one.
(Use to instead to end with the other I16 exactly, instead of minus one.)
Returns an empty list if this I16 is greater than or equal to the other.
expect I16.until(1, 4) == [1, 2, 3]
expect I16.until(-2, 1) == [-2, -1, 0]
expect I16.until(5, 2) == []
Build an I16 from a list of base-10 digits, most significant first.
Each element of the list must be a digit in the range 0 to 9.
Returns Err(OutOfRange) if the resulting value does not fit in an
I16 (-32768 to 32767), or if any element is not a valid digit.
The result is always non-negative; to build a negative value, negate
the result.
expect I16.from_int_digits([1, 2, 3]) == Ok(123)
Convert an I16 to a U64, sign-extending the bits on overflow. Non-negative values are preserved; negative values wrap into the upper end of the U64 range (two's complement reinterpretation of the sign-extended bits).
expect I16.to_u64_wrap(42) == 42
expect I16.to_u64_wrap(-1) == 18446744073709551615
U32
default : -> U32
Convert a U32 to its decimal string representation.
expect U32.to_str(42) == "42"
Returns Bool.True if the value is 0.
expect U32.is_zero(0)
expect !U32.is_zero(7)
Returns Bool.True if the two values are equal.
expect U32.is_eq(3, 3)
expect !U32.is_eq(3, 4)
Returns Bool.True if the first value is greater than the second.
expect U32.is_gt(5, 3)
expect !U32.is_gt(3, 3)
Returns Bool.True if the first value is greater than or equal to the second.
expect U32.is_gte(3, 3)
expect !U32.is_gte(2, 3)
Returns Bool.True if the first value is less than the second.
expect U32.is_lt(3, 5)
expect !U32.is_lt(3, 3)
Returns Bool.True if the first value is less than or equal to the second.
expect U32.is_lte(3, 3)
expect !U32.is_lte(5, 3)
Returns the greater of two U32 values.
expect U32.max(5, 3) == 5
Returns the smaller of two U32 values.
expect U32.min(5, 3) == 3
Add two U32 values.
expect U32.plus(2, 3) == 5
Subtract the second U32 from the first.
expect U32.minus(5, 3) == 2
Multiply two U32 values.
expect U32.times(4, 3) == 12
Return the remainder of dividing the first U32 by the second.
expect U32.rem_by(7, 3) == 1
Return the absolute difference between two U32 values.
expect U32.abs_diff(2, 5) == 3
expect U32.abs_diff(5, 2) == 3
Shift the bits of a U32 to the left by the given number of positions. Bits shifted past the most significant bit are discarded, and zeros are shifted in on the right. Each left shift by one is equivalent to multiplying by 2 (modulo 4294967296).
expect U32.shift_left_by(1, 3) == 8
expect U32.shift_left_by(0b0000_0101, 2) == 0b0001_0100
Shift the bits of a U32 to the right by the given number of positions. Bits shifted past the least significant bit are discarded, and zeros are shifted in on the left. Each right shift by one is equivalent to integer division by 2. For unsigned integers this behaves the same as shift_right_zf_by.
expect U32.shift_right_by(32, 2) == 8
expect U32.shift_right_by(0b1010_0000, 3) == 0b0001_0100
Shift the bits of a U32 to the right by the given number of positions, filling the vacated high bits with zeros ("zero-fill"). For unsigned integers this behaves the same as shift_right_by.
expect U32.shift_right_zf_by(32, 2) == 8
expect U32.shift_right_zf_by(0b1010_0000, 3) == 0b0001_0100
List of integers beginning with this U32 and ending with the other U32.
(Use until instead to end with the other U32 minus one.)
Returns an empty list if this U32 is greater than the other.
expect U32.to(1, 4) == [1, 2, 3, 4]
expect U32.to(3, 3) == [3]
expect U32.to(5, 2) == []
List of integers beginning with this U32 and ending with the other U32 minus one.
(Use to instead to end with the other U32 exactly, instead of minus one.)
Returns an empty list if this U32 is greater than or equal to the other.
expect U32.until(1, 4) == [1, 2, 3]
expect U32.until(3, 3) == []
expect U32.until(5, 2) == []
Build a U32 from a list of base-10 digits, most significant first.
Each element of the list must be a digit in the range 0 to 9.
Returns Err(OutOfRange) if the resulting value does not fit in a
U32 (0 to 4294967295), or if any element is not a valid digit.
expect U32.from_int_digits([1, 2, 3]) == Ok(123)
Convert a U32 to an I32, wrapping on overflow. Values from 0 to
2147483647 are preserved; values from 2147483648 to 4294967295
wrap into the negative range -2147483648 to -1 (two's complement
reinterpretation of the bits).
expect U32.to_i32_wrap(42) == 42
expect U32.to_i32_wrap(3000000000) == -1294967296
I32
default : -> I32
Convert an I32 to its decimal string representation.
expect I32.to_str(42) == "42"
expect I32.to_str(-42) == "-42"
Returns Bool.True if the value is 0.
expect I32.is_zero(0)
expect !I32.is_zero(7)
Returns Bool.True if the value is less than 0.
expect I32.is_negative(-3)
expect !I32.is_negative(0)
Returns Bool.True if the value is greater than 0.
expect I32.is_positive(3)
expect !I32.is_positive(0)
Returns Bool.True if the two values are equal.
expect I32.is_eq(3, 3)
expect !I32.is_eq(3, 4)
Returns Bool.True if the first value is greater than the second.
expect I32.is_gt(5, 3)
expect !I32.is_gt(3, 3)
Returns Bool.True if the first value is greater than or equal to the second.
expect I32.is_gte(3, 3)
expect !I32.is_gte(2, 3)
Returns Bool.True if the first value is less than the second.
expect I32.is_lt(3, 5)
expect !I32.is_lt(3, 3)
Returns Bool.True if the first value is less than or equal to the second.
expect I32.is_lte(3, 3)
expect !I32.is_lte(5, 3)
Returns the greater of two I32 values.
expect I32.max(5, 3) == 5
expect I32.max(-3, -1) == -1
Returns the smaller of two I32 values.
expect I32.min(5, 3) == 3
expect I32.min(-3, -1) == -3
Add two I32 values.
expect I32.plus(2, 3) == 5
Subtract the second I32 from the first.
expect I32.minus(5, 3) == 2
Multiply two I32 values.
expect I32.times(4, 3) == 12
Divide the first I32 by the second, truncating toward zero.
expect I32.div_trunc_by(7, 2) == 3
expect I32.div_trunc_by(-7, 2) == -3
Return the remainder of dividing the first I32 by the second. The sign of the result matches the sign of the dividend.
expect I32.rem_by(7, 3) == 1
expect I32.rem_by(-7, 3) == -1
Return the modulus of the first I32 by the second. The modulus is
the remainder left after dividing one number by another, and is
always in the range 0 up to (but not including) the absolute value
of the divisor. Unlike rem_by, the sign of the result matches the
sign of the divisor.
expect I32.mod_by(7, 3) == 1
expect I32.mod_by(-7, 3) == 2
Shift the bits of an I32 to the left by the given number of positions. Bits shifted past the most significant bit are discarded, and zeros are shifted in on the right.
expect I32.shift_left_by(1, 3) == 8
expect I32.shift_left_by(0b0000_0101, 2) == 0b0001_0100
Shift the bits of an I32 to the right by the given number of positions, preserving the sign ("arithmetic shift"). The sign bit is shifted in on the left, so negative values remain negative. Each right shift by one is equivalent to integer division by 2 (rounding toward negative infinity).
expect I32.shift_right_by(32, 2) == 8
expect I32.shift_right_by(-32, 2) == -8
Shift the bits of an I32 to the right by the given number of positions.
expect I32.shift_right_zf_by(32, 2) == 8
expect I32.shift_right_zf_by(0b0101_0000, 3) == 0b0000_1010
List of integers beginning with this I32 and ending with the other I32.
(Use until instead to end with the other I32 minus one.)
Returns an empty list if this I32 is greater than the other.
expect I32.to(1, 4) == [1, 2, 3, 4]
expect I32.to(-2, 1) == [-2, -1, 0, 1]
expect I32.to(5, 2) == []
List of integers beginning with this I32 and ending with the other I32 minus one.
(Use to instead to end with the other I32 exactly, instead of minus one.)
Returns an empty list if this I32 is greater than or equal to the other.
expect I32.until(1, 4) == [1, 2, 3]
expect I32.until(-2, 1) == [-2, -1, 0]
expect I32.until(5, 2) == []
Build an I32 from a list of base-10 digits, most significant first.
Each element of the list must be a digit in the range 0 to 9.
Returns Err(OutOfRange) if the resulting value does not fit in an
I32 (-2147483648 to 2147483647), or if any element is not a valid digit.
The result is always non-negative; to build a negative value, negate
the result.
expect I32.from_int_digits([1, 2, 3]) == Ok(123)
Convert an I32 to a U64, sign-extending the bits on overflow. Non-negative values are preserved; negative values wrap into the upper end of the U64 range (two's complement reinterpretation of the sign-extended bits).
expect I32.to_u64_wrap(42) == 42
expect I32.to_u64_wrap(-1) == 18446744073709551615
U64
default : -> U64
Convert a U64 to its decimal string representation.
expect U64.to_str(42) == "42"
Returns Bool.True if the value is 0.
expect U64.is_zero(0)
expect !U64.is_zero(7)
Returns Bool.True if the two values are equal.
expect U64.is_eq(3, 3)
expect !U64.is_eq(3, 4)
Returns Bool.True if the first value is greater than the second.
expect U64.is_gt(5, 3)
expect !U64.is_gt(3, 3)
Returns Bool.True if the first value is greater than or equal to the second.
expect U64.is_gte(3, 3)
expect !U64.is_gte(2, 3)
Returns Bool.True if the first value is less than the second.
expect U64.is_lt(3, 5)
expect !U64.is_lt(3, 3)
Returns Bool.True if the first value is less than or equal to the second.
expect U64.is_lte(3, 3)
expect !U64.is_lte(5, 3)
Returns the greater of two U64 values.
expect U64.max(5, 3) == 5
Returns the smaller of two U64 values.
expect U64.min(5, 3) == 3
Add two U64 values.
expect U64.plus(2, 3) == 5
Subtract the second U64 from the first.
expect U64.minus(5, 3) == 2
Multiply two U64 values.
expect U64.times(4, 3) == 12
Return the remainder of dividing the first U64 by the second.
expect U64.rem_by(7, 3) == 1
Return the absolute difference between two U64 values.
expect U64.abs_diff(2, 5) == 3
expect U64.abs_diff(5, 2) == 3
Shift the bits of a U64 to the left by the given number of positions. Bits shifted past the most significant bit are discarded, and zeros are shifted in on the right. Each left shift by one is equivalent to multiplying by 2 (modulo 2^64).
expect U64.shift_left_by(1, 3) == 8
expect U64.shift_left_by(0b0000_0101, 2) == 0b0001_0100
Shift the bits of a U64 to the right by the given number of positions. Bits shifted past the least significant bit are discarded, and zeros are shifted in on the left. Each right shift by one is equivalent to integer division by 2. For unsigned integers this behaves the same as shift_right_zf_by.
expect U64.shift_right_by(32, 2) == 8
expect U64.shift_right_by(0b1010_0000, 3) == 0b0001_0100
Shift the bits of a U64 to the right by the given number of positions, filling the vacated high bits with zeros ("zero-fill"). For unsigned integers this behaves the same as shift_right_by.
expect U64.shift_right_zf_by(32, 2) == 8
expect U64.shift_right_zf_by(0b1010_0000, 3) == 0b0001_0100
List of integers beginning with this U64 and ending with the other U64.
(Use until instead to end with the other U64 minus one.)
Returns an empty list if this U64 is greater than the other.
expect U64.to(1, 4) == [1, 2, 3, 4]
expect U64.to(3, 3) == [3]
expect U64.to(5, 2) == []
List of integers beginning with this U64 and ending with the other U64 minus one.
(Use to instead to end with the other U64 exactly, instead of minus one.)
Returns an empty list if this U64 is greater than or equal to the other.
expect U64.until(1, 4) == [1, 2, 3]
expect U64.until(3, 3) == []
expect U64.until(5, 2) == []
Build a U64 from a list of base-10 digits, most significant first.
Each element of the list must be a digit in the range 0 to 9.
Returns Err(OutOfRange) if the resulting value does not fit in a
U64 (0 to 18446744073709551615), or if any element is not a valid digit.
expect U64.from_int_digits([1, 2, 3]) == Ok(123)
Convert a U64 to an I64, wrapping on overflow. Values from 0 to
9223372036854775807 are preserved; values from 9223372036854775808
to 18446744073709551615 wrap into the negative range
-9223372036854775808 to -1 (two's complement reinterpretation of
the bits).
expect U64.to_i64_wrap(42) == 42
expect U64.to_i64_wrap(10000000000000000000) == -8446744073709551616
I64
default : -> I64
Convert an I64 to its decimal string representation.
expect I64.to_str(42) == "42"
expect I64.to_str(-42) == "-42"
Returns Bool.True if the value is 0.
expect I64.is_zero(0)
expect !I64.is_zero(7)
Returns Bool.True if the value is less than 0.
expect I64.is_negative(-3)
expect !I64.is_negative(0)
Returns Bool.True if the value is greater than 0.
expect I64.is_positive(3)
expect !I64.is_positive(0)
Returns Bool.True if the two values are equal.
expect I64.is_eq(3, 3)
expect !I64.is_eq(3, 4)
Returns Bool.True if the first value is greater than the second.
expect I64.is_gt(5, 3)
expect !I64.is_gt(3, 3)
Returns Bool.True if the first value is greater than or equal to the second.
expect I64.is_gte(3, 3)
expect !I64.is_gte(2, 3)
Returns Bool.True if the first value is less than the second.
expect I64.is_lt(3, 5)
expect !I64.is_lt(3, 3)
Returns Bool.True if the first value is less than or equal to the second.
expect I64.is_lte(3, 3)
expect !I64.is_lte(5, 3)
Returns the greater of two I64 values.
expect I64.max(5, 3) == 5
expect I64.max(-3, -1) == -1
Returns the smaller of two I64 values.
expect I64.min(5, 3) == 3
expect I64.min(-3, -1) == -3
Add two I64 values.
expect I64.plus(2, 3) == 5
Subtract the second I64 from the first.
expect I64.minus(5, 3) == 2
Multiply two I64 values.
expect I64.times(4, 3) == 12
Divide the first I64 by the second, truncating toward zero.
expect I64.div_trunc_by(7, 2) == 3
expect I64.div_trunc_by(-7, 2) == -3
Return the remainder of dividing the first I64 by the second. The sign of the result matches the sign of the dividend.
expect I64.rem_by(7, 3) == 1
expect I64.rem_by(-7, 3) == -1
Return the modulus of the first I64 by the second. The modulus is
the remainder left after dividing one number by another, and is
always in the range 0 up to (but not including) the absolute value
of the divisor. Unlike rem_by, the sign of the result matches the
sign of the divisor.
expect I64.mod_by(7, 3) == 1
expect I64.mod_by(-7, 3) == 2
Shift the bits of an I64 to the left by the given number of positions. Bits shifted past the most significant bit are discarded, and zeros are shifted in on the right.
expect I64.shift_left_by(1, 3) == 8
expect I64.shift_left_by(0b0000_0101, 2) == 0b0001_0100
Shift the bits of an I64 to the right by the given number of positions, preserving the sign ("arithmetic shift"). The sign bit is shifted in on the left, so negative values remain negative. Each right shift by one is equivalent to integer division by 2 (rounding toward negative infinity).
expect I64.shift_right_by(32, 2) == 8
expect I64.shift_right_by(-32, 2) == -8
Shift the bits of an I64 to the right by the given number of positions.
expect I64.shift_right_zf_by(32, 2) == 8
expect I64.shift_right_zf_by(0b0101_0000, 3) == 0b0000_1010
List of integers beginning with this I64 and ending with the other I64.
(Use until instead to end with the other I64 minus one.)
Returns an empty list if this I64 is greater than the other.
expect I64.to(1, 4) == [1, 2, 3, 4]
expect I64.to(-2, 1) == [-2, -1, 0, 1]
expect I64.to(5, 2) == []
List of integers beginning with this I64 and ending with the other I64 minus one.
(Use to instead to end with the other I64 exactly, instead of minus one.)
Returns an empty list if this I64 is greater than or equal to the other.
expect I64.until(1, 4) == [1, 2, 3]
expect I64.until(-2, 1) == [-2, -1, 0]
expect I64.until(5, 2) == []
Build an I64 from a list of base-10 digits, most significant first.
Each element of the list must be a digit in the range 0 to 9.
Returns Err(OutOfRange) if the resulting value does not fit in an
I64 (-9223372036854775808 to 9223372036854775807), or if any
element is not a valid digit. The result is always non-negative; to
build a negative value, negate the result.
expect I64.from_int_digits([1, 2, 3]) == Ok(123)
U128
default : -> U128
Convert a U128 to its decimal string representation.
expect U128.to_str(42) == "42"
Returns Bool.True if the value is 0.
expect U128.is_zero(0)
expect !U128.is_zero(7)
Returns Bool.True if the two values are equal.
expect U128.is_eq(3, 3)
expect !U128.is_eq(3, 4)
Returns Bool.True if the first value is greater than the second.
expect U128.is_gt(5, 3)
expect !U128.is_gt(3, 3)
Returns Bool.True if the first value is greater than or equal to the second.
expect U128.is_gte(3, 3)
expect !U128.is_gte(2, 3)
Returns Bool.True if the first value is less than the second.
expect U128.is_lt(3, 5)
expect !U128.is_lt(3, 3)
Returns Bool.True if the first value is less than or equal to the second.
expect U128.is_lte(3, 3)
expect !U128.is_lte(5, 3)
Returns the greater of two U128 values.
expect U128.max(5, 3) == 5
Returns the smaller of two U128 values.
expect U128.min(5, 3) == 3
Add two U128 values.
expect U128.plus(2, 3) == 5
Subtract the second U128 from the first.
expect U128.minus(5, 3) == 2
Multiply two U128 values.
expect U128.times(4, 3) == 12
Return the remainder of dividing the first U128 by the second.
expect U128.rem_by(7, 3) == 1
Return the absolute difference between two U128 values.
expect U128.abs_diff(2, 5) == 3
expect U128.abs_diff(5, 2) == 3
Shift the bits of a U128 to the left by the given number of positions. Bits shifted past the most significant bit are discarded, and zeros are shifted in on the right. Each left shift by one is equivalent to multiplying by 2 (modulo 2^128).
expect U128.shift_left_by(1, 3) == 8
expect U128.shift_left_by(0b0000_0101, 2) == 0b0001_0100
Shift the bits of a U128 to the right by the given number of positions. Bits shifted past the least significant bit are discarded, and zeros are shifted in on the left. Each right shift by one is equivalent to integer division by 2. For unsigned integers this behaves the same as shift_right_zf_by.
expect U128.shift_right_by(32, 2) == 8
expect U128.shift_right_by(0b1010_0000, 3) == 0b0001_0100
Shift the bits of a U128 to the right by the given number of positions, filling the vacated high bits with zeros ("zero-fill"). For unsigned integers this behaves the same as shift_right_by.
expect U128.shift_right_zf_by(32, 2) == 8
expect U128.shift_right_zf_by(0b1010_0000, 3) == 0b0001_0100
List of integers beginning with this U128 and ending with the other U128.
(Use until instead to end with the other U128 minus one.)
Returns an empty list if this U128 is greater than the other.
expect U128.to(1, 4) == [1, 2, 3, 4]
expect U128.to(3, 3) == [3]
expect U128.to(5, 2) == []
List of integers beginning with this U128 and ending with the other U128 minus one.
(Use to instead to end with the other U128 exactly, instead of minus one.)
Returns an empty list if this U128 is greater than or equal to the other.
expect U128.until(1, 4) == [1, 2, 3]
expect U128.until(3, 3) == []
expect U128.until(5, 2) == []
Build a U128 from a list of base-10 digits, most significant first.
Each element of the list must be a digit in the range 0 to 9.
Returns Err(OutOfRange) if the resulting value does not fit in a
U128 (0 to 340282366920938463463374607431768211455), or if any
element is not a valid digit.
expect U128.from_int_digits([1, 2, 3]) == Ok(123)
Convert a U128 to an I64, wrapping on overflow. Values from 0
to 9223372036854775807 are preserved; larger values wrap by
truncating to the low 64 bits and reinterpreting them in two's
complement.
expect U128.to_i64_wrap(42) == 42
expect U128.to_i64_wrap(10000000000000000000) == -8446744073709551616
Convert a U128 to an I128, wrapping on overflow. Values from 0
to 170141183460469231731687303715884105727 are preserved; larger
values wrap into the negative range (two's complement reinterpretation
of the bits).
expect U128.to_i128_wrap(42) == 42
expect U128.to_i128_wrap(200000000000000000000000000000000000000) == -140282366920938463463374607431768211456
Convert a U128 to an I128, returning Err(OutOfRange) if the value
does not fit. Values from 0 to 170141183460469231731687303715884105727
succeed; larger values return Err(OutOfRange).
expect U128.to_i128_try(42) == Ok(42)
expect U128.to_i128_try(200000000000000000000000000000000000000) == Err(OutOfRange)
I128
default : -> I128
Convert an I128 to its decimal string representation.
expect I128.to_str(42) == "42"
expect I128.to_str(-42) == "-42"
Returns Bool.True if the value is 0.
expect I128.is_zero(0)
expect !I128.is_zero(7)
Returns Bool.True if the value is less than 0.
expect I128.is_negative(-3)
expect !I128.is_negative(0)
Returns Bool.True if the value is greater than 0.
expect I128.is_positive(3)
expect !I128.is_positive(0)
Returns Bool.True if the two values are equal.
expect I128.is_eq(3, 3)
expect !I128.is_eq(3, 4)
Returns Bool.True if the first value is greater than the second.
expect I128.is_gt(5, 3)
expect !I128.is_gt(3, 3)
Returns Bool.True if the first value is greater than or equal to the second.
expect I128.is_gte(3, 3)
expect !I128.is_gte(2, 3)
Returns Bool.True if the first value is less than the second.
expect I128.is_lt(3, 5)
expect !I128.is_lt(3, 3)
Returns Bool.True if the first value is less than or equal to the second.
expect I128.is_lte(3, 3)
expect !I128.is_lte(5, 3)
Returns the greater of two I128 values.
expect I128.max(5, 3) == 5
expect I128.max(-3, -1) == -1
Returns the smaller of two I128 values.
expect I128.min(5, 3) == 3
expect I128.min(-3, -1) == -3
Add two I128 values.
expect I128.plus(2, 3) == 5
Subtract the second I128 from the first.
expect I128.minus(5, 3) == 2
Multiply two I128 values.
expect I128.times(4, 3) == 12
Divide the first I128 by the second, truncating toward zero.
expect I128.div_trunc_by(7, 2) == 3
expect I128.div_trunc_by(-7, 2) == -3
Return the remainder of dividing the first I128 by the second. The sign of the result matches the sign of the dividend.
expect I128.rem_by(7, 3) == 1
expect I128.rem_by(-7, 3) == -1
Return the modulus of the first I128 by the second. The modulus is
the remainder left after dividing one number by another, and is
always in the range 0 up to (but not including) the absolute value
of the divisor. Unlike rem_by, the sign of the result matches the
sign of the divisor.
expect I128.mod_by(7, 3) == 1
expect I128.mod_by(-7, 3) == 2
Shift the bits of an I128 to the left by the given number of positions. Bits shifted past the most significant bit are discarded, and zeros are shifted in on the right.
expect I128.shift_left_by(1, 3) == 8
expect I128.shift_left_by(0b0000_0101, 2) == 0b0001_0100
Shift the bits of an I128 to the right by the given number of positions, preserving the sign ("arithmetic shift"). The sign bit is shifted in on the left, so negative values remain negative. Each right shift by one is equivalent to integer division by 2 (rounding toward negative infinity).
expect I128.shift_right_by(32, 2) == 8
expect I128.shift_right_by(-32, 2) == -8
Shift the bits of an I128 to the right by the given number of positions.
expect I128.shift_right_zf_by(32, 2) == 8
expect I128.shift_right_zf_by(0b0101_0000, 3) == 0b0000_1010
List of integers beginning with this I128 and ending with the other I128.
(Use until instead to end with the other I128 minus one.)
Returns an empty list if this I128 is greater than the other.
expect I128.to(1, 4) == [1, 2, 3, 4]
expect I128.to(-2, 1) == [-2, -1, 0, 1]
expect I128.to(5, 2) == []
List of integers beginning with this I128 and ending with the other I128 minus one.
(Use to instead to end with the other I128 exactly, instead of minus one.)
Returns an empty list if this I128 is greater than or equal to the other.
expect I128.until(1, 4) == [1, 2, 3]
expect I128.until(-2, 1) == [-2, -1, 0]
expect I128.until(5, 2) == []
Build an I128 from a list of base-10 digits, most significant first.
Each element of the list must be a digit in the range 0 to 9.
Returns Err(OutOfRange) if the resulting value does not fit in an
I128 (-170141183460469231731687303715884105728 to
170141183460469231731687303715884105727), or if any element is not
a valid digit. The result is always non-negative; to build a negative
value, negate the result.
expect I128.from_int_digits([1, 2, 3]) == Ok(123)
Encode an I128 using a format that provides encode_i128
decode : src, fmt -> (Try(I128, err), src) where { fmt.decode_i128 : fmt, src -> (Try(I128, err), src) }
Decode an I128 using a format that provides decode_i128
Dec
default : -> Dec
Convert a Dec to its decimal string representation.
expect Dec.to_str(42.5) == "42.5"
expect Dec.to_str(-42.5) == "-42.5"
Returns Bool.True if the value is 0.0.
expect Dec.is_zero(0.0)
expect !Dec.is_zero(0.1)
Returns Bool.True if the value is less than 0.0.
expect Dec.is_negative(-0.1)
expect !Dec.is_negative(0.0)
Returns Bool.True if the value is greater than 0.0.
expect Dec.is_positive(0.1)
expect !Dec.is_positive(0.0)
Returns Bool.True if the two values are equal.
expect Dec.is_eq(3.5, 3.5)
expect !Dec.is_eq(3.5, 4.0)
Returns Bool.True if the first value is greater than the second.
expect Dec.is_gt(5.0, 3.0)
expect !Dec.is_gt(3.0, 3.0)
Returns Bool.True if the first value is greater than or equal to the second.
expect Dec.is_gte(3.0, 3.0)
expect !Dec.is_gte(2.0, 3.0)
Returns Bool.True if the first value is less than the second.
expect Dec.is_lt(3.0, 5.0)
expect !Dec.is_lt(3.0, 3.0)
Returns Bool.True if the first value is less than or equal to the second.
expect Dec.is_lte(3.0, 3.0)
expect !Dec.is_lte(5.0, 3.0)
Returns the greater of two Dec values.
expect Dec.max(5, 3) == 5
expect Dec.max(-3, -1) == -1
Returns the smaller of two Dec values.
expect Dec.min(5, 3) == 3
expect Dec.min(-3, -1) == -3
Negate a Dec.
expect Dec.negate(3.5) == -3.5
expect Dec.negate(-3.5) == 3.5
Return the absolute value of a Dec.
expect Dec.abs(3.5) == 3.5
expect Dec.abs(-3.5) == 3.5
Add two Dec values.
expect Dec.plus(1.5, 2.5) == 4.0
Subtract the second Dec from the first.
expect Dec.minus(5.0, 3.5) == 1.5
Multiply two Dec values.
expect Dec.times(2.5, 4.0) == 10.0
Return the remainder of dividing the first Dec by the second. The sign of the result matches the sign of the dividend.
expect Dec.rem_by(7.5, 2.0) == 1.5
expect Dec.rem_by(-7.5, 2.0) == -1.5
Return the absolute difference between two Dec values.
expect Dec.abs_diff(2.5, 5.0) == 2.5
expect Dec.abs_diff(-1.5, 5.0) == 6.5
Build a Dec from a list of base-10 digits, most significant
first. Each element of the list must be a digit in the range 0
to 9. Returns Err(OutOfRange) if the resulting value does not
fit in a Dec, or if any element is not a valid digit. The result is always
non-negative; to build a negative value, negate the result.
expect Dec.from_int_digits([1, 2, 3]) == Ok(123.0)
Build a Dec from a tuple of (integer digits, fractional digits),
each as a list of base-10 digits most significant first. Each
element of both lists must be a digit in the range 0 to 9.
Returns Err(OutOfRange) if the resulting value does not fit in a
Dec, or if any element is not a valid digit. The result is
always non-negative; to build a negative value, negate the
result.
expect Dec.from_dec_digits(([1, 2], [5])) == Ok(12.5)
Convert a Dec to an I8. The fractional part is truncated
toward zero; the integer part wraps on overflow. Integer-part
values from -128 to 127 are preserved; other values wrap by
truncating to the low 8 bits and reinterpreting them in two's
complement.
expect Dec.to_i8_wrap(42.7) == 42
expect Dec.to_i8_wrap(200.0) == -56
Convert a Dec to an I16. The fractional part is truncated
toward zero; the integer part wraps on overflow. Integer-part
values from -32768 to 32767 are preserved; other values wrap
by truncating to the low 16 bits and reinterpreting them in two's
complement.
expect Dec.to_i16_wrap(42.5) == 42
expect Dec.to_i16_wrap(40000.0) == -25536
Convert a Dec to an I16, returning Err(OutOfRange) if the
integer part does not fit. The fractional part is truncated toward
zero. Integer-part values from -32768 to 32767 succeed; other
values return Err(OutOfRange).
expect Dec.to_i16_try(42.5) == Ok(42)
expect Dec.to_i16_try(40000.0) == Err(OutOfRange)
Convert a Dec to an I32. The fractional part is truncated
toward zero; the integer part wraps on overflow. Integer-part
values from -2147483648 to 2147483647 are preserved; other
values wrap by truncating to the low 32 bits and reinterpreting
them in two's complement.
expect Dec.to_i32_wrap(42.5) == 42
expect Dec.to_i32_wrap(3000000000.0) == -1294967296
Convert a Dec to an I32, returning Err(OutOfRange) if the
integer part does not fit. The fractional part is truncated toward
zero. Integer-part values from -2147483648 to 2147483647
succeed; other values return Err(OutOfRange).
expect Dec.to_i32_try(42.5) == Ok(42)
expect Dec.to_i32_try(3000000000.0) == Err(OutOfRange)
Convert a Dec to an I64. The fractional part is truncated
toward zero; the integer part wraps on overflow. Integer-part
values from -9223372036854775808 to 9223372036854775807 are
preserved; other values wrap by truncating to the low 64 bits and
reinterpreting them in two's complement.
expect Dec.to_i64_wrap(42.5) == 42
Convert a Dec to a U8. The fractional part is truncated
toward zero; the integer part wraps on overflow. Integer-part
values from 0 to 255 are preserved; other values wrap by
truncating to the low 8 bits (two's complement reinterpretation
of those bits).
expect Dec.to_u8_wrap(42.7) == 42
expect Dec.to_u8_wrap(-1.0) == 255
Convert a Dec to a U16. The fractional part is truncated
toward zero; the integer part wraps on overflow. Integer-part
values from 0 to 65535 are preserved; other values wrap by
truncating to the low 16 bits (two's complement reinterpretation
of those bits).
expect Dec.to_u16_wrap(42.5) == 42
expect Dec.to_u16_wrap(-1.0) == 65535
Convert a Dec to a U32. The fractional part is truncated
toward zero; the integer part wraps on overflow. Integer-part
values from 0 to 4294967295 are preserved; other values wrap
by truncating to the low 32 bits (two's complement
reinterpretation of those bits).
expect Dec.to_u32_wrap(42.5) == 42
expect Dec.to_u32_wrap(-1.0) == 4294967295
Convert a Dec to a U32, returning Err(OutOfRange) if the
integer part does not fit. The fractional part is truncated toward
zero. Integer-part values from 0 to 4294967295 succeed; other
values return Err(OutOfRange).
expect Dec.to_u32_try(42.5) == Ok(42)
expect Dec.to_u32_try(-1.0) == Err(OutOfRange)
Convert a Dec to a U64. The fractional part is truncated
toward zero; the integer part wraps on overflow. Integer-part
values from 0 to 18446744073709551615 are preserved; other
values wrap by truncating to the low 64 bits (two's complement
reinterpretation of those bits).
expect Dec.to_u64_wrap(42.5) == 42
expect Dec.to_u64_wrap(-1.0) == 18446744073709551615
List of decimals beginning with this Dec and ending with the
other Dec, stepping by 1.0. (Use until instead to end with
the other Dec minus one.) Returns an empty list if this Dec
is greater than the other.
expect Dec.to(1.0, 4.0) == [1.0, 2.0, 3.0, 4.0]
expect Dec.to(-2.0, 1.0) == [-2.0, -1.0, 0.0, 1.0]
expect Dec.to(5.0, 2.0) == []
List of decimals beginning with this Dec and ending with the
other Dec minus one, stepping by 1.0. (Use to instead to
end with the other Dec exactly, instead of minus one.) Returns
an empty list if this Dec is greater than or equal to the
other.
expect Dec.until(1.0, 4.0) == [1.0, 2.0, 3.0]
expect Dec.until(-2.0, 1.0) == [-2.0, -1.0, 0.0]
expect Dec.until(5.0, 2.0) == []
Encode a Dec using a format that provides encode_dec
decode : src, fmt -> (Try(Dec, err), src) where { fmt.decode_dec : fmt, src -> (Try(Dec, err), src) }
Decode a Dec using a format that provides decode_dec
F32
default : -> F32
Convert an F32 to its decimal string representation.
expect F32.to_str(42.5) == "42.5"
expect F32.to_str(-42.5) == "-42.5"
Returns Bool.True if the value is 0.0. Both positive and
negative zero return Bool.True.
expect F32.is_zero(0.0)
expect !F32.is_zero(0.5)
Returns Bool.True if the value is less than 0.0.
expect F32.is_negative(-0.5)
expect !F32.is_negative(0.0)
Returns Bool.True if the value is greater than 0.0.
expect F32.is_positive(0.5)
expect !F32.is_positive(0.0)
Returns Bool.True if the first value is greater than the second.
Comparisons involving NaN always return Bool.False.
expect F32.is_gt(5.0, 3.0)
expect !F32.is_gt(3.0, 3.0)
Returns Bool.True if the first value is greater than or equal to
the second. Comparisons involving NaN always return Bool.False.
expect F32.is_gte(3.0, 3.0)
expect !F32.is_gte(2.0, 3.0)
Returns Bool.True if the first value is less than the second.
Comparisons involving NaN always return Bool.False.
expect F32.is_lt(3.0, 5.0)
expect !F32.is_lt(3.0, 3.0)
Returns Bool.True if the first value is less than or equal to the
second. Comparisons involving NaN always return Bool.False.
expect F32.is_lte(3.0, 3.0)
expect !F32.is_lte(5.0, 3.0)
Returns the greater of two F32 values.
expect F32.max(5, 3) == 5
expect F32.max(-3, -1) == -1
Returns the smaller of two F32 values.
expect F32.min(5, 3) == 3
expect F32.min(-3, -1) == -3
Negate an F32. Flips the sign bit, so negating 0.0 produces
-0.0 and negating NaN produces NaN.
expect F32.negate(3.5).to_str() == "-3.5"
expect F32.negate(-3.5).to_str() == "3.5"
Return the absolute value of an F32. The result of abs(NaN) is
NaN.
expect F32.abs(3.5).to_str() == "3.5"
expect F32.abs(-3.5).to_str() == "3.5"
Add two F32 values. Addition is subject to IEEE 754 rounding; the
result may be inf, -inf, or NaN.
expect F32.plus(1.5, 2.5).to_str() == "4"
Subtract the second F32 from the first. Subtraction is subject to
IEEE 754 rounding; the result may be inf, -inf, or NaN.
expect F32.minus(5.0, 3.5).to_str() == "1.5"
Multiply two F32 values. Multiplication is subject to IEEE 754
rounding; the result may be inf, -inf, or NaN.
expect F32.times(2.5, 4.0).to_str() == "10"
Divide the first F32 by the second. Unlike integer division,
division by zero does not crash: it produces inf, -inf, or
NaN as specified by IEEE 754.
expect F32.div_by(10.0, 4.0).to_str() == "2.5"
Return the remainder of dividing the first F32 by the second. The sign of the result matches the sign of the dividend.
expect F32.rem_by(7.5, 2.0).to_str() == "1.5"
expect F32.rem_by(-7.5, 2.0).to_str() == "-1.5"
Return the absolute difference between two F32 values.
expect F32.abs_diff(2.5, 5.0).to_str() == "2.5"
expect F32.abs_diff(-1.5, 5.0).to_str() == "6.5"
Build an F32 from a list of base-10 digits, most significant
first. Each element of the list must be a digit in the range 0
to 9. Returns Err(OutOfRange) if the resulting value does not
fit in an F32, or if any element is not a valid digit. The
result is always non-negative; to build a negative value, negate
the result.
expect match F32.from_int_digits([1, 2, 3]) {
Ok(x) => F32.to_str(x) == "123"
Err(_) => False
}
Build an F32 from a tuple of (integer digits, fractional digits),
each as a list of base-10 digits most significant first. Each
element of both lists must be a digit in the range 0 to 9.
Returns Err(OutOfRange) if the resulting value does not fit in an
F32, or if any element is not a valid digit. The result is
always non-negative; to build a negative value, negate the
result.
expect match F32.from_dec_digits(([1, 2], [5])) {
Ok(x) => F32.to_str(x) == "12.5"
Err(_) => False
}
Parse an F32 from a Str. Returns Err(BadNumStr) if the
string is not a valid decimal number, or if the parsed value does
not fit in an F32.
expect match F32.from_str("42.5") {
Ok(x) => F32.to_str(x) == "42.5"
Err(_) => False
}
expect match F32.from_str("-1.25") {
Ok(x) => F32.to_str(x) == "-1.25"
Err(_) => False
}
expect Try.is_err(F32.from_str("not a number"))
Convert an F32 to an I8. The fractional part is truncated
toward zero; the integer part wraps on overflow. Integer-part
values from -128 to 127 are preserved; other values wrap by
truncating to the low 8 bits and reinterpreting them in two's
complement. The result for NaN, inf, or -inf is
implementation-defined.
expect F32.to_i8_wrap(42.7) == 42
Convert an F32 to an I8, returning Err(OutOfRange) if the
integer part does not fit, or if the value is NaN, inf, or
-inf. The fractional part is truncated toward zero.
Integer-part values from -128 to 127 succeed; other values
return Err(OutOfRange).
expect F32.to_i8_try(42.7) == Ok(42)
expect F32.to_i8_try(200.0) == Err(OutOfRange)
Convert an F32 to an I16. The fractional part is truncated
toward zero; the integer part wraps on overflow. Integer-part
values from -32768 to 32767 are preserved; other values wrap
by truncating to the low 16 bits and reinterpreting them in two's
complement. The result for NaN, inf, or -inf is
implementation-defined.
expect F32.to_i16_wrap(42.5) == 42
Convert an F32 to an I16, returning Err(OutOfRange) if the
integer part does not fit, or if the value is NaN, inf, or
-inf. The fractional part is truncated toward zero.
Integer-part values from -32768 to 32767 succeed; other values
return Err(OutOfRange).
expect F32.to_i16_try(42.5) == Ok(42)
expect F32.to_i16_try(40000.0) == Err(OutOfRange)
Encode an F32 using a format that provides encode_f32
decode : src, fmt -> (Try(F32, err), src) where { fmt.decode_f32 : fmt, src -> (Try(F32, err), src) }
Decode an F32 using a format that provides decode_f32
F64
default : -> F64
Convert an F64 to its decimal string representation.
expect F64.to_str(42.5) == "42.5"
expect F64.to_str(-42.5) == "-42.5"
Returns Bool.True if the value is 0.0. Both positive and
negative zero return Bool.True.
expect F64.is_zero(0.0)
expect !F64.is_zero(0.5)
Returns Bool.True if the value is less than 0.0.
expect F64.is_negative(-0.5)
expect !F64.is_negative(0.0)
Returns Bool.True if the value is greater than 0.0.
expect F64.is_positive(0.5)
expect !F64.is_positive(0.0)
Returns Bool.True if the first value is greater than the second.
Comparisons involving NaN always return Bool.False.
expect F64.is_gt(5.0, 3.0)
expect !F64.is_gt(3.0, 3.0)
Returns Bool.True if the first value is greater than or equal to
the second. Comparisons involving NaN always return Bool.False.
expect F64.is_gte(3.0, 3.0)
expect !F64.is_gte(2.0, 3.0)
Returns Bool.True if the first value is less than the second.
Comparisons involving NaN always return Bool.False.
expect F64.is_lt(3.0, 5.0)
expect !F64.is_lt(3.0, 3.0)
Returns Bool.True if the first value is less than or equal to the
second. Comparisons involving NaN always return Bool.False.
expect F64.is_lte(3.0, 3.0)
expect !F64.is_lte(5.0, 3.0)
Returns the greater of two F64 values.
expect F64.max(5, 3) == 5
expect F64.max(-3, -1) == -1
Returns the smaller of two F64 values.
expect F64.min(5, 3) == 3
expect F64.min(-3, -1) == -3
Negate an F64. Flips the sign bit, so negating 0.0 produces
-0.0 and negating NaN produces NaN.
expect F64.negate(3.5).to_str() == "-3.5"
expect F64.negate(-3.5).to_str() == "3.5"
Return the absolute value of an F64. The result of abs(NaN) is
NaN.
expect F64.abs(3.5).to_str() == "3.5"
expect F64.abs(-3.5).to_str() == "3.5"
Add two F64 values. Addition is subject to IEEE 754 rounding; the
result may be inf, -inf, or NaN.
expect F64.plus(1.5, 2.5).to_str() == "4"
Subtract the second F64 from the first. Subtraction is subject to
IEEE 754 rounding; the result may be inf, -inf, or NaN.
expect F64.minus(5.0, 3.5).to_str() == "1.5"
Multiply two F64 values. Multiplication is subject to IEEE 754
rounding; the result may be inf, -inf, or NaN.
expect F64.times(2.5, 4.0).to_str() == "10"
Divide the first F64 by the second. Unlike integer division,
division by zero does not crash: it produces inf, -inf, or
NaN as specified by IEEE 754.
expect F64.div_by(10.0, 4.0).to_str() == "2.5"
Return the remainder of dividing the first F64 by the second. The sign of the result matches the sign of the dividend.
expect F64.rem_by(7.5, 2.0).to_str() == "1.5"
expect F64.rem_by(-7.5, 2.0).to_str() == "-1.5"
Return the absolute difference between two F64 values.
expect F64.abs_diff(2.5, 5.0).to_str() == "2.5"
expect F64.abs_diff(-1.5, 5.0).to_str() == "6.5"
Build an F64 from a list of base-10 digits, most significant
first. Each element of the list must be a digit in the range 0
to 9. Returns Err(OutOfRange) if the resulting value does not
fit in an F64, or if any element is not a valid digit. The
result is always non-negative; to build a negative value, negate
the result.
expect match F64.from_int_digits([1, 2, 3]) {
Ok(x) => F64.to_str(x) == "123"
Err(_) => False
}
Build an F64 from a tuple of (integer digits, fractional digits),
each as a list of base-10 digits most significant first. Each
element of both lists must be a digit in the range 0 to 9.
Returns Err(OutOfRange) if the resulting value does not fit in an
F64, or if any element is not a valid digit. The result is
always non-negative; to build a negative value, negate the
result.
expect match F64.from_dec_digits(([1, 2], [5])) {
Ok(x) => F64.to_str(x) == "12.5"
Err(_) => False
}
Parse an F64 from a Str. Returns Err(BadNumStr) if the
string is not a valid decimal number, or if the parsed value does
not fit in an F64.
expect match F64.from_str("42.5") {
Ok(x) => F64.to_str(x) == "42.5"
Err(_) => False
}
expect match F64.from_str("-1.25") {
Ok(x) => F64.to_str(x) == "-1.25"
Err(_) => False
}
expect Try.is_err(F64.from_str("not a number"))
Convert an F64 to an I8. The fractional part is truncated
toward zero; the integer part wraps on overflow. Integer-part
values from -128 to 127 are preserved; other values wrap by
truncating to the low 8 bits and reinterpreting them in two's
complement. The result for NaN, inf, or -inf is
implementation-defined.
expect F64.to_i8_wrap(42.7) == 42
Convert an F64 to an I8, returning Err(OutOfRange) if the
integer part does not fit, or if the value is NaN, inf, or
-inf. The fractional part is truncated toward zero.
Integer-part values from -128 to 127 succeed; other values
return Err(OutOfRange).
expect F64.to_i8_try(42.7) == Ok(42)
expect F64.to_i8_try(200.0) == Err(OutOfRange)
Convert an F64 to an I16. The fractional part is truncated
toward zero; the integer part wraps on overflow. Integer-part
values from -32768 to 32767 are preserved; other values wrap
by truncating to the low 16 bits and reinterpreting them in two's
complement. The result for NaN, inf, or -inf is
implementation-defined.
expect F64.to_i16_wrap(42.5) == 42
Convert an F64 to an I16, returning Err(OutOfRange) if the
integer part does not fit, or if the value is NaN, inf, or
-inf. The fractional part is truncated toward zero.
Integer-part values from -32768 to 32767 succeed; other values
return Err(OutOfRange).
expect F64.to_i16_try(42.5) == Ok(42)
expect F64.to_i16_try(40000.0) == Err(OutOfRange)
Convert an F64 to an F32, returning Err(OutOfRange) if the
value's magnitude exceeds the F32 range (which would otherwise
overflow to inf or -inf), or if the value is NaN. Values
that fit in an F32 succeed, though precision may still be lost
due to F32's smaller mantissa.
expect match F64.to_f32_try(1.5) {
Ok(x) => F32.to_str(x) == "1.5"
Err(_) => False
}
Encode an F64 using a format that provides encode_f64
decode : src, fmt -> (Try(F64, err), src) where { fmt.decode_f64 : fmt, src -> (Try(F64, err), src) }
Decode an F64 using a format that provides decode_f64