Current section

Files

Jump to
bigi src bigi.erl
Raw

src/bigi.erl

-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, 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, decode/1, 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]).
-type big_int() :: any().
-type endianness() :: little_endian | big_endian.
-type signedness() :: signed | unsigned.
-file("/Users/nicd/svn/bigi/src/bigi.gleam", 25).
-spec zero() -> big_int().
zero() ->
bigi_ffi:zero().
-file("/Users/nicd/svn/bigi/src/bigi.gleam", 38).
-spec from_int(integer()) -> big_int().
from_int(Int) ->
bigi_ffi:from(Int).
-file("/Users/nicd/svn/bigi/src/bigi.gleam", 46).
-spec from_string(binary()) -> {ok, big_int()} | {error, nil}.
from_string(Str) ->
bigi_ffi:from_string(Str).
-file("/Users/nicd/svn/bigi/src/bigi.gleam", 54).
-spec from_bytes(bitstring(), endianness(), signedness()) -> {ok, big_int()} |
{error, nil}.
from_bytes(Bytes, Endianness, Signedness) ->
bigi_ffi:from_bytes(Bytes, Endianness, Signedness).
-file("/Users/nicd/svn/bigi/src/bigi.gleam", 69).
-spec to_int(big_int()) -> {ok, integer()} | {error, nil}.
to_int(Bigint) ->
bigi_ffi:to(Bigint).
-file("/Users/nicd/svn/bigi/src/bigi.gleam", 74).
-spec to_string(big_int()) -> binary().
to_string(Bigint) ->
erlang:integer_to_binary(Bigint).
-file("/Users/nicd/svn/bigi/src/bigi.gleam", 83).
-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("/Users/nicd/svn/bigi/src/bigi.gleam", 94).
-spec compare(big_int(), big_int()) -> gleam@order:order().
compare(A, B) ->
bigi_ffi:compare(A, B).
-file("/Users/nicd/svn/bigi/src/bigi.gleam", 99).
-spec absolute(big_int()) -> big_int().
absolute(Bigint) ->
erlang:abs(Bigint).
-file("/Users/nicd/svn/bigi/src/bigi.gleam", 104).
-spec negate(big_int()) -> big_int().
negate(Bigint) ->
bigi_ffi:negate(Bigint).
-file("/Users/nicd/svn/bigi/src/bigi.gleam", 109).
-spec add(big_int(), big_int()) -> big_int().
add(A, B) ->
bigi_ffi:add(A, B).
-file("/Users/nicd/svn/bigi/src/bigi.gleam", 114).
-spec subtract(big_int(), big_int()) -> big_int().
subtract(A, B) ->
bigi_ffi:subtract(A, B).
-file("/Users/nicd/svn/bigi/src/bigi.gleam", 119).
-spec multiply(big_int(), big_int()) -> big_int().
multiply(A, B) ->
bigi_ffi:multiply(A, B).
-file("/Users/nicd/svn/bigi/src/bigi.gleam", 126).
-spec divide(big_int(), big_int()) -> big_int().
divide(A, B) ->
bigi_ffi:divide(A, B).
-file("/Users/nicd/svn/bigi/src/bigi.gleam", 159).
-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("/Users/nicd/svn/bigi/src/bigi.gleam", 170).
-spec remainder(big_int(), big_int()) -> big_int().
remainder(A, B) ->
bigi_ffi:remainder(A, B).
-file("/Users/nicd/svn/bigi/src/bigi.gleam", 135).
-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("/Users/nicd/svn/bigi/src/bigi.gleam", 178).
-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("/Users/nicd/svn/bigi/src/bigi.gleam", 188).
-spec modulo(big_int(), big_int()) -> big_int().
modulo(A, B) ->
bigi_ffi:modulo(A, B).
-file("/Users/nicd/svn/bigi/src/bigi.gleam", 195).
-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("/Users/nicd/svn/bigi/src/bigi.gleam", 205).
-spec power(big_int(), big_int()) -> {ok, big_int()} | {error, nil}.
power(A, B) ->
bigi_ffi:power(A, B).
-file("/Users/nicd/svn/bigi/src/bigi.gleam", 218).
-spec decode(gleam@dynamic:dynamic_()) -> {ok, big_int()} |
{error, list(gleam@dynamic:decode_error())}.
decode(Dyn) ->
bigi_ffi:decode(Dyn).
-file("/Users/nicd/svn/bigi/src/bigi.gleam", 223).
-spec bitwise_and(big_int(), big_int()) -> big_int().
bitwise_and(A, B) ->
bigi_ffi:bitwise_and(A, B).
-file("/Users/nicd/svn/bigi/src/bigi.gleam", 228).
-spec bitwise_exclusive_or(big_int(), big_int()) -> big_int().
bitwise_exclusive_or(A, B) ->
bigi_ffi:bitwise_exclusive_or(A, B).
-file("/Users/nicd/svn/bigi/src/bigi.gleam", 233).
-spec bitwise_not(big_int()) -> big_int().
bitwise_not(Bigint) ->
bigi_ffi:bitwise_not(Bigint).
-file("/Users/nicd/svn/bigi/src/bigi.gleam", 238).
-spec bitwise_or(big_int(), big_int()) -> big_int().
bitwise_or(A, B) ->
bigi_ffi:bitwise_or(A, B).
-file("/Users/nicd/svn/bigi/src/bigi.gleam", 243).
-spec bitwise_shift_left(big_int(), integer()) -> big_int().
bitwise_shift_left(Bigint, Amount) ->
bigi_ffi:bitwise_shift_left(Bigint, Amount).
-file("/Users/nicd/svn/bigi/src/bigi.gleam", 248).
-spec bitwise_shift_right(big_int(), integer()) -> big_int().
bitwise_shift_right(Bigint, Amount) ->
bigi_ffi:bitwise_shift_right(Bigint, Amount).
-file("/Users/nicd/svn/bigi/src/bigi.gleam", 258).
-spec is_odd(big_int()) -> boolean().
is_odd(Bigint) ->
bigi_ffi:remainder(Bigint, bigi_ffi:from(2)) /= bigi_ffi:zero().
-file("/Users/nicd/svn/bigi/src/bigi.gleam", 263).
-spec max(big_int(), big_int()) -> big_int().
max(A, B) ->
case bigi_ffi:compare(A, B) of
lt ->
B;
_ ->
A
end.
-file("/Users/nicd/svn/bigi/src/bigi.gleam", 271).
-spec min(big_int(), big_int()) -> big_int().
min(A, B) ->
case bigi_ffi:compare(A, B) of
lt ->
A;
_ ->
B
end.
-file("/Users/nicd/svn/bigi/src/bigi.gleam", 251).
-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("/Users/nicd/svn/bigi/src/bigi.gleam", 281).
-spec sum(list(big_int())) -> big_int().
sum(Bigints) ->
gleam@list:fold(Bigints, bigi_ffi:zero(), fun bigi_ffi:add/2).
-file("/Users/nicd/svn/bigi/src/bigi.gleam", 288).
-spec product(list(big_int())) -> big_int().
product(Bigints) ->
gleam@list:fold(Bigints, bigi_ffi:from(1), fun bigi_ffi:multiply/2).
-file("/Users/nicd/svn/bigi/src/bigi.gleam", 295).
-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("/Users/nicd/svn/bigi/src/bigi.gleam", 311).
-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 => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"bigi"/utf8>>,
function => <<"get_digit"/utf8>>,
line => 314})
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 => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail@1,
module => <<"bigi"/utf8>>,
function => <<"get_digit"/utf8>>,
line => 318})
end,
Digits@1 = [Digit@1 | Digits],
get_digit(bigi_ffi:divide(Bigint, Divisor), Digits@1, Divisor)
end.
-file("/Users/nicd/svn/bigi/src/bigi.gleam", 210).
-spec digits(big_int()) -> list(integer()).
digits(Bigint) ->
Divisor = bigi_ffi:from(10),
get_digit(Bigint, [], Divisor).