-module(bigi). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -define(FILEPATH, "src/bigi.gleam"). -export([zero/0, from_int/1, from_string/1, from_bytes/3, to_int/1, to_string/1, to_bytes/4, compare/2, absolute/1, negate/1, add/2, subtract/2, multiply/2, divide/2, divide_no_zero/2, remainder/2, floor_divide/2, remainder_no_zero/2, modulo/2, modulo_no_zero/2, power/2, do_decode/1, decoder/0, bitwise_and/2, bitwise_exclusive_or/2, bitwise_not/1, bitwise_or/2, bitwise_shift_left/2, bitwise_shift_right/2, is_odd/1, max/2, min/2, clamp/3, sum/1, product/1, undigits/2, digits/1]). -export_type([big_int/0, endianness/0, signedness/0]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. -type big_int() :: any(). -type endianness() :: little_endian | big_endian. -type signedness() :: signed | unsigned. -file("src/bigi.gleam", 25). ?DOC(" Create a big integer representing zero.\n"). -spec zero() -> big_int(). zero() -> bigi_ffi:zero(). -file("src/bigi.gleam", 38). ?DOC( " Create a big integer from a regular integer.\n" "\n" " Note that in the JavaScript target, if your integer is bigger than the\n" " [maximum safe integer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER)\n" " or smaller than the\n" " [minimum safe integer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MIN_SAFE_INTEGER),\n" " you may lose precision when operating on it, including when converting it\n" " into a big integer (as the JavaScript Number type has already reduced the\n" " precision of the value).\n" ). -spec from_int(integer()) -> big_int(). from_int(Int) -> bigi_ffi:from(Int). -file("src/bigi.gleam", 46). ?DOC( " Convert a string into a big integer.\n" "\n" " If the string does not represent a big integer in base 10, an error is\n" " returned. Trailing non-digit content is not allowed.\n" ). -spec from_string(binary()) -> {ok, big_int()} | {error, nil}. from_string(Str) -> bigi_ffi:from_string(Str). -file("src/bigi.gleam", 54). ?DOC( " Convert raw bytes into a big integer.\n" " \n" " If the bit array does not contain a whole number of bytes then an error is\n" " returned.\n" ). -spec from_bytes(bitstring(), endianness(), signedness()) -> {ok, big_int()} | {error, nil}. from_bytes(Bytes, Endianness, Signedness) -> bigi_ffi:from_bytes(Bytes, Endianness, Signedness). -file("src/bigi.gleam", 69). ?DOC( " Convert a big integer to a regular integer.\n" "\n" " In Erlang, this cannot fail, as all Erlang integers are big integers. In the\n" " JavaScript target, this will fail if the integer is bigger than the\n" " [maximum safe integer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER)\n" " or smaller than the\n" " [minimum safe integer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MIN_SAFE_INTEGER).\n" ). -spec to_int(big_int()) -> {ok, integer()} | {error, nil}. to_int(Bigint) -> bigi_ffi:to(Bigint). -file("src/bigi.gleam", 74). ?DOC(" Convert the big integer into a simple string - a sequence of digits.\n"). -spec to_string(big_int()) -> binary(). to_string(Bigint) -> erlang:integer_to_binary(Bigint). -file("src/bigi.gleam", 83). ?DOC( " Convert a big integer to raw bytes.\n" " \n" " The size of the returned bit array is specified by `byte_count`, e.g. 8 will\n" " return a bit array containing 8 bytes (64 bits). If the big integer doesn't\n" " fit in the specified number of bytes then an error is returned.\n" ). -spec to_bytes(big_int(), endianness(), signedness(), integer()) -> {ok, bitstring()} | {error, nil}. to_bytes(Bigint, Endianness, Signedness, Byte_count) -> bigi_ffi:to_bytes(Bigint, Endianness, Signedness, Byte_count). -file("src/bigi.gleam", 94). ?DOC( " Compare two big integers, returning an order that denotes if `a` is lower,\n" " bigger than, or equal to `b`.\n" ). -spec compare(big_int(), big_int()) -> gleam@order:order(). compare(A, B) -> bigi_ffi:compare(A, B). -file("src/bigi.gleam", 99). ?DOC(" Get the absolute value of a big integer.\n"). -spec absolute(big_int()) -> big_int(). absolute(Bigint) -> erlang:abs(Bigint). -file("src/bigi.gleam", 104). ?DOC(" Returns the negative of the value provided.\n"). -spec negate(big_int()) -> big_int(). negate(Bigint) -> bigi_ffi:negate(Bigint). -file("src/bigi.gleam", 109). ?DOC(" Add two big integers together.\n"). -spec add(big_int(), big_int()) -> big_int(). add(A, B) -> bigi_ffi:add(A, B). -file("src/bigi.gleam", 114). ?DOC(" Subtract the subtrahend from the minuend.\n"). -spec subtract(big_int(), big_int()) -> big_int(). subtract(A, B) -> bigi_ffi:subtract(A, B). -file("src/bigi.gleam", 119). ?DOC(" Multiply two big integers together.\n"). -spec multiply(big_int(), big_int()) -> big_int(). multiply(A, B) -> bigi_ffi:multiply(A, B). -file("src/bigi.gleam", 126). ?DOC( " Divide the dividend with the divisor using integer division.\n" "\n" " Follows the standard Gleam divide-by-zero rule of 0 when the divisor is 0.\n" ). -spec divide(big_int(), big_int()) -> big_int(). divide(A, B) -> bigi_ffi:divide(A, B). -file("src/bigi.gleam", 159). ?DOC( " Divide the dividend with the divisor using integer division.\n" "\n" " Returns an error if the divisor is 0.\n" ). -spec divide_no_zero(big_int(), big_int()) -> {ok, big_int()} | {error, nil}. divide_no_zero(A, B) -> bigi_ffi:divide_no_zero(A, B). -file("src/bigi.gleam", 170). ?DOC( " Divide the dividend with the divisor using integer division and return the\n" " remainder.\n" "\n" " Follows the standard Gleam divide-by-zero rule of 0 when the divisor is 0.\n" ). -spec remainder(big_int(), big_int()) -> big_int(). remainder(A, B) -> bigi_ffi:remainder(A, B). -file("src/bigi.gleam", 135). ?DOC( " Performs a *floored* integer division, which means that the result will\n" " always be rounded towards negative infinity.\n" "\n" " If you want to perform truncated integer division (rounding towards zero),\n" " use `divide` or `divide_no_zero` instead.\n" "\n" " Returns an error if the divisor is 0.\n" ). -spec floor_divide(big_int(), big_int()) -> {ok, big_int()} | {error, nil}. floor_divide(Dividend, Divisor) -> Z = bigi_ffi:zero(), case Divisor =:= Z of true -> {error, nil}; false -> case bigi_ffi:compare(bigi_ffi:multiply(Dividend, Divisor), Z) of lt -> case bigi_ffi:remainder(Dividend, Divisor) /= Z of true -> {ok, bigi_ffi:subtract( bigi_ffi:divide(Dividend, Divisor), bigi_ffi:from(1) )}; false -> {ok, bigi_ffi:divide(Dividend, Divisor)} end; _ -> {ok, bigi_ffi:divide(Dividend, Divisor)} end end. -file("src/bigi.gleam", 178). ?DOC( " Divide the dividend with the divisor using integer division and return the\n" " remainder.\n" "\n" " Returns an error if the divisor is 0.\n" ). -spec remainder_no_zero(big_int(), big_int()) -> {ok, big_int()} | {error, nil}. remainder_no_zero(A, B) -> bigi_ffi:remainder_no_zero(A, B). -file("src/bigi.gleam", 188). ?DOC( " Calculate a mathematical modulo operation.\n" "\n" " Follows the standard Gleam divide-by-zero rule of 0 when the divisor is 0.\n" ). -spec modulo(big_int(), big_int()) -> big_int(). modulo(A, B) -> bigi_ffi:modulo(A, B). -file("src/bigi.gleam", 195). ?DOC( " Calculate a mathematical modulo operation.\n" "\n" " Returns an error if the divisor is 0.\n" ). -spec modulo_no_zero(big_int(), big_int()) -> {ok, big_int()} | {error, nil}. modulo_no_zero(A, B) -> bigi_ffi:modulo_no_zero(A, B). -file("src/bigi.gleam", 205). ?DOC( " Raise the base to the exponent.\n" "\n" " If the exponent is negative, an error is returned.\n" ). -spec power(big_int(), big_int()) -> {ok, big_int()} | {error, nil}. power(A, B) -> bigi_ffi:power(A, B). -file("src/bigi.gleam", 223). -spec do_decode(gleam@dynamic:dynamic_()) -> {ok, big_int()} | {error, big_int()}. do_decode(Dyn) -> bigi_ffi:decode(Dyn). -file("src/bigi.gleam", 217). ?DOC( " Returns a decoder that decodes a Dynamic value into a big integer, if\n" " possible.\n" ). -spec decoder() -> gleam@dynamic@decode:decoder(big_int()). decoder() -> gleam@dynamic@decode:new_primitive_decoder( <<"BigInt"/utf8>>, fun bigi_ffi:decode/1 ). -file("src/bigi.gleam", 228). ?DOC(" Calculates the bitwise AND of its arguments.\n"). -spec bitwise_and(big_int(), big_int()) -> big_int(). bitwise_and(A, B) -> bigi_ffi:bitwise_and(A, B). -file("src/bigi.gleam", 233). ?DOC(" Calculates the bitwise XOR of its arguments.\n"). -spec bitwise_exclusive_or(big_int(), big_int()) -> big_int(). bitwise_exclusive_or(A, B) -> bigi_ffi:bitwise_exclusive_or(A, B). -file("src/bigi.gleam", 238). ?DOC(" Calculates the bitwise NOT of its argument.\n"). -spec bitwise_not(big_int()) -> big_int(). bitwise_not(Bigint) -> bigi_ffi:bitwise_not(Bigint). -file("src/bigi.gleam", 243). ?DOC(" Calculates the bitwise OR of its arguments.\n"). -spec bitwise_or(big_int(), big_int()) -> big_int(). bitwise_or(A, B) -> bigi_ffi:bitwise_or(A, B). -file("src/bigi.gleam", 248). ?DOC(" Calculates the result of an arithmetic left bitshift by the given amount.\n"). -spec bitwise_shift_left(big_int(), integer()) -> big_int(). bitwise_shift_left(Bigint, Amount) -> bigi_ffi:bitwise_shift_left(Bigint, Amount). -file("src/bigi.gleam", 253). ?DOC(" Calculates the result of an arithmetic right bitshift by the given amount.\n"). -spec bitwise_shift_right(big_int(), integer()) -> big_int(). bitwise_shift_right(Bigint, Amount) -> bigi_ffi:bitwise_shift_right(Bigint, Amount). -file("src/bigi.gleam", 263). ?DOC(" Returns whether the big integer provided is odd.\n"). -spec is_odd(big_int()) -> boolean(). is_odd(Bigint) -> bigi_ffi:remainder(Bigint, bigi_ffi:from(2)) /= bigi_ffi:zero(). -file("src/bigi.gleam", 268). ?DOC(" Compares two big integers, returning the larger of the two.\n"). -spec max(big_int(), big_int()) -> big_int(). max(A, B) -> case bigi_ffi:compare(A, B) of lt -> B; _ -> A end. -file("src/bigi.gleam", 276). ?DOC(" Compares two big integers, returning the smaller of the two.\n"). -spec min(big_int(), big_int()) -> big_int(). min(A, B) -> case bigi_ffi:compare(A, B) of lt -> A; _ -> B end. -file("src/bigi.gleam", 256). ?DOC(" Restricts a big integer between a lower and upper bound.\n"). -spec clamp(big_int(), big_int(), big_int()) -> big_int(). clamp(Bigint, Min_bound, Max_bound) -> _pipe = Bigint, _pipe@1 = min(_pipe, Max_bound), max(_pipe@1, Min_bound). -file("src/bigi.gleam", 286). ?DOC( " Sums a list of big integers.\n" "\n" " Returns 0 if the list was empty.\n" ). -spec sum(list(big_int())) -> big_int(). sum(Bigints) -> gleam@list:fold(Bigints, bigi_ffi:zero(), fun bigi_ffi:add/2). -file("src/bigi.gleam", 293). ?DOC( " Multiplies a list of big integers.\n" "\n" " Returns 1 if the list was empty.\n" ). -spec product(list(big_int())) -> big_int(). product(Bigints) -> gleam@list:fold(Bigints, bigi_ffi:from(1), fun bigi_ffi:multiply/2). -file("src/bigi.gleam", 300). ?DOC( " Joins a list of digits into a single value. Returns an error if the base is\n" " less than 2 or if the list contains a digit greater than or equal to the\n" " specified base.\n" ). -spec undigits(list(integer()), integer()) -> {ok, big_int()} | {error, nil}. undigits(Digits, Base) -> case Base < 2 of true -> {error, nil}; false -> Base@1 = bigi_ffi:from(Base), gleam@list:try_fold( Digits, bigi_ffi:zero(), fun(Acc, Digit) -> Digit@1 = bigi_ffi:from(Digit), case bigi_ffi:compare(Digit@1, Base@1) of gt -> {error, nil}; eq -> {error, nil}; _ -> {ok, bigi_ffi:add( bigi_ffi:multiply(Acc, Base@1), Digit@1 )} end end ) end. -file("src/bigi.gleam", 316). -spec get_digit(big_int(), list(integer()), big_int()) -> list(integer()). get_digit(Bigint, Digits, Divisor) -> case bigi_ffi:compare(Bigint, Divisor) of lt -> Digit@1 = case bigi_ffi:to(Bigint) of {ok, Digit} -> Digit; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, file => <>, module => <<"bigi"/utf8>>, function => <<"get_digit"/utf8>>, line => 319, value => _assert_fail, start => 10987, 'end' => 11024, pattern_start => 10998, pattern_end => 11007}) end, [Digit@1 | Digits]; _ -> Digit@3 = case begin _pipe = bigi_ffi:remainder(Bigint, Divisor), bigi_ffi:to(_pipe) end of {ok, Digit@2} -> Digit@2; _assert_fail@1 -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, file => <>, module => <<"bigi"/utf8>>, function => <<"get_digit"/utf8>>, line => 323, value => _assert_fail@1, start => 11072, 'end' => 11149, pattern_start => 11083, pattern_end => 11092}) end, Digits@1 = [Digit@3 | Digits], get_digit(bigi_ffi:divide(Bigint, Divisor), Digits@1, Divisor) end. -file("src/bigi.gleam", 210). ?DOC( " Get the digits in a given bigint as a list of integers in base 10.\n" "\n" " The list is ordered starting from the most significant digit.\n" ). -spec digits(big_int()) -> list(integer()). digits(Bigint) -> Divisor = bigi_ffi:from(10), get_digit(Bigint, [], Divisor).