-module(bigi). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -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, add/2, subtract/2, multiply/2, divide/2, divide_no_zero/2, remainder/2, remainder_no_zero/2, modulo/2, modulo_no_zero/2, power/2, decode/1, digits/1]). -export_type([big_int/0, endianness/0, signedness/0]). -type big_int() :: any(). -type endianness() :: little_endian | big_endian. -type signedness() :: signed | unsigned. -spec zero() -> big_int(). zero() -> bigi_ffi:zero(). -spec from_int(integer()) -> big_int(). from_int(Int) -> bigi_ffi:from(Int). -spec from_string(binary()) -> {ok, big_int()} | {error, nil}. from_string(Str) -> bigi_ffi:from_string(Str). -spec from_bytes(bitstring(), endianness(), signedness()) -> {ok, big_int()} | {error, nil}. from_bytes(Bytes, Endianness, Signedness) -> bigi_ffi:from_bytes(Bytes, Endianness, Signedness). -spec to_int(big_int()) -> {ok, integer()} | {error, nil}. to_int(Bigint) -> bigi_ffi:to(Bigint). -spec to_string(big_int()) -> binary(). to_string(Bigint) -> erlang:integer_to_binary(Bigint). -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). -spec compare(big_int(), big_int()) -> gleam@order:order(). compare(A, B) -> bigi_ffi:compare(A, B). -spec absolute(big_int()) -> big_int(). absolute(Bigint) -> erlang:abs(Bigint). -spec add(big_int(), big_int()) -> big_int(). add(A, B) -> bigi_ffi:add(A, B). -spec subtract(big_int(), big_int()) -> big_int(). subtract(A, B) -> bigi_ffi:subtract(A, B). -spec multiply(big_int(), big_int()) -> big_int(). multiply(A, B) -> bigi_ffi:multiply(A, B). -spec divide(big_int(), big_int()) -> big_int(). divide(A, B) -> bigi_ffi:divide(A, B). -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). -spec remainder(big_int(), big_int()) -> big_int(). remainder(A, B) -> bigi_ffi:remainder(A, B). -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). -spec modulo(big_int(), big_int()) -> big_int(). modulo(A, B) -> bigi_ffi:modulo(A, B). -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). -spec power(big_int(), big_int()) -> {ok, big_int()} | {error, nil}. power(A, B) -> bigi_ffi:power(A, B). -spec decode(gleam@dynamic:dynamic_()) -> {ok, big_int()} | {error, list(gleam@dynamic:decode_error())}. decode(Dyn) -> bigi_ffi:decode(Dyn). -spec get_digit(big_int(), list(integer()), big_int()) -> list(integer()). get_digit(Bigint, Digits, Divisor) -> case bigi_ffi:compare(Bigint, Divisor) of lt -> _assert_subject = bigi_ffi:to(Bigint), {ok, Digit} = case _assert_subject of {ok, _} -> _assert_subject; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Assertion pattern match failed"/utf8>>, value => _assert_fail, module => <<"bigi"/utf8>>, function => <<"get_digit"/utf8>>, line => 191}) end, [Digit | Digits]; _ -> _assert_subject@1 = begin _pipe = bigi_ffi:remainder(Bigint, Divisor), bigi_ffi:to(_pipe) end, {ok, Digit@1} = case _assert_subject@1 of {ok, _} -> _assert_subject@1; _assert_fail@1 -> erlang:error(#{gleam_error => let_assert, message => <<"Assertion pattern match failed"/utf8>>, value => _assert_fail@1, module => <<"bigi"/utf8>>, function => <<"get_digit"/utf8>>, line => 195}) end, Digits@1 = [Digit@1 | Digits], get_digit(bigi_ffi:divide(Bigint, Divisor), Digits@1, Divisor) end. -spec digits(big_int()) -> list(integer()). digits(Bigint) -> Divisor = bigi_ffi:from(10), get_digit(Bigint, [], Divisor).