bigi

Types

A big integer.

pub type BigInt

Endianness specifier used when converting between a big integer and raw bytes.

pub type Endianness {
  LittleEndian
  BigEndian
}

Constructors

  • LittleEndian
  • BigEndian

Signedness specifier used when converting between a big integer and raw bytes.

pub type Signedness {
  Signed
  Unsigned
}

Constructors

  • Signed
  • Unsigned

Functions

pub fn absolute(bigint: BigInt) -> BigInt

Get the absolute value of a big integer.

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

Add two big integers together.

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

Calculates the bitwise AND of its arguments.

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

Calculates the bitwise XOR of its arguments.

pub fn bitwise_not(bigint: BigInt) -> BigInt

Calculates the bitwise NOT of its argument.

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

Calculates the bitwise OR of its arguments.

pub fn bitwise_shift_left(
  bigint: BigInt,
  by amount: Int,
) -> BigInt

Calculates the result of an arithmetic left bitshift by the given amount.

pub fn bitwise_shift_right(
  bigint: BigInt,
  by amount: Int,
) -> BigInt

Calculates the result of an arithmetic right bitshift by the given amount.

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

Restricts a big integer between a lower and upper bound.

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

Compare two big integers, returning an order that denotes if a is lower, bigger than, or equal to b.

pub fn decode(dyn: Dynamic) -> Result(BigInt, List(DecodeError))

Decode a gleam/dynamic value into a big integer, if possible.

pub fn digits(bigint: BigInt) -> List(Int)

Get the digits in a given bigint as a list of integers in base 10.

The list is ordered starting from the most significant digit.

pub fn divide(dividend a: BigInt, divisor b: BigInt) -> BigInt

Divide the dividend with the divisor using integer division.

Follows the standard Gleam divide-by-zero rule of 0 when the divisor is 0.

pub fn divide_no_zero(
  dividend a: BigInt,
  divisor b: BigInt,
) -> Result(BigInt, Nil)

Divide the dividend with the divisor using integer division.

Returns an error if the divisor is 0.

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

Performs a floored integer division, which means that the result will always be rounded towards negative infinity.

If you want to perform truncated integer division (rounding towards zero), use divide or divide_no_zero instead.

Returns an error if the divisor is 0.

pub fn from_bytes(
  bytes: BitArray,
  endianness: Endianness,
  signedness: Signedness,
) -> Result(BigInt, Nil)

Convert raw bytes into a big integer.

If the bit array does not contain a whole number of bytes then an error is returned.

pub fn from_int(int: Int) -> BigInt

Create a big integer from a regular integer.

Note that in the JavaScript target, if your integer is bigger than the maximum safe integer or smaller than the minimum safe integer, you may lose precision when operating on it, including when converting it into a big integer (as the JavaScript Number type has already reduced the precision of the value).

pub fn from_string(str: String) -> Result(BigInt, Nil)

Convert a string into a big integer.

If the string does not represent a big integer in base 10, an error is returned. Trailing non-digit content is not allowed.

pub fn is_odd(bigint: BigInt) -> Bool

Returns whether the big integer provided is odd.

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

Compares two big integers, returning the larger of the two.

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

Compares two big integers, returning the smaller of the two.

pub fn modulo(dividend a: BigInt, divisor b: BigInt) -> BigInt

Calculate a mathematical modulo operation.

Follows the standard Gleam divide-by-zero rule of 0 when the divisor is 0.

pub fn modulo_no_zero(
  dividend a: BigInt,
  divisor b: BigInt,
) -> Result(BigInt, Nil)

Calculate a mathematical modulo operation.

Returns an error if the divisor is 0.

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

Multiply two big integers together.

pub fn negate(bigint: BigInt) -> BigInt

Returns the negative of the value provided.

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

Raise the base to the exponent.

If the exponent is negative, an error is returned.

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

Multiplies a list of big integers.

Returns 1 if the list was empty.

pub fn remainder(dividend a: BigInt, divisor b: BigInt) -> BigInt

Divide the dividend with the divisor using integer division and return the remainder.

Follows the standard Gleam divide-by-zero rule of 0 when the divisor is 0.

pub fn remainder_no_zero(
  dividend a: BigInt,
  divisor b: BigInt,
) -> Result(BigInt, Nil)

Divide the dividend with the divisor using integer division and return the remainder.

Returns an error if the divisor is 0.

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

Subtract the subtrahend from the minuend.

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

Sums a list of big integers.

Returns 0 if the list was empty.

pub fn to_bytes(
  bigint: BigInt,
  endianness: Endianness,
  signedness: Signedness,
  byte_count: Int,
) -> Result(BitArray, Nil)

Convert a big integer to raw bytes.

The size of the returned bit array is specified by byte_count, e.g. 8 will return a bit array containing 8 bytes (64 bits). If the big integer doesn’t fit in the specified number of bytes then an error is returned.

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

Convert a big integer to a regular integer.

In Erlang, this cannot fail, as all Erlang integers are big integers. In the JavaScript target, this will fail if the integer is bigger than the maximum safe integer or smaller than the minimum safe integer.

pub fn to_string(bigint: BigInt) -> String

Convert the big integer into a simple string - a sequence of digits.

pub fn undigits(
  digits: List(Int),
  base: Int,
) -> Result(BigInt, Nil)

Joins a list of digits into a single value. Returns an error if the base is less than 2 or if the list contains a digit greater than or equal to the specified base.

pub fn zero() -> BigInt

Create a big integer representing zero.

Search Document