Module std::u8
use std::ascii;
use std::option;
use std::string;
use std::vector;
Function bitwise_not
Returns the bitwise not of the value.
Each bit that is 1 becomes 0. Each bit that is 0 becomes 1.
public fun bitwise_not(x: u8): u8
Function max
Return the larger of x and y
public fun max(x: u8, y: u8): u8
Function min
Return the smaller of x and y
public fun min(x: u8, y: u8): u8
Function diff
Return the absolute value of x - y
public fun diff(x: u8, y: u8): u8
Function mul_div
Calculate x * y / z, upcasting intermediate values to avoid overflow when possible.
Aborts if z is 0.
Aborts if the result is larger than MAX.
public fun mul_div(x: u8, y: u8, z: u8): u8
Function mul_div_ceil
Calculate x * y / z, upcasting intermediate values to avoid overflow when possible.
Rounds up the result if there is a remainder.
Aborts if z is 0.
Aborts if the result is larger than MAX.
public fun mul_div_ceil(x: u8, y: u8, z: u8): u8
Function div_ceil
Calculate x / y, but round up the result.
public fun div_ceil(x: u8, y: u8): u8
Function pow
Return the value of a base raised to a power
public fun pow(base: u8, exponent: u8): u8
Function sqrt
Get a nearest lower integer Square Root for x. Given that this function can only operate with integers, it is impossible to get perfect (or precise) integer square root for some numbers.
Example:
math::sqrt(9) => 3
math::sqrt(8) => 2 // the nearest lower square root is 4;
In integer math, one of the possible ways to get results with more precision is to use higher values or temporarily multiply the value by some bigger number. Ideally if this is a square of 10 or 100.
Example:
math::sqrt(8) => 2;
math::sqrt(8 * 10000) => 282;
// now we can use this value as if it was 2.82;
// but to get the actual result, this value needs
// to be divided by 100 (because sqrt(10000)).
math::sqrt(8 * 1000000) => 2828; // same as above, 2828 / 1000 (2.828)
public fun sqrt(x: u8): u8
Function to_string
public fun to_string(x: u8): std::string::String
Function checked_add
Try to add x and y.
Returns None if the addition would overflow.
public fun checked_add(x: u8, y: u8): std::option::Option<u8>
Function checked_sub
Try to subtract y from x.
Returns None if y > x.
public fun checked_sub(x: u8, y: u8): std::option::Option<u8>
Function checked_mul
Try to multiply x and y.
Returns None if the multiplication would overflow.
public fun checked_mul(x: u8, y: u8): std::option::Option<u8>
Function checked_div
Try to divide x by y.
Returns None if y is zero.
public fun checked_div(x: u8, y: u8): std::option::Option<u8>
Function saturating_add
Add x and y, saturating at the maximum value instead of overflowing.
public fun saturating_add(x: u8, y: u8): u8
Function saturating_sub
Subtract y from x, saturating at 0 instead of underflowing.
public fun saturating_sub(x: u8, y: u8): u8
Function saturating_mul
Multiply x and y, saturating at the maximum value instead of overflowing.
public fun saturating_mul(x: u8, y: u8): u8
Function checked_shl
Shifts x left by shift bits.
Returns None if the shift is greater than or equal to the bit size of 8.
public fun checked_shl(x: u8, shift: u8): std::option::Option<u8>