Current section
Files
Jump to
Current section
Files
src/bigi.erl
-module(bigi).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([zero/0, from_int/1, to_int/1, to_string/1, 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, digits/1]).
-export_type([big_int/0]).
-type big_int() :: any().
-spec zero() -> big_int().
zero() ->
bigi_ffi:zero().
-spec from_int(integer()) -> big_int().
from_int(Int) ->
bigi_ffi:from(Int).
-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 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()) -> big_int().
power(A, B) ->
bigi_ffi:power(A, B).
-spec get_digit(big_int(), list(integer()), big_int()) -> list(integer()).
get_digit(Bigint, Digits, Divisor) ->
case bigi_ffi:compare(Bigint, Divisor) of
gt ->
_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 => 127})
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 => 131})
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).