Module

pooka/int

src/pooka/int.pooka

Integers 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.

Pooka
import pooka/int

assert int.divide(-5, 2) == Ok(-2)

API reference

class

ParseError

src/pooka/int.pooka:20
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.

field

text

src/pooka/int.pooka:20
text: String
INFERREDString
constructor

new

src/pooka/int.pooka:20
INFERREDfn(String) -> pooka/int.ParseError
enum

MathError

src/pooka/int.pooka:22
enum MathError
variant

NegativeExponent

src/pooka/int.pooka:23
MathError.NegativeExponent
variant

FloatOutOfRange

src/pooka/int.pooka:24
MathError.FloatOutOfRange
variant

DivisionByZero

src/pooka/int.pooka:25
MathError.DivisionByZero
variant

InvalidBase

src/pooka/int.pooka:26
MathError.InvalidBase
function

parse

src/pooka/int.pooka:50
parse(text: String) -> Result(Int, ParseError)
INFERREDfn(String) -> Result(Int, pooka/int.ParseError)

Parse a integer value, retaining failure as ParseError.

Pooka
import pooka/int

assert int.parse("3") == Ok(3)
function

to_string

src/pooka/int.pooka:64
to_string(value: Int) -> String
INFERREDfn(Int) -> String

Render a integer value using the library's textual representation.

Pooka
import pooka/int

let text = int.to_string(3)
function

abs

src/pooka/int.pooka:75
abs(value: Int) -> Int
INFERREDfn(Int) -> Int

Return the nonnegative magnitude.

Pooka
import pooka/int

assert int.abs(-3) == 3
function

min

src/pooka/int.pooka:89
min(a: Int, b: Int) -> Int
INFERREDfn(Int, Int) -> Int

Return the smaller value.

Pooka
import pooka/int

assert int.min(2, 3) == 2
function

max

src/pooka/int.pooka:103
max(a: Int, b: Int) -> Int
INFERREDfn(Int, Int) -> Int

Return the larger value.

Pooka
import pooka/int

assert int.max(2, 3) == 3
function

clamp

src/pooka/int.pooka:117
clamp(value: Int, lower: Int, upper: Int) -> Int
INFERREDfn(Int, Int, Int) -> Int

Keep a value between the bounds; reversed bounds are normalized first.

Pooka
import pooka/int

assert int.clamp(3, -2, 2) == 2
function

pow

src/pooka/int.pooka:129
pow(base: Int, exponent: Int) -> Result(Int, MathError)
INFERREDfn(Int, Int) -> Result(Int, pooka/int.MathError)

Raise an integer to a nonnegative exponent using repeated squaring. Negative exponents return MathError.

Pooka
import pooka/int

assert int.pow(2, 10) == Ok(1024)
function

to_float

src/pooka/int.pooka:156
to_float(value: Int) -> Result(Float, MathError)
INFERREDfn(Int) -> Result(Float, pooka/int.MathError)

Convert explicitly, returning FloatOutOfRange when the integer cannot become a BEAM float.

Pooka
import pooka/int

assert int.to_float(2) == Ok(2.0)
function

is_even

src/pooka/int.pooka:170
is_even(value: Int) -> Bool
INFERREDfn(Int) -> Bool

Test divisibility by two, including negative integers.

Pooka
import pooka/int

assert int.is_even(-4)
function

is_odd

src/pooka/int.pooka:181
is_odd(value: Int) -> Bool
INFERREDfn(Int) -> Bool

Test whether the integer is not even.

Pooka
import pooka/int

assert int.is_odd(-3)
function

divide

src/pooka/int.pooka:192
divide(dividend: Int, divisor: Int) -> Result(Int, MathError)
INFERREDfn(Int, Int) -> Result(Int, pooka/int.MathError)

Divide with truncation toward zero. A zero divisor returns DivisionByZero.

Pooka
import pooka/int

assert int.divide(-5, 2) == Ok(-2)
function

remainder

src/pooka/int.pooka:207
remainder(dividend: Int, divisor: Int) -> Result(Int, MathError)
INFERREDfn(Int, Int) -> Result(Int, pooka/int.MathError)

Return the integer remainder, retaining the dividend's sign. A zero divisor returns DivisionByZero.

Pooka
import pooka/int

assert int.remainder(-5, 2) == Ok(-1)
function

to_base

src/pooka/int.pooka:221
to_base(value: Int, base: Int) -> Result(String, MathError)
INFERREDfn(Int, Int) -> Result(String, pooka/int.MathError)

Render a lowercase integer in bases two through thirty-six.

Pooka
import pooka/int

assert int.to_base(255, 16) == Ok("ff")
function

parse_base

src/pooka/int.pooka:236
parse_base(text: String, base: Int) -> Result(Int, ParseError)
INFERREDfn(String, Int) -> Result(Int, pooka/int.ParseError)

Parse an integer in bases two through thirty-six. Invalid bases or text return ParseError.

Pooka
import pooka/int

assert int.parse_base("ff", 16) == Ok(255)