Standardbibliothek
Antra::Int
import Antra::IntInteger operations for Antra.
Provides safe parsing, base conversion, and integer helper functions.
Überblick#
Neben Parsen und Basen (Int.parse, Int.to_base, ...) gibt es ganzzahlige
Division und Bit-Helfer. / liefert in Antra einen Bruch (7 / 2 ist 3.5);
Int.div und Int.mod runden dagegen nach unten, wie Pythons // und %:
import Antra::Int
println([Int.div(7, 2), Int.div(-7, 2), Int.mod(-7, 3)]) # [3, -4, 2]
println(Int.divmod(17, 5)) # [3, 2]
println([Int.bit_count(11), Int.bit_length(255)]) # [3, 8]
println(Int.rotl(0x80000001, 1)) # 3 (32 Bit)
println(Int.rotl(1, 1, [bits: 8])) # 2
println(Int.to_hex(255, [width: 4])) # 00ffDie Bit-Operatoren selbst sind Sprachoperatoren auf vorzeichenbehafteten
64-Bit-Ganzzahlen: &, |, ^, <<, >> und das präfixe ~
(bitweises NOT, ~5 ist -6). Mit einem Int jenseits von 64 Bit melden sie
einen Fehler, ebenso Int.bit_count, Int.bit_length, die Rotationen und
Int.to_hex; Int.to_base, Int.gcd, Int.div und Int.mod rechnen
dagegen exakt.
Funktionen#
fn base_parse(str, base)Parses an integer string in the given base (2–36).
Returns %Ok[n] on success, otherwise %Err["..."].
Example:
Int.base_parse("ff", 16) # %Ok[255]
Int.base_parse("1010", 2) # %Ok[10]
Int.base_parse("zz", 36) # %Ok[1295]fn bit_count(n)Number of set bits of a non-negative integer.
fn bit_length(n)Number of bits needed to write a non-negative integer (0 for 0).
fn clamp(n, lo, hi)Clamps n to the interval [lo, hi].
Example:
Int.clamp(5, 0, 10) # 5
Int.clamp(-3, 0, 10) # 0
Int.clamp(15, 0, 10) # 10fn digits(n)Returns the decimal digits of the integer as a list.
Example:
Int.digits(123) # [1, 2, 3]
Int.digits(0) # [0]fn div(a, b)Floor division: rounds toward negative infinity (like Python's //).
Example:
Int.div(7, 2) # 3
Int.div(-7, 2) # -4fn divmod(a, b)[Int.div(a, b), Int.mod(a, b)].
fn gcd(a, b)Computes the greatest common divisor of a and b.
Example:
Int.gcd(12, 8) # 4
Int.gcd(7, 5) # 1fn is_even(n)Returns true if n is even.
Example:
Int.is_even(4) # true
Int.is_even(3) # falsefn is_odd(n)Returns true if n is odd.
Example:
Int.is_odd(3) # true
Int.is_odd(4) # falsefn lcm(a, b)Computes the least common multiple of a and b.
Example:
Int.lcm(4, 6) # 12
Int.lcm(3, 7) # 21fn mod(a, b)Remainder of floor division; takes the sign of b (like Python's %).
Example:
Int.mod(-7, 3) # 2fn parse(str)Safely parses a decimal integer string.
Returns %Ok[n] if the string is a valid integer,
otherwise %Err["..."].
Example:
Int.parse("42") # %Ok[42]
Int.parse("abc") # %Err["..."]fn rotl(n, k, [bits: = 32])Rotates the low bits bits of n left by k.
Example:
Int.rotl(0x80000001, 1) # 3
Int.rotl(1, 1, [bits: 8]) # 2fn rotr(n, k, [bits: = 32])Rotates the low bits bits of n right by k.
fn to_base(n, base)Returns the integer as a string in the given base (2–36).
Returns %Ok["..."] on success, otherwise %Err["..."].
Example:
Int.to_base(255, 16) # %Ok["ff"]
Int.to_base(10, 2) # %Ok["1010"]
Int.to_base(42, 8) # %Ok["52"]fn to_hex(n, [width: = 0])Lowercase hexadecimal text, zero-padded to width digits.
Example:
Int.to_hex(255, [width: 4]) # "00ff"