gossamer/big_int

Arbitrary-precision integer arithmetic for values outside Gleam Int’s safe-integer range. Construct via from_int for safe-range starting values or parse when reading larger literals from text. Mirrors gleam/int where the operation carries over.

Types

A JavaScript BigInt — an arbitrary-precision integer. Use when working with values outside the safe-integer range of Gleam Int (±2^53−1).

See BigInt on MDN.

pub type BigInt

Values

pub fn absolute_value(x: BigInt) -> BigInt

Returns the absolute value of x.

pub fn add(a: BigInt, b: BigInt) -> BigInt

Returns the sum of a and b.

pub fn as_int_n(x: BigInt, bits bits: Int) -> BigInt

Wraps x to a bits-wide two’s-complement signed integer — x modulo 2^bits, interpreted as signed. A bits of 0 or less yields 0. Equivalent to JavaScript’s BigInt.asIntN.

pub fn as_uint_n(x: BigInt, bits bits: Int) -> BigInt

Wraps x to a bits-wide unsigned integer — x modulo 2^bits. A bits of 0 or less yields 0. Equivalent to JavaScript’s BigInt.asUintN.

pub fn base_parse(
  string: String,
  base: Int,
) -> Result(BigInt, Nil)

Parses an integer string in the given base (236), with an optional sign. Digits above 9 use letters, case-insensitively. Returns Error(Nil) if base is outside that range or the string isn’t a valid integer in that base.

pub fn bitwise_and(x: BigInt, y: BigInt) -> BigInt

Returns the bitwise AND of x and y. Operates on the two’s complement representation.

pub fn bitwise_exclusive_or(x: BigInt, y: BigInt) -> BigInt

Returns the bitwise XOR of x and y. Operates on the two’s complement representation.

pub fn bitwise_not(x: BigInt) -> BigInt

Returns the bitwise NOT of x. Equivalent to -x - 1.

pub fn bitwise_or(x: BigInt, y: BigInt) -> BigInt

Returns the bitwise OR of x and y. Operates on the two’s complement representation.

pub fn bitwise_shift_left(
  x: BigInt,
  y: BigInt,
) -> Result(BigInt, Nil)

Shifts x left by y bits. Returns Error(Nil) if y is negative, or if the result would exceed the runtime’s BigInt size limit.

pub fn bitwise_shift_right(
  x: BigInt,
  y: BigInt,
) -> Result(BigInt, Nil)

Shifts x right by y bits (arithmetic shift — sign-extends). Returns Error(Nil) if y is negative.

pub fn clamp(
  x: BigInt,
  min min_bound: BigInt,
  max max_bound: BigInt,
) -> BigInt

Restricts x to be within the range min_boundmax_bound (inclusive).

pub fn compare(a: BigInt, with b: BigInt) -> order.Order

Compares a and b, returning their relative ordering as a gleam/order.Order.

pub fn divide(
  dividend: BigInt,
  by divisor: BigInt,
) -> Result(BigInt, Nil)

Truncating integer division. Returns Error(Nil) if divisor is 0.

pub fn floor_divide(
  dividend: BigInt,
  by divisor: BigInt,
) -> Result(BigInt, Nil)

Floored integer division — the quotient is rounded toward negative infinity. Returns Error(Nil) if divisor is 0.

pub fn from_int(value: Int) -> BigInt

Creates a BigInt from a Gleam Int. Pair with parse when the source is a string literal outside the safe-integer range.

pub fn is_even(x: BigInt) -> Bool

Whether x is even.

pub fn is_odd(x: BigInt) -> Bool

Whether x is odd.

pub fn max(a: BigInt, b: BigInt) -> BigInt

Returns the larger of a and b.

pub fn min(a: BigInt, b: BigInt) -> BigInt

Returns the smaller of a and b.

pub fn modulo(
  dividend: BigInt,
  by divisor: BigInt,
) -> Result(BigInt, Nil)

Floored modulo of dividend by divisor — the result takes the sign of divisor. Returns Error(Nil) if divisor is 0.

pub fn multiply(a: BigInt, b: BigInt) -> BigInt

Returns the product of a and b.

pub fn negate(x: BigInt) -> BigInt

Returns the additive inverse of x (i.e., -x).

pub fn parse(string: String) -> Result(BigInt, Nil)

Parses an integer string. Accepts decimal ("42"), hex ("0x2a"), octal ("0o52"), and binary ("0b101010") literals with an optional sign and surrounding whitespace. Returns Error(Nil) 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 parses as 0.

pub fn power(
  base: BigInt,
  of exponent: BigInt,
) -> Result(BigInt, Nil)

Raises base to the power of exponent. Returns Error(Nil) if exponent is negative (the result would be fractional), or if the result would exceed the runtime’s BigInt size limit.

pub fn product(numbers: List(BigInt)) -> BigInt

Multiplies a list of BigInts. The empty list has a product of 1.

pub fn remainder(
  dividend: BigInt,
  by divisor: BigInt,
) -> Result(BigInt, Nil)

Returns the remainder of dividend / divisor, truncating toward zero — the result takes the sign of dividend. Returns Error(Nil) if divisor is 0.

pub fn subtract(a: BigInt, b: BigInt) -> BigInt

Returns the difference of a and b.

pub fn sum(numbers: List(BigInt)) -> BigInt

Sums a list of BigInts. The empty list sums to 0.

pub fn to_base16(x: BigInt) -> String

Returns the base-16 (hexadecimal) string representation of x, with uppercase digits.

pub fn to_base2(x: BigInt) -> String

Returns the base-2 (binary) string representation of x.

pub fn to_base36(x: BigInt) -> String

Returns the base-36 string representation of x, with uppercase digits.

pub fn to_base8(x: BigInt) -> String

Returns the base-8 (octal) string representation of x.

pub fn to_base_string(
  x: BigInt,
  base: Int,
) -> Result(String, Nil)

Returns the string representation of x in the given base (236), with digits above 9 as uppercase letters. Returns Error(Nil) if base is outside that range.

pub fn to_float(x: BigInt) -> Result(Float, Nil)

Converts a BigInt to a Float. Values beyond 2^53 lose precision; values too large for a Float return Error(Nil) rather than a non-finite result.

pub fn to_int(x: BigInt) -> Result(Int, Nil)

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

pub fn to_string(x: BigInt) -> String

Returns the decimal string representation of x.

Search Document