Skip to content
Open Saka

int

Integers: the conversions of a finite float to the integer near it, integer text in any radix, exact powers, and the quotient rounded to the nearest integer. The conversions each take a float of either format whose Set holds only finite Values, f64::finite or f32::finite, so a contract, a match arm, or a clamp proves it, and give the Set from the rounding of the Set’s least Value to that of its greatest. A function runs once for each combination of the Values of its ! arguments.

Write these names after int::, as in int::floor.

floor

def floor[set T: floating](value! T)! int

The greatest integer at most a finite float: int::floor(-1.5) is -2.

ceil

def ceil[set T: floating](value! T)! int

The least integer at least a finite float: int::ceil(-1.5) is -1.

round

def round[set T: floating](value! T)! int

The integer nearest a finite float, choosing the even integer at a tie, as float::round does: both 1.5 and 2.5 round to 2.

trunc

def trunc[set T: floating](value! T)! int

A finite float without its fraction, toward zero: int::trunc(-1.5) is -1.

parse

def parse(text! str)? int

Reads an optional - and decimal digits, or gives an empty Bag for any other text: int::parse("-042") is -42, and int::parse("+1"), int::parse(" 1"), and int::parse("1.0") are empty. The text may be as long as a string, and reading it charges its digits’ work, so a text of many digits can fail with LimitExceeded.

parse_radix

def parse_radix(text! str, radix! int:{2..36})? int

Reads an optional - and digits of radix, in either case, or gives an empty Bag for any other text: int::parse_radix("ff", 16) and int::parse_radix("-FF", 16) are 255 and -255, and int::parse_radix("2", 2) is empty. It charges its work as int::parse does.

format_radix

def format_radix(value! int, radix! int:{2..36})! str

Spells value in radix with lowercase digits and a - before a negative one: int::format_radix(0x4682B4, 16) is "4682b4", and int::format_radix(-5, 2) is "-101". Spelling an integer of many digits charges its work and can fail with LimitExceeded.

pow

def pow(base! int, exponent! nat)! int

Raises base to exponent exactly; int::pow(0, 0) is 1. Fails with LimitExceeded before building a result that passes the limits, and only on a row that reaches it: take then int::pow(10, n) else 0 with take false is 0 for any n.

round_div

def round_div(n! int, d! -int:{1..*} | int:{1..*})! int

The integer nearest n / d, with ties to even, as int::round takes them: int::round_div(7, 2) is 4, int::round_div(5, 2) is 2, and int::round_div(-7, 2) is -4. Its divisor’s Set excludes 0, as div’s does.