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

Values

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

Compare two big integers, returning an order that denotes if the first argument is lower, bigger than, or equal to the second.

pub fn decoder() -> decode.Decoder(BigInt)

Returns a decoder that decodes a 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_base(
  input: String,
  from base: Int,
  using alphabet: fn(String) -> Result(Int, Nil),
) -> Result(BigInt, Nil)

Parse a big integer from an arbitrary base.

The passed alphabet function must return Ok(n) for a given character, where n is the base-10 numerical value of that character. This allows using any kind of alphabet. The alphabet must contain enough characters to cover the entire value range of the chosen base.

The base must be positive and larger than 1.

pub fn from_base16(base16: String) -> Result(BigInt, Nil)

Parse a hexadecimal string into a big integer.

The string may contain an optional dash at the start to denote a negative number, followed by an optional 0x prefix. Following those, only numbers 0 through 9 and letters a through f are allowed. The string is NOT trimmed for whitespace. The string may be upper, lower, or mixed case, but the prefix 0x must always be lower case.

pub fn from_base2(base2: String) -> Result(BigInt, Nil)

Parse a binary string into a big integer.

The string may contain an optional dash at the start to denote a negative number, followed by an optional 0b prefix. Following those, only 0 and 1 are allowed. The string is NOT trimmed for whitespace.

Note that no conversion is done for the number. This means that it is always treated as unsigned, and will only be negative if it is preceded by a dash. As an example, "0b100" returns 4, and "-0b100" returns -4.

pub fn from_base8(base8: String) -> Result(BigInt, Nil)

Parse an octal string into a big integer.

The string may contain an optional dash at the start to denote a negative number, followed by an optional 0o prefix. Following those, only numbers 0 through 7 are allowed. The string is NOT trimmed for whitespace.

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(
  multiplicand a: BigInt,
  multiplier b: BigInt,
) -> BigInt

Multiply two big integers together.

pub fn negate(bigint: BigInt) -> BigInt

Returns the negative of the value provided.

pub fn negative_one() -> BigInt

Create a big integer representing negative one.

pub fn one() -> BigInt

Create a big integer representing one.

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 ten() -> BigInt

Create a big integer representing ten.

pub fn to_base(
  input: BigInt,
  to base: Int,
  using alphabet: fn(Int) -> Result(String, Nil),
) -> Result(String, Nil)

Stringify a big integer into a number of arbitrary base.

The passed alphabet function must return Ok(c) for a given base-10 integer, where c is the symbol of that integer in the given base. This allows using any kind of alphabet. The alphabet must contain enough symbols to cover the entire value range of the chosen base.

The base must be positive and larger than 1.

pub fn to_base16(int: BigInt) -> String

Stringify a big integer into a hexadecimal string.

A dash is added at the front if the number is negative. The resulting string will be lower case.

pub fn to_base2(int: BigInt) -> String

Stringify a big integer into a binary string.

A dash is added at the front if the number is negative. The number that follows will always be an unsigned binary number.

pub fn to_base8(int: BigInt) -> String

Stringify a big integer into an octal string.

A dash is added at the front if the number is negative.

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