gossamer/big_int

Types

A JS bigint — an arbitrary-precision integer. Use when working with values outside the safe-integer range of Gleam Int (±2^53−1) or interoperating with BigInt64Array/BigUint64Array.

Mixed-type arithmetic (bigint + number) throws TypeError in JS, but Gleam’s type system makes that unreachable — every arithmetic function here takes two BigInt operands.

See BigInt on MDN.

pub type BigInt

Values

pub fn absolute_value(value: BigInt) -> BigInt
pub fn add(a: BigInt, b: BigInt) -> BigInt
pub fn compare(a: BigInt, b: BigInt) -> order.Order
pub fn divide(
  a: BigInt,
  by divisor: BigInt,
) -> Result(BigInt, js_error.JsError)

Truncating integer division. Returns an error if divisor is 0.

pub fn from_int(value: Int) -> BigInt
pub fn from_string(
  string: String,
) -> Result(BigInt, js_error.JsError)

Parses an integer string. Accepts decimal ("42"), hex ("0x2a"), octal ("0o52"), and binary ("0b101010") literals with an optional sign and surrounding whitespace. Returns an error on malformed input — decimal floats like "1.5", scientific notation like "1e3", and the trailing-n literal suffix like "42n" are all rejected. The empty string returns 0, matching JS BigInt("").

pub fn multiply(a: BigInt, b: BigInt) -> BigInt
pub fn negate(value: BigInt) -> BigInt
pub fn remainder(
  a: BigInt,
  by divisor: BigInt,
) -> Result(BigInt, js_error.JsError)

Returns the remainder of a / divisor. Returns an error if divisor is 0.

pub fn subtract(a: BigInt, b: BigInt) -> BigInt
pub fn to_int(value: BigInt) -> Result(Int, Nil)

Converts a BigInt to a Gleam Int. Returns an error if the value is outside the safe-integer range (±2^53−1) and would lose precision when narrowed.

pub fn to_string(value: BigInt) -> String
Search Document