Module
pooka/int
src/pooka/int.pookaIntegers can carry very large numbers without borrowing a decimal coat.
Work with arbitrary-precision integers and explicit conversions. Fallible arithmetic returns MathError instead of silently inventing a result. Division truncates toward zero; parsing and formatting bases support values from two through thirty-six.
import pooka/int
assert int.divide(-5, 2) == Ok(-2)
API reference
class ParseError(text: String)
A int failure carrying the details needed by its caller. Keep this value in Result until a boundary can explain it.
text: String
String
fn(String) -> pooka/int.ParseErrorenum MathError
MathError.NegativeExponent
MathError.FloatOutOfRange
MathError.DivisionByZero
MathError.InvalidBase
parse(text: String) -> Result(Int, ParseError)
fn(String) -> Result(Int, pooka/int.ParseError)Parse a integer value, retaining failure as ParseError.
import pooka/int
assert int.parse("3") == Ok(3)
to_string(value: Int) -> String
fn(Int) -> StringRender a integer value using the library's textual representation.
import pooka/int
let text = int.to_string(3)
abs(value: Int) -> Int
fn(Int) -> IntReturn the nonnegative magnitude.
import pooka/int
assert int.abs(-3) == 3
min(a: Int, b: Int) -> Int
fn(Int, Int) -> IntReturn the smaller value.
import pooka/int
assert int.min(2, 3) == 2
max(a: Int, b: Int) -> Int
fn(Int, Int) -> IntReturn the larger value.
import pooka/int
assert int.max(2, 3) == 3
clamp(value: Int, lower: Int, upper: Int) -> Int
fn(Int, Int, Int) -> IntKeep a value between the bounds; reversed bounds are normalized first.
import pooka/int
assert int.clamp(3, -2, 2) == 2
pow(base: Int, exponent: Int) -> Result(Int, MathError)
fn(Int, Int) -> Result(Int, pooka/int.MathError)Raise an integer to a nonnegative exponent using repeated squaring. Negative exponents return MathError.
import pooka/int
assert int.pow(2, 10) == Ok(1024)
to_float(value: Int) -> Result(Float, MathError)
fn(Int) -> Result(Float, pooka/int.MathError)Convert explicitly, returning FloatOutOfRange when the integer cannot become a BEAM float.
import pooka/int
assert int.to_float(2) == Ok(2.0)
is_even(value: Int) -> Bool
fn(Int) -> BoolTest divisibility by two, including negative integers.
import pooka/int
assert int.is_even(-4)
is_odd(value: Int) -> Bool
fn(Int) -> BoolTest whether the integer is not even.
import pooka/int
assert int.is_odd(-3)
divide(dividend: Int, divisor: Int) -> Result(Int, MathError)
fn(Int, Int) -> Result(Int, pooka/int.MathError)Divide with truncation toward zero. A zero divisor returns DivisionByZero.
import pooka/int
assert int.divide(-5, 2) == Ok(-2)
remainder(dividend: Int, divisor: Int) -> Result(Int, MathError)
fn(Int, Int) -> Result(Int, pooka/int.MathError)Return the integer remainder, retaining the dividend's sign. A zero divisor returns DivisionByZero.
import pooka/int
assert int.remainder(-5, 2) == Ok(-1)
to_base(value: Int, base: Int) -> Result(String, MathError)
fn(Int, Int) -> Result(String, pooka/int.MathError)Render a lowercase integer in bases two through thirty-six.
import pooka/int
assert int.to_base(255, 16) == Ok("ff")
parse_base(text: String, base: Int) -> Result(Int, ParseError)
fn(String, Int) -> Result(Int, pooka/int.ParseError)Parse an integer in bases two through thirty-six. Invalid bases or text return ParseError.
import pooka/int
assert int.parse_base("ff", 16) == Ok(255)